constexpr Base64EncodingBufferSize for C++23

This commit is contained in:
orignal
2026-09-20 10:03:32 -04:00
parent b3ebd19293
commit b8a8bcc7e0
+12 -8
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2025, The PurpleI2P Project
* Copyright (c) 2013-2026, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
@@ -14,9 +14,9 @@
#include <string_view>
#include <cstdlib>
namespace i2p
namespace i2p
{
namespace data
namespace data
{
std::string ByteStreamToBase64 (const uint8_t * InBuffer, size_t InCount);
size_t Base64ToByteStream (std::string_view base64Str, uint8_t * OutBuffer, size_t len);
@@ -26,26 +26,30 @@ namespace data
constexpr bool IsBase64 (char ch)
{
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '~';
}
}
size_t Base32ToByteStream (std::string_view base32Str, uint8_t * outBuf, size_t outLen);
std::string ByteStreamToBase32 (const uint8_t * inBuf, size_t len);
std::string ByteStreamToBase32 (const uint8_t * inBuf, size_t len);
constexpr bool IsBase32 (char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= '2' && ch <= '7');
}
}
/**
* Compute the size for a buffer to contain encoded base64 given that the size of the input is input_size bytes
*/
#if __cplusplus >= 202302L // C++23
constexpr size_t Base64EncodingBufferSize(size_t input_size)
#else
inline size_t Base64EncodingBufferSize(size_t input_size)
#endif
{
auto d = std::div (input_size, 3);
if (d.rem) d.quot++;
return 4 * d.quot;
}
}
} // data
} // i2p