mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:47:37 +00:00
Base64: Pre-allocate size of input and output
Problem: - Output of decode and encode grow as the decode and encode happen. This is inefficient because a large size will require many reallocations. - `const` qualifiers are missing on variables which are not intended to change. Solution: - Since the size of the decoded or encoded message is known prior to starting, calculate the size and set the output to that size immediately. All appends will not incur the reallocation overhead. - Add `const` qualifiers to show intent.
This commit is contained in:
parent
8f535435dc
commit
2983215fb1
3 changed files with 41 additions and 22 deletions
|
@ -35,6 +35,7 @@ TEST_CASE(test_decode)
|
|||
auto decode_equal = [&](const char* input, const char* expected) {
|
||||
auto decoded = decode_base64(StringView(input));
|
||||
EXPECT(String::copy(decoded) == String(expected));
|
||||
EXPECT(StringView(expected).length() <= calculate_base64_decoded_length(StringView(input).bytes()));
|
||||
};
|
||||
|
||||
decode_equal("", "");
|
||||
|
@ -51,6 +52,7 @@ TEST_CASE(test_encode)
|
|||
auto encode_equal = [&](const char* input, const char* expected) {
|
||||
auto encoded = encode_base64({ input, strlen(input) });
|
||||
EXPECT(encoded == String(expected));
|
||||
EXPECT_EQ(StringView(expected).length(), calculate_base64_encoded_length(StringView(input).bytes()));
|
||||
};
|
||||
|
||||
encode_equal("", "");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue