Add toBase64String()
This commit is contained in:
@@ -78,6 +78,35 @@ tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::s
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string toBase64String(std::basic_string_view<unsigned char> buf)
|
||||||
|
{
|
||||||
|
constexpr std::string_view base64_chars{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
|
||||||
|
|
||||||
|
std::string out;
|
||||||
|
|
||||||
|
int val = 0;
|
||||||
|
int valb = -6;
|
||||||
|
for (unsigned char c : buf)
|
||||||
|
{
|
||||||
|
val = (val << 8) + c;
|
||||||
|
valb += 8;
|
||||||
|
|
||||||
|
while (valb >= 0)
|
||||||
|
{
|
||||||
|
out.push_back(base64_chars[(val>>valb)&0x3F]);
|
||||||
|
valb -= 6;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valb>-6)
|
||||||
|
out.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
|
||||||
|
|
||||||
|
while (out.size() % 4)
|
||||||
|
out.push_back('=');
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
bool stringEqualsIgnoreCase(std::string_view a, std::string_view b)
|
bool stringEqualsIgnoreCase(std::string_view a, std::string_view b)
|
||||||
{
|
{
|
||||||
if (a.size() != b.size())
|
if (a.size() != b.size())
|
||||||
|
@@ -44,6 +44,12 @@ inline std::string toHexString(std::string_view str)
|
|||||||
|
|
||||||
tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex);
|
tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex);
|
||||||
|
|
||||||
|
std::string toBase64String(std::basic_string_view<unsigned char> buf);
|
||||||
|
inline std::string toBase64String(std::string_view str)
|
||||||
|
{
|
||||||
|
return toBase64String(std::basic_string_view<unsigned char>{reinterpret_cast<const unsigned char *>(str.data()), str.size()});
|
||||||
|
}
|
||||||
|
|
||||||
bool stringEqualsIgnoreCase(std::string_view a, std::string_view b);
|
bool stringEqualsIgnoreCase(std::string_view a, std::string_view b);
|
||||||
|
|
||||||
//void stringReplaceAll(char search, char replace, std::string &subject);
|
//void stringReplaceAll(char search, char replace, std::string &subject);
|
||||||
|
Reference in New Issue
Block a user