Text: add Void::Text base32 encode/decode facade (closes #49)

This commit is contained in:
faumaray committed 2026-12-03 16:33:06 +03:00
1 parent 62c2082fb3
commit f158d7b1cc
2 files changed
+69

No files matched your search

+37
View File
@@ -187,6 +187,13 @@ namespace Void::Encoding::Base32
return Detail::encode_with(data, Detail::alphabet, true);
}
/// Encode bytes to RFC 4648 Base32 with an explicit padding choice.
[[nodiscard]] inline std::string encode(std::span<const std::byte> data,
bool pad)
{
return Detail::encode_with(data, Detail::alphabet, pad);
}
/// Encode an unsigned-byte view without forcing callers to reinterpret
/// their binary buffers as text.
[[nodiscard]] inline std::string encode(std::span<const std::uint8_t> data)
@@ -271,3 +278,33 @@ namespace Void::Encoding::Base32
}
} // namespace Void::Encoding::Base32
namespace Void::Text
{
/// Encode bytes as uppercase RFC 4648 Base32. Padding is emitted by
/// default and can be disabled for unpadded forms.
[[nodiscard]] inline std::string
base32_encode(std::span<const std::byte> data, bool pad = true)
{
return Void::Encoding::Base32::encode(data, pad);
}
/// Decode RFC 4648 Base32 text. Case-insensitive, accepts padded or
/// unpadded input, and rejects invalid characters, impossible lengths,
/// malformed padding and non-zero trailing bits by returning nullopt.
[[nodiscard]] inline std::optional<std::string>
base32_decode(std::string_view text, TextLimits limits = {})
{
return Void::Encoding::Base32::decode(text, limits);
}
/// Decode a byte view as RFC 4648 Base32 text.
[[nodiscard]] inline std::optional<std::string>
base32_decode(std::span<const std::byte> text, TextLimits limits = {})
{
return base32_decode(
std::string_view{reinterpret_cast<const char*>(text.data()),
text.size()},
limits);
}
} // namespace Void::Text