Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
convert_string_stub.cpp
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
13#include <string>
14#include <vector>
15#include <algorithm>
16#include <optional>
17#include <tuple>
18
19namespace utility_module {
20namespace convert_string {
21
22std::tuple<std::optional<std::string>, std::optional<std::string>> to_base64(const std::vector<uint8_t>& data) {
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}
47
48std::tuple<std::vector<uint8_t>, std::optional<std::string>> from_base64(const std::string& encoded) {
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}
71
72void replace(std::string& str, const std::string& from, const std::string& to) {
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}
79
80std::tuple<std::optional<std::vector<uint8_t>>, std::optional<std::string>> to_array(const std::string& str) {
81 return {std::vector<uint8_t>(str.begin(), str.end()), std::nullopt};
82}
83
84std::tuple<std::optional<std::string>, std::optional<std::string>> to_string(const std::vector<uint8_t>& data) {
85 return {std::string(data.begin(), data.end()), std::nullopt};
86}
87
88} // namespace convert_string
89} // namespace utility_module
String conversion utilities.
std::tuple< std::vector< uint8_t >, std::optional< std::string > > from_base64(const std::string &encoded)
std::tuple< std::optional< std::string >, std::optional< std::string > > to_string(const std::vector< uint8_t > &data)
void replace(std::string &str, const std::string &from, const std::string &to)
std::tuple< std::optional< std::string >, std::optional< std::string > > to_base64(const std::vector< uint8_t > &data)
std::tuple< std::optional< std::vector< uint8_t > >, std::optional< std::string > > to_array(const std::string &str)