From 5ba8aba19789dd549d047beae056a676cf81a491 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 22 Jul 2020 11:39:07 -0400 Subject: [PATCH] AK: Make encode_base64 take a ByteBuffer and return a String That makes the interface symmetric with decode_base64 and it's what all current callers want (except for one, which is buggy). --- AK/Base64.cpp | 14 ++++++++------ AK/Base64.h | 2 +- AK/Tests/TestBase64.cpp | 4 ++-- Libraries/LibWeb/Bindings/WindowObject.cpp | 4 ++-- Userland/base64.cpp | 4 ++-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 4d238a578d..91e067c2a0 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -25,6 +25,8 @@ */ #include +#include +#include #include #include #include @@ -84,20 +86,20 @@ ByteBuffer decode_base64(const StringView& input) return ByteBuffer::copy(output.data(), output.size()); } -ByteBuffer encode_base64(const StringView& input) +String encode_base64(const ByteBuffer& input) { - Vector output; + StringBuilder output; auto get = [&](size_t offset, bool* need_padding = nullptr) -> u8 { - if (offset >= input.length()) { + if (offset >= input.size()) { if (need_padding) *need_padding = true; return 0; } - return (u8)input[offset]; + return input[offset]; }; - for (size_t i = 0; i < input.length(); i += 3) { + for (size_t i = 0; i < input.size(); i += 3) { bool is_8bit = false; bool is_16bit = false; @@ -121,7 +123,7 @@ ByteBuffer encode_base64(const StringView& input) output.append(out3); } - return ByteBuffer::copy(output.data(), output.size()); + return output.to_string(); } } diff --git a/AK/Base64.h b/AK/Base64.h index d61fcbce7b..dc3b9c4975 100644 --- a/AK/Base64.h +++ b/AK/Base64.h @@ -32,7 +32,7 @@ namespace AK { ByteBuffer decode_base64(const StringView&); -ByteBuffer encode_base64(const StringView&); +String encode_base64(const ByteBuffer&); } diff --git a/AK/Tests/TestBase64.cpp b/AK/Tests/TestBase64.cpp index d495d79092..9e65c03a83 100644 --- a/AK/Tests/TestBase64.cpp +++ b/AK/Tests/TestBase64.cpp @@ -49,8 +49,8 @@ TEST_CASE(test_decode) TEST_CASE(test_encode) { auto encode_equal = [&](const char* input, const char* expected) { - auto encoded = encode_base64(StringView(input)); - EXPECT(String::copy(encoded) == String(expected)); + auto encoded = encode_base64(ByteBuffer::wrap(input, strlen(input))); + EXPECT(encoded == String(expected)); }; encode_equal("", ""); diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp index 9d7d4b2285..bcce2b7e48 100644 --- a/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -267,8 +267,8 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa) if (interpreter.exception()) return {}; // FIXME: This should convert string to a non-UTF-8 byte string first. - auto encoded = encode_base64(StringView(string)); - return JS::js_string(interpreter, String::copy(encoded)); + auto encoded = encode_base64(ByteBuffer::wrap(string.characters(), string.length())); + return JS::js_string(interpreter, move(encoded)); } JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter) diff --git a/Userland/base64.cpp b/Userland/base64.cpp index bbb4b15bc3..0af7a684a4 100644 --- a/Userland/base64.cpp +++ b/Userland/base64.cpp @@ -75,6 +75,6 @@ int main(int argc, char** argv) return 0; } - auto encoded = encode_base64(StringView(buffer)); - printf("%s\n", String::copy(encoded).characters()); + auto encoded = encode_base64(buffer); + printf("%s\n", encoded.characters()); }