mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add an option to the base64 encoder to omit padding
Will be used by an upcoming JS prototype
This commit is contained in:
parent
94230acf28
commit
41e14e3fc3
Notes:
github-actions[bot]
2024-09-03 15:47:32 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/41e14e3fc3a Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1258 Reviewed-by: https://github.com/awesomekling
3 changed files with 56 additions and 6 deletions
|
@ -54,14 +54,22 @@ ErrorOr<ByteBuffer> decode_base64url(StringView input)
|
|||
return decode_base64_impl(input, simdutf::base64_url);
|
||||
}
|
||||
|
||||
ErrorOr<String> encode_base64(ReadonlyBytes input)
|
||||
ErrorOr<String> encode_base64(ReadonlyBytes input, OmitPadding omit_padding)
|
||||
{
|
||||
return encode_base64_impl(input, simdutf::base64_default);
|
||||
auto options = omit_padding == OmitPadding::Yes
|
||||
? simdutf::base64_default_no_padding
|
||||
: simdutf::base64_default;
|
||||
|
||||
return encode_base64_impl(input, options);
|
||||
}
|
||||
|
||||
ErrorOr<String> encode_base64url(ReadonlyBytes input)
|
||||
ErrorOr<String> encode_base64url(ReadonlyBytes input, OmitPadding omit_padding)
|
||||
{
|
||||
return encode_base64_impl(input, simdutf::base64_url_with_padding);
|
||||
auto options = omit_padding == OmitPadding::Yes
|
||||
? simdutf::base64_url
|
||||
: simdutf::base64_url_with_padding;
|
||||
|
||||
return encode_base64_impl(input, options);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,8 +16,13 @@ namespace AK {
|
|||
ErrorOr<ByteBuffer> decode_base64(StringView);
|
||||
ErrorOr<ByteBuffer> decode_base64url(StringView);
|
||||
|
||||
ErrorOr<String> encode_base64(ReadonlyBytes);
|
||||
ErrorOr<String> encode_base64url(ReadonlyBytes);
|
||||
enum class OmitPadding {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
ErrorOr<String> encode_base64(ReadonlyBytes, OmitPadding = OmitPadding::No);
|
||||
ErrorOr<String> encode_base64url(ReadonlyBytes, OmitPadding = OmitPadding::No);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -73,6 +73,22 @@ TEST_CASE(test_encode)
|
|||
encode_equal("foobar"sv, "Zm9vYmFy"sv);
|
||||
}
|
||||
|
||||
TEST_CASE(test_encode_omit_padding)
|
||||
{
|
||||
auto encode_equal = [&](StringView input, StringView expected) {
|
||||
auto encoded = MUST(encode_base64(input.bytes(), AK::OmitPadding::Yes));
|
||||
EXPECT_EQ(encoded, expected);
|
||||
};
|
||||
|
||||
encode_equal(""sv, ""sv);
|
||||
encode_equal("f"sv, "Zg"sv);
|
||||
encode_equal("fo"sv, "Zm8"sv);
|
||||
encode_equal("foo"sv, "Zm9v"sv);
|
||||
encode_equal("foob"sv, "Zm9vYg"sv);
|
||||
encode_equal("fooba"sv, "Zm9vYmE"sv);
|
||||
encode_equal("foobar"sv, "Zm9vYmFy"sv);
|
||||
}
|
||||
|
||||
TEST_CASE(test_urldecode)
|
||||
{
|
||||
auto decode_equal = [&](StringView input, StringView expected) {
|
||||
|
@ -114,3 +130,24 @@ TEST_CASE(test_urlencode)
|
|||
|
||||
encode_equal("hello!!world"sv, "aGVsbG8hIXdvcmxk"sv);
|
||||
}
|
||||
|
||||
TEST_CASE(test_urlencode_omit_padding)
|
||||
{
|
||||
auto encode_equal = [&](StringView input, StringView expected) {
|
||||
auto encoded = MUST(encode_base64url(input.bytes(), AK::OmitPadding::Yes));
|
||||
EXPECT_EQ(encoded, expected);
|
||||
};
|
||||
|
||||
encode_equal(""sv, ""sv);
|
||||
encode_equal("f"sv, "Zg"sv);
|
||||
encode_equal("fo"sv, "Zm8"sv);
|
||||
encode_equal("foo"sv, "Zm9v"sv);
|
||||
encode_equal("foob"sv, "Zm9vYg"sv);
|
||||
encode_equal("fooba"sv, "Zm9vYmE"sv);
|
||||
encode_equal("foobar"sv, "Zm9vYmFy"sv);
|
||||
|
||||
encode_equal("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."sv, "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEu"sv);
|
||||
encode_equal("hello?world"sv, "aGVsbG8_d29ybGQ"sv);
|
||||
|
||||
encode_equal("hello!!world"sv, "aGVsbG8hIXdvcmxk"sv);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue