Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
utility_module::convert_string Namespace Reference

Functions

std::tuple< std::optional< std::string >, std::optional< std::string > > to_base64 (const std::vector< uint8_t > &data)
 
std::tuple< std::vector< uint8_t >, std::optional< std::string > > from_base64 (const std::string &encoded)
 
void replace (std::string &str, const std::string &from, const std::string &to)
 
std::tuple< std::optional< std::vector< uint8_t > >, std::optional< std::string > > to_array (const std::string &str)
 
std::tuple< std::optional< std::string >, std::optional< std::string > > to_string (const std::vector< uint8_t > &data)
 

Function Documentation

◆ from_base64()

std::tuple< std::vector< uint8_t >, std::optional< std::string > > utility_module::convert_string::from_base64 ( const std::string & encoded)

Definition at line 48 of file convert_string_stub.cpp.

48 {
49 // Simple base64 decoding stub
50 std::vector<uint8_t> result;
51
52 std::vector<int> T(256, -1);
53 for (int i = 0; i < 64; i++) {
54 T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
55 }
56
57 int val = 0;
58 int valb = -8;
59 for (char c : encoded) {
60 if (T[c] == -1) break;
61 val = (val << 6) + T[c];
62 valb += 6;
63 if (valb >= 0) {
64 result.push_back((val >> valb) & 0xFF);
65 valb -= 8;
66 }
67 }
68
69 return {result, std::nullopt};
70}

◆ replace()

void utility_module::convert_string::replace ( std::string & str,
const std::string & from,
const std::string & to )

Definition at line 72 of file convert_string_stub.cpp.

72 {
73 size_t start_pos = 0;
74 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
75 str.replace(start_pos, from.length(), to);
76 start_pos += to.length();
77 }
78}

◆ to_array()

std::tuple< std::optional< std::vector< uint8_t > >, std::optional< std::string > > utility_module::convert_string::to_array ( const std::string & str)

Definition at line 80 of file convert_string_stub.cpp.

80 {
81 return {std::vector<uint8_t>(str.begin(), str.end()), std::nullopt};
82}

◆ to_base64()

std::tuple< std::optional< std::string >, std::optional< std::string > > utility_module::convert_string::to_base64 ( const std::vector< uint8_t > & data)

Definition at line 22 of file convert_string_stub.cpp.

22 {
23 // Simple base64 encoding stub
24 static const char* base64_chars =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26 "abcdefghijklmnopqrstuvwxyz"
27 "0123456789+/";
28
29 std::string result;
30 int val = 0;
31 int valb = -6;
32
33 for (uint8_t c : data) {
34 val = (val << 8) + c;
35 valb += 8;
36 while (valb >= 0) {
37 result.push_back(base64_chars[(val >> valb) & 0x3F]);
38 valb -= 6;
39 }
40 }
41
42 if (valb > -6) result.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
43 while (result.size() % 4) result.push_back('=');
44
45 return {result, std::nullopt};
46}

◆ to_string()

std::tuple< std::optional< std::string >, std::optional< std::string > > utility_module::convert_string::to_string ( const std::vector< uint8_t > & data)

Definition at line 84 of file convert_string_stub.cpp.

84 {
85 return {std::string(data.begin(), data.end()), std::nullopt};
86}