4 comments

[ 2.9 ms ] story [ 16.2 ms ] thread
well written and insightful. Thank you for sharing your learning.
Doesn't base64 have a simple closed-form formula for the expansion, which can be easily inverted for a single-step calculation of max source length?

Something like

encoded_length = 4 * ceil(length / 3) + len(prefix + suffix)

cut_at = floor((desired_length - len(prefix+suffix) / 4) * 3

Then, you should also try to at least cut on a utf8 code-point boundary and maybe handle the whole unicode grapheme something something.