Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
convert_string.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <string>
8#include <vector>
9#include <utility>
10#include <algorithm>
11#include <sstream>
12#include <iomanip>
13
14namespace utility_module {
15
20public:
24 static std::pair<std::vector<uint8_t>, std::string> to_array(const std::string& str) {
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 }
32
36 static std::pair<std::string, std::string> to_string(const std::vector<uint8_t>& arr) {
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 }
44
48 static std::pair<std::string, std::string> to_base64(const std::vector<uint8_t>& data) {
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 }
77
81 static std::pair<std::vector<uint8_t>, std::string> from_base64(const std::string& str) {
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 }
106
110 static void replace(std::string& str, const std::string& from, const std::string& to) {
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 }
119
123 static std::pair<std::string, std::string> to_string(const std::string& str) {
124 return {str, ""};
125 }
126};
127
128} // namespace utility_module
String conversion utilities.
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_base64(const std::vector< uint8_t > &data)
Convert data to base64.
static std::pair< std::string, std::string > to_string(const std::string &str)
Convert string to string (identity function for compatibility)
static std::pair< std::vector< uint8_t >, std::string > from_base64(const std::string &str)
Convert from base64.
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.