cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifndef CPP_TOOLBOX_CONTAINER_STRING_HPP
4# define CPP_TOOLBOX_CONTAINER_STRING_HPP
5
6# include <limits>
7# include <string>
8# include <string_view>
9# include <vector>
10
11# include <cpp-toolbox/cpp-toolbox_export.hpp>
12
14{
40CPP_TOOLBOX_EXPORT auto split(std::string_view str, std::string_view delimiter)
41 -> std::vector<std::string>;
42
67CPP_TOOLBOX_EXPORT auto split(std::string_view str, char delimiter)
68 -> std::vector<std::string>;
69
93CPP_TOOLBOX_EXPORT auto join(const std::vector<std::string>& parts,
94 std::string_view glue) -> std::string;
95
119CPP_TOOLBOX_EXPORT auto join(const std::vector<std::string_view>& parts,
120 std::string_view glue) -> std::string;
121
144CPP_TOOLBOX_EXPORT auto trim_left(std::string_view str) -> std::string;
145
168CPP_TOOLBOX_EXPORT auto trim_right(std::string_view str) -> std::string;
169
191CPP_TOOLBOX_EXPORT auto trim(std::string_view str) -> std::string;
192
215CPP_TOOLBOX_EXPORT auto starts_with(std::string_view s, std::string_view prefix)
216 -> bool;
217
240CPP_TOOLBOX_EXPORT auto ends_with(std::string_view s, std::string_view suffix)
241 -> bool;
242
265CPP_TOOLBOX_EXPORT auto contains(std::string_view s, std::string_view substring)
266 -> bool;
267
292CPP_TOOLBOX_EXPORT auto is_empty_or_whitespace(std::string_view s) -> bool;
293
319CPP_TOOLBOX_EXPORT auto is_numeric(std::string_view s) -> bool;
320
346CPP_TOOLBOX_EXPORT auto is_integer(std::string_view s) -> bool;
347
374CPP_TOOLBOX_EXPORT auto is_float(std::string_view s) -> bool;
375
405CPP_TOOLBOX_EXPORT auto replace(
406 std::string_view s,
407 std::string_view old_value,
408 std::string_view new_value,
409 std::size_t count = std::numeric_limits<std::size_t>::max()) -> std::string;
410
433CPP_TOOLBOX_EXPORT auto replace_all(std::string_view s,
434 std::string_view old_value,
435 std::string_view new_value) -> std::string;
436
467CPP_TOOLBOX_EXPORT auto replace_by_nth(std::string_view s,
468 std::string_view old_value,
469 std::string_view new_value,
470 std::size_t n) -> std::string;
471
501CPP_TOOLBOX_EXPORT auto remove_nth(std::string_view s,
502 std::string_view from,
503 std::size_t n) -> std::string;
504
529CPP_TOOLBOX_EXPORT auto remove(
530 std::string_view s,
531 std::string_view value,
532 std::size_t count = std::numeric_limits<std::size_t>::max()) -> std::string;
533
557CPP_TOOLBOX_EXPORT auto remove_all(std::string_view s, std::string_view value)
558 -> std::string;
559
582CPP_TOOLBOX_EXPORT auto remove_all(std::string_view s, char value)
583 -> std::string;
584
606CPP_TOOLBOX_EXPORT auto to_lower(std::string_view s) -> std::string;
607
629CPP_TOOLBOX_EXPORT auto to_upper(std::string_view s) -> std::string;
630
656CPP_TOOLBOX_EXPORT auto to_string(std::string_view s) -> std::string;
657
684CPP_TOOLBOX_EXPORT auto left_pad(std::string_view s,
685 std::size_t width,
686 char pad_char = ' ') -> std::string;
687
714CPP_TOOLBOX_EXPORT auto right_pad(std::string_view s,
715 std::size_t width,
716 char pad_char = ' ') -> std::string;
717
755CPP_TOOLBOX_EXPORT auto pad(std::string_view s,
756 std::size_t width,
757 char pad_char = ' ',
758 std::size_t position = 0) -> std::string;
759
782CPP_TOOLBOX_EXPORT auto reverse(std::string_view s) -> std::string;
783
820CPP_TOOLBOX_EXPORT auto try_parse_int(std::string_view s, int& out) -> bool;
821
858CPP_TOOLBOX_EXPORT auto try_parse_double(std::string_view s, double& out)
859 -> bool;
860
897CPP_TOOLBOX_EXPORT auto try_parse_float(std::string_view s, float& out) -> bool;
898
927CPP_TOOLBOX_EXPORT auto levenshtein_distance(std::string_view s1,
928 std::string_view s2)
929 -> std::size_t;
930
958CPP_TOOLBOX_EXPORT auto longest_common_subsequence_length(std::string_view s1,
959 std::string_view s2)
960 -> std::size_t;
961
991CPP_TOOLBOX_EXPORT auto longest_common_substring_length(std::string_view s1,
992 std::string_view s2)
993 -> std::size_t;
994
1024CPP_TOOLBOX_EXPORT auto url_encode(std::string_view s) -> std::string;
1025
1058CPP_TOOLBOX_EXPORT auto url_decode(std::string_view s) -> std::string;
1059
1085CPP_TOOLBOX_EXPORT auto base64_encode(std::string_view data) -> std::string;
1086
1117CPP_TOOLBOX_EXPORT auto base64_decode(std::string_view encoded_data)
1118 -> std::string;
1119
1153CPP_TOOLBOX_EXPORT auto slugify(std::string_view s) -> std::string;
1154
1175CPP_TOOLBOX_EXPORT auto hexview(const char* data,
1176 std::size_t size,
1177 bool with_prefix = true) -> std::string;
1178
1198CPP_TOOLBOX_EXPORT auto hexview(const std::string& data,
1199 bool with_prefix = true) -> std::string;
1200
1221CPP_TOOLBOX_EXPORT auto hexview(const std::vector<char>& data,
1222 bool with_prefix = true) -> std::string;
1223
1244CPP_TOOLBOX_EXPORT auto hexview(const std::vector<std::byte>& data,
1245 bool with_prefix = true) -> std::string;
1246
1268template<typename T>
1269auto hex_to_integral(const std::string& hex_str, bool with_prefix = true) -> T
1270{
1271 static_assert(std::is_integral_v<T>,
1272 "T 必须是整型/T must be an integral type");
1273 std::string_view s(hex_str);
1274 // 跳过可选的 "0x" 或 "0X" 前缀/Skip optional "0x" or "0X" prefix
1275 if (with_prefix && s.size() >= 2 && s[0] == '0'
1276 && (s[1] == 'x' || s[1] == 'X'))
1277 {
1278 s.remove_prefix(2);
1279 }
1280 if (s.empty()) { // Simplified empty check after prefix removal
1281 throw std::invalid_argument(
1282 "非法的十六进制字符串: 空字符串或只有前缀/Invalid hex string: empty or "
1283 "only prefix");
1284 }
1285 // 十六进制字符串长度必须是偶数,除非它是单个 '0' / Hex string length must be
1286 // even, unless it's a single '0'
1287 if (s.size() % 2 != 0 && s != "0") { // 允许单个 "0" / Allow single "0"
1288 throw std::invalid_argument(
1289 "非法的十六进制字符串长度 (必须是偶数,除非是'0')/Invalid hex string "
1290 "length (must be even, unless it's '0')");
1291 }
1292
1293 // 使用 std::stoull 自动处理 base 参数/Use std::stoull to handle base
1294 // parameter automatically
1295 size_t pos = 0; // To check how many characters were consumed
1296 unsigned long long tmp = 0;
1297 try {
1298 tmp = std::stoull(std::string(s), &pos, 16);
1299 } catch (const std::invalid_argument& e) {
1300 // Re-throw with a potentially more informative message if needed, or just
1301 // let it propagate
1302 throw std::invalid_argument("非法的十六进制字符/Invalid hex character(s)");
1303 } catch (const std::out_of_range& e) {
1304 // Let std::out_of_range propagate
1305 throw;
1306 }
1307
1308 // 检查是否所有字符都被消耗/Check if all characters were consumed
1309 if (pos != s.length()) {
1310 throw std::invalid_argument(
1311 "包含非法十六进制字符或尾随字符/Contains invalid hex or trailing "
1312 "characters");
1313 }
1314
1315 // Check for potential overflow when casting back to T (stoull checks against
1316 // ULLONG_MAX) This is a bit tricky, especially for signed types. A simple
1317 // check for unsigned types:
1318 if constexpr (std::is_unsigned_v<T>) {
1319 if (tmp > static_cast<unsigned long long>(std::numeric_limits<T>::max())) {
1320 throw std::out_of_range(
1321 "值超出目标类型范围/Value out of range for target type");
1322 }
1323 } else { // For signed types
1324 // This comparison logic needs careful consideration of T's range
1325 // For simplicity, we might rely on the static_cast behavior or add more
1326 // complex checks if needed. E.g., check if tmp fits within both min and max
1327 // of T if (tmp > static_cast<unsigned long
1328 // long>(std::numeric_limits<T>::max()) &&
1329 // (tmp > -static_cast<long long>(std::numeric_limits<T>::min()) ) ) {
1330 // ... }
1331 // Simpler check: If the original string wasn't negative and result is >
1332 // max, it's overflow
1333 if (tmp > static_cast<unsigned long long>(std::numeric_limits<T>::max())) {
1334 // Check if it could represent a negative number within range if T is
1335 // signed This might require more robust two's complement handling
1336 // depending on exact needs. For now, let's assume stoull parsed correctly
1337 // and cast might overflow. A safer approach might involve parsing
1338 // differently for signed types. Let's keep the basic check for now.
1339 if (tmp > static_cast<unsigned long long>(std::numeric_limits<T>::max()))
1340 {
1341 throw std::out_of_range(
1342 "值超出目标类型范围/Value out of range for target type");
1343 }
1344 }
1345 }
1346
1347 return static_cast<T>(tmp);
1348}
1349
1371template<typename Container>
1372auto hex_to_bytes(const std::string& hex_str, bool with_prefix = true)
1373 -> Container
1374{
1375 using Byte = typename Container::value_type;
1376 std::string_view s(hex_str);
1377 if (with_prefix && s.size() >= 2 && s[0] == '0'
1378 && (s[1] == 'x' || s[1] == 'X'))
1379 {
1380 s.remove_prefix(2);
1381 }
1382 if (s.size() % 2 != 0) {
1383 throw std::invalid_argument(
1384 "十六进制字符串长度必须是偶数/Hex string length must be even");
1385 }
1386 Container output;
1387 output.clear();
1388 output.reserve(s.size() / 2);
1389 for (size_t i = 0; i < s.size(); i += 2) {
1390 auto hi = s[i];
1391 auto lo = s[i + 1];
1392 if (!std::isxdigit(hi) || !std::isxdigit(lo)) {
1393 throw std::invalid_argument(
1394 "包含非法十六进制字符/Contains invalid hex characters");
1395 }
1396 Byte byte = static_cast<Byte>(
1397 ((std::isdigit(hi) ? hi - '0' : std::toupper(hi) - 'A' + 10) * 16)
1398 + (std::isdigit(lo) ? lo - '0' : std::toupper(lo) - 'A' + 10));
1399 output.push_back(byte);
1400 }
1401 return output;
1402}
1403
1404} // namespace toolbox::container::string
1405#endif // CPP_TOOLBOX_CONTAINER_STRING_HPP
Definition string.hpp:14
CPP_TOOLBOX_EXPORT auto is_float(std::string_view s) -> bool
检查字符串视图是否表示有效的浮点数值 / Checks if a string view represents a valid floating-point value
CPP_TOOLBOX_EXPORT auto left_pad(std::string_view s, std::size_t width, char pad_char=' ') -> std::string
在字符串左侧填充指定字符以达到最小宽度 / Pads a string on the left with a specified character to reach a minimum width
CPP_TOOLBOX_EXPORT auto replace(std::string_view s, std::string_view old_value, std::string_view new_value, std::size_t count=std::numeric_limits< std::size_t >::max()) -> std::string
在字符串视图中将一个子字符串替换为另一个子字符串,最多替换指定次数 / Replaces occurrences of a substring within a string view with ano...
CPP_TOOLBOX_EXPORT auto to_string(std::string_view s) -> std::string
将字符串视图转换为标准字符串 / Converts a string view to a standard string
CPP_TOOLBOX_EXPORT auto longest_common_substring_length(std::string_view s1, std::string_view s2) -> std::size_t
计算两个字符串的最长公共子串长度 / Calculates the length of the Longest Common Substring of two strings
CPP_TOOLBOX_EXPORT auto trim(std::string_view str) -> std::string
删除字符串视图两端的空白字符 / Removes both leading and trailing whitespace characters from a string view
CPP_TOOLBOX_EXPORT auto to_lower(std::string_view s) -> std::string
将字符串视图中的所有字符转换为小写 / Converts all characters in a string view to lowercase
CPP_TOOLBOX_EXPORT auto reverse(std::string_view s) -> std::string
反转字符串视图中字符的顺序 / Reverses the order of characters in a string view
CPP_TOOLBOX_EXPORT auto try_parse_int(std::string_view s, int &out) -> bool
尝试从字符串视图解析整数 / Attempts to parse an integer from a string view
CPP_TOOLBOX_EXPORT auto url_encode(std::string_view s) -> std::string
对字符串进行百分比编码以安全地包含在URL中 / Percent-encodes a string for safe inclusion in a URL
CPP_TOOLBOX_EXPORT auto right_pad(std::string_view s, std::size_t width, char pad_char=' ') -> std::string
在字符串右侧填充指定字符以达到最小宽度 / Pads a string on the right with a specified character to reach a minimum width
CPP_TOOLBOX_EXPORT auto is_numeric(std::string_view s) -> bool
检查字符串视图是否表示数值(整数或浮点数)/ Checks if a string view represents a numeric value (integer or floating-point)
CPP_TOOLBOX_EXPORT auto hexview(const char *data, std::size_t size, bool with_prefix=true) -> std::string
将二进制数据转换为十六进制字符串表示/Convert binary data to hexadecimal string representation
CPP_TOOLBOX_EXPORT auto ends_with(std::string_view s, std::string_view suffix) -> bool
检查字符串视图是否以指定后缀结尾 / Checks if a string view ends with a specified suffix
CPP_TOOLBOX_EXPORT auto is_empty_or_whitespace(std::string_view s) -> bool
检查字符串视图是否为空或仅包含空白字符 / Checks if a string view is empty or consists only of whitespace characters
CPP_TOOLBOX_EXPORT auto trim_left(std::string_view str) -> std::string
删除字符串视图开头的空白字符 / Removes leading whitespace characters from a string view
CPP_TOOLBOX_EXPORT auto base64_decode(std::string_view encoded_data) -> std::string
将Base64编码的字符串解码回原始数据 / Decodes a Base64 encoded string back into its original data
CPP_TOOLBOX_EXPORT auto try_parse_float(std::string_view s, float &out) -> bool
尝试从字符串视图解析单精度浮点数 / Attempts to parse a single-precision floating-point number from a string view
CPP_TOOLBOX_EXPORT auto is_integer(std::string_view s) -> bool
检查字符串视图是否表示有效的整数值 / Checks if a string view represents a valid integer value
CPP_TOOLBOX_EXPORT auto replace_all(std::string_view s, std::string_view old_value, std::string_view new_value) -> std::string
替换字符串视图中所有出现的子字符串 / Replaces all occurrences of a substring within a string view
CPP_TOOLBOX_EXPORT auto remove_all(std::string_view s, std::string_view value) -> std::string
从字符串视图中删除所有出现的子字符串 / Removes all occurrences of a substring from a string view
CPP_TOOLBOX_EXPORT auto slugify(std::string_view s) -> std::string
将字符串转换为URL友好的"slug" / Converts a string into a URL-friendly "slug"
CPP_TOOLBOX_EXPORT auto replace_by_nth(std::string_view s, std::string_view old_value, std::string_view new_value, std::size_t n) -> std::string
替换字符串中第N次出现的子字符串 / Replaces the Nth occurrence of a substring within a string
CPP_TOOLBOX_EXPORT auto to_upper(std::string_view s) -> std::string
将字符串视图中的所有字符转换为大写 / Converts all characters in a string view to uppercase
CPP_TOOLBOX_EXPORT auto longest_common_subsequence_length(std::string_view s1, std::string_view s2) -> std::size_t
计算两个字符串的最长公共子序列(LCS)长度 / Calculates the length of the Longest Common Subsequence (LCS) of two strings
CPP_TOOLBOX_EXPORT auto contains(std::string_view s, std::string_view substring) -> bool
检查字符串视图是否包含指定子字符串 / Checks if a string view contains a specified substring
auto hex_to_integral(const std::string &hex_str, bool with_prefix=true) -> T
将十六进制字符串转换为整数类型/Convert hexadecimal string to integral type
Definition string.hpp:1269
CPP_TOOLBOX_EXPORT auto pad(std::string_view s, std::size_t width, char pad_char=' ', std::size_t position=0) -> std::string
使用指定字符填充字符串以达到最小宽度,可控制填充位置 / Pads a string with a specified character to reach a minimum width,...
auto hex_to_bytes(const std::string &hex_str, bool with_prefix=true) -> Container
将十六进制字符串转换为字节容器/Convert hexadecimal string to byte container
Definition string.hpp:1372
CPP_TOOLBOX_EXPORT auto split(std::string_view str, std::string_view delimiter) -> std::vector< std::string >
使用字符串分隔符将字符串视图分割成字符串向量 / Splits a string view into a vector of strings based on a string delimiter.
CPP_TOOLBOX_EXPORT auto remove(std::string_view s, std::string_view value, std::size_t count=std::numeric_limits< std::size_t >::max()) -> std::string
从字符串视图中删除子字符串的出现,最多删除指定次数 / Removes occurrences of a substring from a string view,...
CPP_TOOLBOX_EXPORT auto url_decode(std::string_view s) -> std::string
解码百分比编码的字符串(URL解码) / Decodes a percent-encoded string (URL decoding)
CPP_TOOLBOX_EXPORT auto join(const std::vector< std::string > &parts, std::string_view glue) -> std::string
使用连接字符串将字符串向量连接成单个字符串 / Joins a vector of strings into a single string, separated by a glue string
CPP_TOOLBOX_EXPORT auto levenshtein_distance(std::string_view s1, std::string_view s2) -> std::size_t
计算两个字符串之间的Levenshtein距离 / Calculates the Levenshtein distance between two strings
CPP_TOOLBOX_EXPORT auto starts_with(std::string_view s, std::string_view prefix) -> bool
检查字符串视图是否以指定前缀开头 / Checks if a string view starts with a specified prefix
CPP_TOOLBOX_EXPORT auto base64_encode(std::string_view data) -> std::string
将二进制数据(以字符串视图表示)编码为Base64格式 / Encodes binary data (represented as a string_view) into Base64 format
CPP_TOOLBOX_EXPORT auto remove_nth(std::string_view s, std::string_view from, std::size_t n) -> std::string
删除字符串中第N次出现的子字符串 / Removes the Nth occurrence of a substring within a string
CPP_TOOLBOX_EXPORT auto try_parse_double(std::string_view s, double &out) -> bool
尝试从字符串视图解析双精度浮点数 / Attempts to parse a double-precision floating-point number from a string view
CPP_TOOLBOX_EXPORT auto trim_right(std::string_view str) -> std::string
删除字符串视图末尾的空白字符 / Removes trailing whitespace characters from a string view