1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:07:45 +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:
Lenny Maiorani 2020-10-13 10:48:48 -04:00 committed by Andreas Kling
parent 8f535435dc
commit 2983215fb1
3 changed files with 41 additions and 22 deletions

View file

@ -26,11 +26,17 @@
#pragma once
#include <AK/Forward.h>
#include <AK/ByteBuffer.h>
#include <AK/Span.h>
#include <AK/String.h>
#include <AK/StringView.h>
namespace AK {
size_t calculate_base64_decoded_length(const StringView&);
size_t calculate_base64_encoded_length(ReadonlyBytes);
ByteBuffer decode_base64(const StringView&);
String encode_base64(ReadonlyBytes);