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
+32
View File
@@ -289,3 +289,35 @@ Void::Testing::Test base32_respects_text_limits()
Void::Testing::expect(
!decode_crockford("MZXW6YTB", TextLimits{.max_output = 4}).has_value());
}
// ── Void::Text facade ──────────────────────────────────────────────────────
Void::Testing::Test base32_text_facade_matches_encoding_namespace()
{
using Void::Text::base32_decode;
using Void::Text::base32_encode;
const std::array data{std::byte{'f'}, std::byte{'o'}, std::byte{'o'}};
const std::string padded =
base32_encode(std::span<const std::byte>{data});
Void::Testing::expect_eq(padded, std::string{"MZXW6==="});
Void::Testing::expect_eq(
base32_encode(std::span<const std::byte>{data}, false),
std::string{"MZXW6"});
const auto decoded = base32_decode(padded);
Void::Testing::require(decoded.has_value());
Void::Testing::expect_eq(*decoded, std::string{"foo"});
const auto decoded_unpadded = base32_decode(std::string_view{"MZXW6"});
Void::Testing::require(decoded_unpadded.has_value());
Void::Testing::expect_eq(*decoded_unpadded, std::string{"foo"});
const auto bytes = std::as_bytes(std::span{padded.data(), padded.size()});
const auto decoded_bytes = base32_decode(bytes);
Void::Testing::require(decoded_bytes.has_value());
Void::Testing::expect_eq(*decoded_bytes, std::string{"foo"});
Void::Testing::expect(!base32_decode(std::string_view{"MZXW6!"}).has_value());
Void::Testing::expect(!base32_decode(std::string_view{"MZXW6=="}).has_value());
}