std::string_view 的使用
This commit is contained in:
+12
-11
@@ -19,6 +19,7 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include "windows_include.h"
|
||||
@@ -124,20 +125,20 @@ std::string get_exe_dir() {
|
||||
return std::filesystem::path(exe_path).parent_path().string();
|
||||
}
|
||||
|
||||
std::string get_abs_path(const std::string &path) {
|
||||
std::string get_abs_path(std::string_view path) {
|
||||
if (path.empty()) {
|
||||
return path;
|
||||
return "";
|
||||
}
|
||||
|
||||
if (path.rfind("@", 0) == 0) {
|
||||
return get_exe_dir() + path.substr(1);
|
||||
return get_exe_dir() + std::string(path.substr(1));
|
||||
}
|
||||
|
||||
if (path.rfind("~", 0) == 0) {
|
||||
return get_home_dir() + path.substr(1);
|
||||
return get_home_dir() + std::string(path.substr(1));
|
||||
}
|
||||
|
||||
return path;
|
||||
return std::string(path);
|
||||
}
|
||||
|
||||
std::string utc_2_local_time(std::time_t us_timestamp, const char *format) {
|
||||
@@ -239,7 +240,7 @@ std::uint32_t base64_decode_raw(const std::uint8_t *code,
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string base64_encode(const std::string &data) {
|
||||
std::string base64_encode(std::string_view data) {
|
||||
const auto text_len = static_cast<std::uint32_t>(data.size());
|
||||
std::vector<std::uint8_t> encode(4 * ((text_len + 2) / 3));
|
||||
const std::uint32_t encoded_len =
|
||||
@@ -248,7 +249,7 @@ std::string base64_encode(const std::string &data) {
|
||||
return std::string(encode.begin(), encode.begin() + encoded_len);
|
||||
}
|
||||
|
||||
std::string base64_decode(const std::string &data) {
|
||||
std::string base64_decode(std::string_view data) {
|
||||
const auto code_len = static_cast<std::uint32_t>(data.size());
|
||||
if ((code_len & 0x03U) != 0U) {
|
||||
return "";
|
||||
@@ -304,7 +305,7 @@ std::string get_current_time_as_string() {
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
void create_file_if_not_exists(const std::string &file_name) {
|
||||
void create_file_if_not_exists(std::string_view file_name) {
|
||||
std::error_code ec;
|
||||
const fs::path file_path(file_name);
|
||||
|
||||
@@ -333,7 +334,7 @@ void create_file_if_not_exists(const std::string &file_name) {
|
||||
fs::permissions(file_path, perms, ec);
|
||||
}
|
||||
|
||||
void create_dir_if_not_exists(const std::string &dir_name) {
|
||||
void create_dir_if_not_exists(std::string_view dir_name) {
|
||||
std::error_code ec;
|
||||
const fs::path dir_path(dir_name);
|
||||
|
||||
@@ -351,8 +352,8 @@ void create_dir_if_not_exists(const std::string &dir_name) {
|
||||
fs::permissions(dir_path, perms, ec);
|
||||
}
|
||||
|
||||
void write_stack_trace_to_file(const std::string &stack_trace,
|
||||
const std::string &filename) {
|
||||
void write_stack_trace_to_file(std::string_view stack_trace,
|
||||
std::string_view filename) {
|
||||
create_file_if_not_exists(filename);
|
||||
std::ofstream out_file(filename, std::ios::out | std::ios::app);
|
||||
if (out_file.is_open()) {
|
||||
|
||||
Reference in New Issue
Block a user