22std::tuple<std::optional<std::string>, std::optional<std::string>>
to_base64(
const std::vector<uint8_t>& data) {
24 static const char* base64_chars =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26 "abcdefghijklmnopqrstuvwxyz"
33 for (uint8_t c : data) {
37 result.push_back(base64_chars[(val >> valb) & 0x3F]);
42 if (valb > -6) result.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
43 while (result.size() % 4) result.push_back(
'=');
45 return {result, std::nullopt};
48std::tuple<std::vector<uint8_t>, std::optional<std::string>>
from_base64(
const std::string& encoded) {
50 std::vector<uint8_t> result;
52 std::vector<int> T(256, -1);
53 for (
int i = 0; i < 64; i++) {
54 T[
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
59 for (
char c : encoded) {
60 if (T[c] == -1)
break;
61 val = (val << 6) + T[c];
64 result.push_back((val >> valb) & 0xFF);
69 return {result, std::nullopt};
72void replace(std::string& str,
const std::string& from,
const std::string& to) {
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();
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};
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};
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)