From f158d7b1ccc6dc32315a18e73b0d5a048c0fe038 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Thu, 3 Dec 2026 16:33:06 +0300 Subject: [PATCH] Text: add Void::Text base32 encode/decode facade (closes #49) --- include/Void/Text/Base32.hpp | 37 ++++++++++++++++++++++++++++++++ tests/standalone/Text/Base32.cpp | 32 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/include/Void/Text/Base32.hpp b/include/Void/Text/Base32.hpp index b3f976b..a79b176 100644 --- a/include/Void/Text/Base32.hpp +++ b/include/Void/Text/Base32.hpp @@ -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 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 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 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 + 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 + base32_decode(std::span text, TextLimits limits = {}) + { + return base32_decode( + std::string_view{reinterpret_cast(text.data()), + text.size()}, + limits); + } +} // namespace Void::Text diff --git a/tests/standalone/Text/Base32.cpp b/tests/standalone/Text/Base32.cpp index edd5349..4efea60 100644 --- a/tests/standalone/Text/Base32.cpp +++ b/tests/standalone/Text/Base32.cpp @@ -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{data}); + Void::Testing::expect_eq(padded, std::string{"MZXW6==="}); + Void::Testing::expect_eq( + base32_encode(std::span{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()); +}