From 9221465e57abd01f35a266f414f74182e69e8b0e Mon Sep 17 00:00:00 2001 From: otavepto Date: Sun, 14 Apr 2024 14:40:49 +0200 Subject: [PATCH] use string_view in common helpers --- helpers/common_helpers.cpp | 110 +++++++++++----------- helpers/common_helpers/common_helpers.hpp | 55 +++++------ 2 files changed, 77 insertions(+), 88 deletions(-) diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index 1a66bf23..57c0d358 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -19,19 +19,19 @@ static bool create_dir_impl(std::filesystem::path &dirpath) return std::filesystem::create_directories(dirpath); } -bool common_helpers::create_dir(const std::string &filepath) +bool common_helpers::create_dir(const std::string_view &filepath) { - std::filesystem::path parent = std::filesystem::path(filepath).parent_path(); + std::filesystem::path parent(std::filesystem::path(filepath).parent_path()); return create_dir_impl(parent); } -bool common_helpers::create_dir(const std::wstring &filepath) +bool common_helpers::create_dir(const std::wstring_view &filepath) { - std::filesystem::path parent = std::filesystem::path(filepath).parent_path(); + std::filesystem::path parent(std::filesystem::path(filepath).parent_path()); return create_dir_impl(parent); } -void common_helpers::write(std::ofstream &file, const std::string &data) +void common_helpers::write(std::ofstream &file, const std::string_view &data) { if (!file.is_open()) { return; @@ -40,7 +40,7 @@ void common_helpers::write(std::ofstream &file, const std::string &data) file << data << std::endl; } -std::wstring common_helpers::str_to_w(const std::string &str) +std::wstring common_helpers::str_to_w(const std::string_view &str) { if (str.empty()) return std::wstring(); auto cvt_state = std::mbstate_t(); @@ -51,7 +51,7 @@ std::wstring common_helpers::str_to_w(const std::string &str) return res.substr(0, conversion_bytes); } -std::string common_helpers::wstr_to_a(const std::wstring &wstr) +std::string common_helpers::wstr_to_a(const std::wstring_view &wstr) { if (wstr.empty()) return std::string(); auto cvt_state = std::mbstate_t(); @@ -62,53 +62,45 @@ std::string common_helpers::wstr_to_a(const std::wstring &wstr) return res.substr(0, conversion_bytes); } -bool common_helpers::starts_with_i(const std::string &target, const std::string &query) -{ - return starts_with_i(str_to_w(target), str_to_w(query)); -} - -bool common_helpers::starts_with_i(const std::wstring &target, const std::wstring &query) +bool common_helpers::starts_with_i(const std::string_view &target, const std::string_view &query) { if (target.size() < query.size()) { return false; } - auto _target = std::wstring(target); - auto _query = std::wstring(query); - std::transform(_target.begin(), _target.end(), _target.begin(), - [](wchar_t c) { return std::tolower(c); }); - - std::transform(_query.begin(), _query.end(), _query.begin(), - [](wchar_t c) { return std::tolower(c); }); - - return _target.compare(0, _query.length(), _query) == 0; - + return str_cmp_insensitive(target.substr(0, query.size()), query); } -bool common_helpers::ends_with_i(const std::string &target, const std::string &query) -{ - return ends_with_i(str_to_w(target), str_to_w(query)); -} - -bool common_helpers::ends_with_i(const std::wstring &target, const std::wstring &query) +bool common_helpers::starts_with_i(const std::wstring_view &target, const std::wstring_view &query) { if (target.size() < query.size()) { return false; } - auto _target = std::wstring(target); - auto _query = std::wstring(query); - std::transform(_target.begin(), _target.end(), _target.begin(), - [](wchar_t c) { return std::tolower(c); }); - - std::transform(_query.begin(), _query.end(), _query.begin(), - [](wchar_t c) { return std::tolower(c); }); - - return _target.compare(_target.length() - _query.length(), _query.length(), _query) == 0; - + return str_cmp_insensitive(target.substr(0, query.size()), query); } -std::string common_helpers::string_strip(const std::string& str) +bool common_helpers::ends_with_i(const std::string_view &target, const std::string_view &query) +{ + if (target.size() < query.size()) { + return false; + } + + const auto target_offset = target.length() - query.length(); + return str_cmp_insensitive(target.substr(target_offset), query); +} + +bool common_helpers::ends_with_i(const std::wstring_view &target, const std::wstring_view &query) +{ + if (target.size() < query.size()) { + return false; + } + + const auto target_offset = target.length() - query.length(); + return str_cmp_insensitive(target.substr(target_offset), query); +} + +std::string common_helpers::string_strip(const std::string_view &str) { static constexpr const char whitespaces[] = " \t\r\n"; @@ -124,18 +116,17 @@ std::string common_helpers::string_strip(const std::string& str) } } - return str.substr(start, end - start + 1); + return std::string(str.substr(start, end - start + 1)); } -std::string common_helpers::uint8_vector_to_hex_string(const std::vector& v) +std::string common_helpers::uint8_vector_to_hex_string(const std::vector &v) { std::string result{}; result.reserve(v.size() * 2); // two digits per character static constexpr const char hex[] = "0123456789ABCDEF"; - for (uint8_t c : v) - { + for (uint8_t c : v) { result.push_back(hex[c / 16]); result.push_back(hex[c % 16]); } @@ -143,7 +134,7 @@ std::string common_helpers::uint8_vector_to_hex_string(const std::vector #include +#include #include #include #include @@ -9,29 +10,26 @@ namespace common_helpers { -bool create_dir(const std::string &dir); +bool create_dir(const std::string_view &dir); +bool create_dir(const std::wstring_view &dir); -bool create_dir(const std::wstring &dir); +void write(std::ofstream &file, const std::string_view &data); -void write(std::ofstream &file, const std::string &data); +std::wstring str_to_w(const std::string_view &str); +std::string wstr_to_a(const std::wstring_view &wstr); -std::wstring str_to_w(const std::string &str); +bool starts_with_i(const std::string_view &target, const std::string_view &query); +bool starts_with_i(const std::wstring_view &target, const std::wstring_view &query); -std::string wstr_to_a(const std::wstring &wstr); +bool ends_with_i(const std::string_view &target, const std::string_view &query); +bool ends_with_i(const std::wstring_view &target, const std::wstring_view &query); -bool starts_with_i(const std::string &target, const std::string &query); +std::string string_strip(const std::string_view &str); -bool starts_with_i(const std::wstring &target, const std::wstring &query); +std::string uint8_vector_to_hex_string(const std::vector &v); -bool ends_with_i(const std::string &target, const std::string &query); - -bool ends_with_i(const std::wstring &target, const std::wstring &query); - -std::string string_strip(const std::string& str); - -std::string uint8_vector_to_hex_string(const std::vector& v); - -bool str_cmp_insensitive(const std::string &str1, const std::string &str2); +bool str_cmp_insensitive(const std::string_view &str1, const std::string_view &str2); +bool str_cmp_insensitive(const std::wstring_view &str1, const std::wstring_view &str2); std::string ascii_to_lowercase(std::string data); @@ -39,34 +37,25 @@ void thisThreadYieldFor(std::chrono::microseconds u); void consume_bom(std::ifstream &input); -std::string to_lower(std::string str); +std::string to_lower(const std::string_view &str); +std::wstring to_lower(const std::wstring_view &wstr); -std::wstring to_lower(std::wstring wstr); +std::string to_upper(const std::string_view &str); +std::wstring to_upper(const std::wstring_view &wstr); -std::string to_upper(std::string str); - -std::wstring to_upper(std::wstring wstr); - -std::string to_absolute(const std::string &path, const std::string &base = std::string()); - -std::wstring to_absolute(const std::wstring &path, const std::wstring &base = std::wstring()); +std::string to_absolute(const std::string_view &path, const std::string_view &base = std::string_view()); +std::wstring to_absolute(const std::wstring_view &path, const std::wstring_view &base = std::wstring_view()); bool file_exist(const std::filesystem::path &filepath); - bool file_exist(const std::string &filepath); - bool file_exist(const std::wstring &filepath); bool file_size(const std::filesystem::path &filepath, size_t &size); - bool file_size(const std::string &filepath, size_t &size); - bool file_size(const std::wstring &filepath, size_t &size); bool dir_exist(const std::filesystem::path &dirpath); - -bool dir_exist(const std::string &dirpath); - -bool dir_exist(const std::wstring &dirpath); +bool dir_exist(const std::string_view &dirpath); +bool dir_exist(const std::wstring_view &dirpath); }