mirror of
https://github.com/Vita3K/Vita3K.git
synced 2026-07-11 01:34:23 +02:00
util: Optimize string utils
This commit is contained in:
@@ -43,7 +43,7 @@ std::string lowercase_copy(std::string value) {
|
||||
}
|
||||
|
||||
std::string normalize_locale(std::string locale) {
|
||||
locale = string_utils::trim_copy(std::move(locale));
|
||||
locale = string_utils::trim_copy(locale);
|
||||
std::replace(locale.begin(), locale.end(), '_', '-');
|
||||
return lowercase_copy(std::move(locale));
|
||||
}
|
||||
@@ -99,7 +99,7 @@ std::string localized_text(const pugi::xml_node &node, const std::string &prefer
|
||||
|
||||
fs::path resolve_asset_path(const fs::path &theme_dir, std::string asset_value) {
|
||||
const QString relative_path = gui::utils::sanitize_relative_path_reference(
|
||||
QString::fromStdString(string_utils::trim_copy(std::move(asset_value))));
|
||||
QString::fromStdString(string_utils::trim_copy(asset_value)));
|
||||
if (relative_path.isEmpty())
|
||||
return {};
|
||||
|
||||
@@ -107,7 +107,7 @@ fs::path resolve_asset_path(const fs::path &theme_dir, std::string asset_value)
|
||||
}
|
||||
|
||||
std::optional<VitaThemeColor> parse_theme_color(std::string raw_value) {
|
||||
raw_value = string_utils::trim_copy(std::move(raw_value));
|
||||
raw_value = string_utils::trim_copy(raw_value);
|
||||
if (raw_value.empty())
|
||||
return std::nullopt;
|
||||
|
||||
|
||||
@@ -23,18 +23,16 @@
|
||||
|
||||
namespace string_utils {
|
||||
|
||||
std::vector<std::string> split_string(const std::string &str, char delimiter);
|
||||
|
||||
std::wstring utf_to_wide(const std::string &str);
|
||||
std::string wide_to_utf(const std::wstring &str);
|
||||
std::string utf16_to_utf8(const std::u16string &str);
|
||||
std::u16string utf8_to_utf16(const std::string &str);
|
||||
std::string trim_copy(const std::string &str);
|
||||
std::string trim_copy(std::string_view str);
|
||||
std::string remove_special_chars(std::string str);
|
||||
void replace(std::string &str, const std::string &in, const std::string &out);
|
||||
std::vector<uint8_t> string_to_byte_array(const std::string &string);
|
||||
std::string toupper(const std::string &s);
|
||||
std::string tolower(const std::string &s);
|
||||
void replace(std::string &str, std::string_view in, std::string_view out);
|
||||
std::vector<uint8_t> string_to_byte_array(std::string_view string);
|
||||
std::string toupper(std::string s);
|
||||
std::string tolower(std::string s);
|
||||
int stoi_def(const std::string &str, int default_value = 0, const char *name = "value");
|
||||
|
||||
} // namespace string_utils
|
||||
|
||||
@@ -20,29 +20,13 @@
|
||||
#include <util/log.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
#include <ranges>
|
||||
|
||||
namespace string_utils {
|
||||
|
||||
std::vector<std::string> split_string(const std::string &str, char delimiter) {
|
||||
std::istringstream str_stream(str);
|
||||
std::string segment;
|
||||
std::vector<std::string> seglist;
|
||||
|
||||
const size_t num_segments = std::count_if(str.begin(), str.end(), [&](char c) {
|
||||
return c == delimiter;
|
||||
}) + (str.empty() ? 1 : 0);
|
||||
|
||||
seglist.reserve(num_segments);
|
||||
|
||||
while (std::getline(str_stream, segment, delimiter)) {
|
||||
seglist.push_back(segment);
|
||||
}
|
||||
return seglist;
|
||||
}
|
||||
|
||||
std::wstring utf_to_wide(const std::string &str) {
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
|
||||
return myconv.from_bytes(str);
|
||||
@@ -53,18 +37,13 @@ std::string wide_to_utf(const std::wstring &str) {
|
||||
return myconv.to_bytes(str);
|
||||
}
|
||||
|
||||
std::string trim_copy(const std::string &str) {
|
||||
const auto begin = std::find_if_not(str.begin(), str.end(), [](const unsigned char ch) {
|
||||
return std::isspace(ch) != 0;
|
||||
});
|
||||
const auto end = std::find_if_not(str.rbegin(), str.rend(), [](const unsigned char ch) {
|
||||
return std::isspace(ch) != 0;
|
||||
}).base();
|
||||
|
||||
if (begin >= end)
|
||||
return {};
|
||||
|
||||
return std::string(begin, end);
|
||||
std::string trim_copy(std::string_view str) {
|
||||
return str
|
||||
| std::views::drop_while([](unsigned char ch) { return std::isspace(ch); })
|
||||
| std::views::reverse
|
||||
| std::views::drop_while([](unsigned char ch) { return std::isspace(ch); })
|
||||
| std::views::reverse
|
||||
| std::ranges::to<std::string>();
|
||||
}
|
||||
|
||||
std::string remove_special_chars(std::string str) {
|
||||
@@ -90,7 +69,7 @@ std::string remove_special_chars(std::string str) {
|
||||
|
||||
// Based on: https://stackoverflow.com/a/23135441
|
||||
// Search and replace "in" with "out" in the given string
|
||||
void replace(std::string &str, const std::string &in, const std::string &out) {
|
||||
void replace(std::string &str, std::string_view in, std::string_view out) {
|
||||
size_t pos = 0;
|
||||
while ((pos = str.find(in, pos)) != std::string::npos) {
|
||||
str.replace(pos, in.length(), out);
|
||||
@@ -98,16 +77,18 @@ void replace(std::string &str, const std::string &in, const std::string &out) {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> string_to_byte_array(const std::string &string) {
|
||||
std::vector<uint8_t> string_to_byte_array(std::string_view string) {
|
||||
if (string.length() % 2 != 0) {
|
||||
LOG_ERROR("Invalid hex string: {:?}", string);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<uint8_t> hex_bytes;
|
||||
hex_bytes.reserve(string.length() / 2);
|
||||
|
||||
if (string.length() % 2 != 0)
|
||||
LOG_WARN("Hex string length ({}) is not even", string.length());
|
||||
|
||||
for (size_t i = 0; i < string.length(); i += 2) {
|
||||
std::string byte = string.substr(i, 2);
|
||||
hex_bytes.push_back(static_cast<uint8_t>(std::strtoul(byte.c_str(), nullptr, 16)));
|
||||
for (const char *p = string.data(); p < std::to_address(string.end()); p += 2) {
|
||||
uint8_t byte = 0;
|
||||
std::from_chars(p, p + 2, byte, 16);
|
||||
hex_bytes.push_back(byte);
|
||||
}
|
||||
return hex_bytes;
|
||||
}
|
||||
@@ -140,18 +121,14 @@ std::u16string utf8_to_utf16(const std::string &str) {
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string toupper(const std::string &s) {
|
||||
std::string r = s;
|
||||
std::transform(r.begin(), r.end(), r.begin(),
|
||||
[](unsigned char c) { return std::toupper(c); });
|
||||
return r;
|
||||
std::string toupper(std::string s) {
|
||||
std::ranges::transform(s, s.begin(), [](unsigned char c) { return std::toupper(c); });
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string tolower(const std::string &s) {
|
||||
std::string r = s;
|
||||
std::transform(r.begin(), r.end(), r.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
return r;
|
||||
std::string tolower(std::string s) {
|
||||
std::ranges::transform(s, s.begin(), [](unsigned char c) { return std::tolower(c); });
|
||||
return s;
|
||||
}
|
||||
|
||||
int stoi_def(const std::string &str, int default_value, const char *name) {
|
||||
|
||||
Reference in New Issue
Block a user