mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-26 15:56:18 +02:00
Merge pull request #6366 from danpls/base64-reserve
libutil: Reserve memory when en/decoding base64
This commit is contained in:
commit
c0ad86f681
1 changed files with 4 additions and 0 deletions
|
@ -1471,6 +1471,7 @@ constexpr char base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
|
||||||
std::string base64Encode(std::string_view s)
|
std::string base64Encode(std::string_view s)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
|
res.reserve((s.size() + 2) / 3 * 4);
|
||||||
int data = 0, nbits = 0;
|
int data = 0, nbits = 0;
|
||||||
|
|
||||||
for (char c : s) {
|
for (char c : s) {
|
||||||
|
@ -1502,6 +1503,9 @@ std::string base64Decode(std::string_view s)
|
||||||
}();
|
}();
|
||||||
|
|
||||||
std::string res;
|
std::string res;
|
||||||
|
// Some sequences are missing the padding consisting of up to two '='.
|
||||||
|
// vvv
|
||||||
|
res.reserve((s.size() + 2) / 4 * 3);
|
||||||
unsigned int d = 0, bits = 0;
|
unsigned int d = 0, bits = 0;
|
||||||
|
|
||||||
for (char c : s) {
|
for (char c : s) {
|
||||||
|
|
Loading…
Reference in a new issue