pw_tokenizer: Base64EncodedBufferSize function
Rename Base64EncodedSize to Base64EncodedBufferSize and have it include
the null terminator. This makes it easier to use directly to size
buffers for Base64 encoding.
Change-Id: Ied24eed67f8d3c5e42a405c98e0b20b30d543536
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/20300
Commit-Queue: Wyatt Hepler <hepler@google.com>
Reviewed-by: Ewout van Bekkum <ewout@google.com>
diff --git a/pw_tokenizer/base64_test.cc b/pw_tokenizer/base64_test.cc
index e751b81..fbbd064 100644
--- a/pw_tokenizer/base64_test.cc
+++ b/pw_tokenizer/base64_test.cc
@@ -74,6 +74,7 @@
TEST_F(PrefixedBase64, Encode_EmptyInput_WritesPrefix) {
EXPECT_EQ(1u, PrefixedBase64Encode(std::span<byte>(), base64_));
EXPECT_EQ('$', base64_[0]);
+ EXPECT_EQ('\0', base64_[1]);
}
TEST_F(PrefixedBase64, Encode_EmptyOutput_WritesNothing) {
@@ -114,7 +115,7 @@
}
TEST_F(PrefixedBase64, EncodeToVector_VectorTooSmall_OnlyNullTerminates) {
- constexpr byte big[Base64EncodedSize(
+ constexpr byte big[Base64EncodedBufferSize(
PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE_BYTES + 1)] = {};
auto buffer = PrefixedBase64Encode(big);
@@ -122,6 +123,19 @@
EXPECT_EQ('\0', buffer[0]);
}
+TEST_F(PrefixedBase64, Base64EncodedBufferSize_Empty_RoomForPrefixAndNull) {
+ EXPECT_EQ(2u, Base64EncodedBufferSize(0));
+}
+
+TEST_F(PrefixedBase64, Base64EncodedBufferSize_PositiveSizes) {
+ for (unsigned i = 1; i <= 3; ++i) {
+ EXPECT_EQ(6u, Base64EncodedBufferSize(i));
+ }
+ for (unsigned i = 4; i <= 6; ++i) {
+ EXPECT_EQ(10u, Base64EncodedBufferSize(i));
+ }
+}
+
TEST_F(PrefixedBase64, Decode) {
for (auto& [binary, base64] : kTestData) {
EXPECT_EQ(binary.size(), PrefixedBase64Decode(base64, binary_));