String conversion utilities.
More...
#include <convert_string.h>
|
| static std::pair< std::vector< uint8_t >, std::string > | to_array (const std::string &str) |
| | Convert string to byte array.
|
| |
| static std::pair< std::string, std::string > | to_string (const std::vector< uint8_t > &arr) |
| | Convert byte array to string.
|
| |
| static std::pair< std::string, std::string > | to_base64 (const std::vector< uint8_t > &data) |
| | Convert data to base64.
|
| |
| static std::pair< std::vector< uint8_t >, std::string > | from_base64 (const std::string &str) |
| | Convert from base64.
|
| |
| static void | replace (std::string &str, const std::string &from, const std::string &to) |
| | Replace all occurrences of a substring.
|
| |
| static std::pair< std::string, std::string > | to_string (const std::string &str) |
| | Convert string to string (identity function for compatibility)
|
| |
String conversion utilities.
Definition at line 19 of file convert_string.h.
◆ from_base64()
| static std::pair< std::vector< uint8_t >, std::string > utility_module::convert_string::from_base64 |
( |
const std::string & | str | ) |
|
|
inlinestatic |
Convert from base64.
Definition at line 81 of file convert_string.h.
81 {
82 try {
83 const std::string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
84 std::vector<uint8_t> result;
85 int val = 0;
86 int valb = -8;
87
88 for (char c : str) {
89 if (c == '=') break;
90 size_t pos = chars.find(c);
91 if (pos == std::string::npos) continue;
92
93 val = (val << 6) + pos;
94 valb += 6;
95 if (valb >= 0) {
96 result.push_back((val >> valb) & 0xFF);
97 valb -= 8;
98 }
99 }
100
101 return {result, ""};
102 } catch (const std::exception& e) {
103 return {{}, e.what()};
104 }
105 }
◆ replace()
| static void utility_module::convert_string::replace |
( |
std::string & | str, |
|
|
const std::string & | from, |
|
|
const std::string & | to ) |
|
inlinestatic |
Replace all occurrences of a substring.
Definition at line 110 of file convert_string.h.
110 {
111 if (from.empty()) return;
112
113 size_t start_pos = 0;
114 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
115 str.replace(start_pos, from.length(), to);
116 start_pos += to.length();
117 }
118 }
◆ to_array()
| static std::pair< std::vector< uint8_t >, std::string > utility_module::convert_string::to_array |
( |
const std::string & | str | ) |
|
|
inlinestatic |
Convert string to byte array.
Definition at line 24 of file convert_string.h.
24 {
25 try {
26 std::vector<uint8_t> result(str.begin(), str.end());
27 return {result, ""};
28 } catch (const std::exception& e) {
29 return {{}, e.what()};
30 }
31 }
◆ to_base64()
| static std::pair< std::string, std::string > utility_module::convert_string::to_base64 |
( |
const std::vector< uint8_t > & | data | ) |
|
|
inlinestatic |
Convert data to base64.
Definition at line 48 of file convert_string.h.
48 {
49 try {
50 const std::string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
51 std::string result;
52 int val = 0;
53 int valb = -6;
54
55 for (uint8_t c : data) {
56 val = (val << 8) + c;
57 valb += 8;
58 while (valb >= 0) {
59 result.push_back(chars[(val >> valb) & 0x3F]);
60 valb -= 6;
61 }
62 }
63
64 if (valb > -6) {
65 result.push_back(chars[((val << 8) >> (valb + 8)) & 0x3F]);
66 }
67
68 while (result.size() % 4) {
69 result.push_back('=');
70 }
71
72 return {result, ""};
73 } catch (const std::exception& e) {
74 return {"", e.what()};
75 }
76 }
◆ to_string() [1/2]
| static std::pair< std::string, std::string > utility_module::convert_string::to_string |
( |
const std::string & | str | ) |
|
|
inlinestatic |
Convert string to string (identity function for compatibility)
Definition at line 123 of file convert_string.h.
123 {
124 return {str, ""};
125 }
◆ to_string() [2/2]
| static std::pair< std::string, std::string > utility_module::convert_string::to_string |
( |
const std::vector< uint8_t > & | arr | ) |
|
|
inlinestatic |
Convert byte array to string.
Definition at line 36 of file convert_string.h.
36 {
37 try {
38 std::string result(arr.begin(), arr.end());
39 return {result, ""};
40 } catch (const std::exception& e) {
41 return {"", e.what()};
42 }
43 }
The documentation for this class was generated from the following file: