Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add partial base64 in-place encoding support #2166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Mis1eader-dev
Copy link
Member

@Mis1eader-dev Mis1eader-dev commented Sep 21, 2024

This PR allows for encoding a sequence of bytes, which does not exceed 12 bytes, in-place.
Added the declaration of another base64 encoding function that will do in-place encoding but with dynamic allocation.
Tried as much as I could to stick to the old implementation, had to make some structured changes to make it work for in-place encoding.

Replaced .resize() calls as they were allocating more than they needed in terms of capacity. Use this sample code to check out this change:

int main()
{
    constexpr auto len = 20;
    string s1(len, '0');
    string s2;
    s2.resize(len, '0');
    cout << "S1: " << s1 << " | Len: " << s1.size() << " | Cap: " << s1.capacity() << '\n'; // Cap: 20
    cout << "S2: " << s2 << " | Len: " << s2.size() << " | Cap: " << s2.capacity() << '\n'; // Cap: 30
}

@Mis1eader-dev
Copy link
Member Author

Wait I forgot to to test it on longer payloads, may have to split this implementation into 2 functions.

@Mis1eader-dev Mis1eader-dev marked this pull request as draft September 21, 2024 09:18
@Mis1eader-dev
Copy link
Member Author

Surprisingly it works for longer inputs too, I don't know how.
Welp, if it works it works. ¯\(ツ)

@Mis1eader-dev Mis1eader-dev marked this pull request as ready for review September 21, 2024 09:23
@Mis1eader-dev
Copy link
Member Author

One small thing to keep note of, in one of the inner loops where it does a copy ahead is not safe, it could read up to 3 bytes outside of the input buffer, but they will not be used after the outer loop is finished executing.
If you need me to make it tightly safe within bounds, I'll add a few checks in there to make sure it never reads outside of the initial inLen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant