Open mina86 opened 2 years ago
https://docs.rs/base64-simd/latest/base64_simd/struct.Base64.html#method.decode_inplace
fn b64decode(value: String) -> Result<Vec<u8>, base64_simd::Error> {
let base64 = base64_simd::Base64::STANDARD;
let mut vec = value.into_bytes();
let new_length = base64.decode_inplace(&mut vec)?.len();
vec.truncate(new_length);
Ok(vec)
}
I can see the appeal in general of not requiring more memory footprint than necessary. Do you have a specific use case in mind? Does this functionality need to extend to anything beyond a function that decodes in place in order to be useful?
There are two cases:
Function decoding in place would be sufficient.
By the way, I realised that there is one complication. If I understand base58 correctly, decoding happens from the end so the first byte that is known is the last byte in the buffer. This means that decode_inplace would store the data at the end of the buffer. This would complicate the first use case and require the use of Vec::copy_within. Still, that’s probably better than having to allocate a new Vector.
Not sure about base58, but base64 decodes in front to back order in groups of 4 tokens, so that problem at least does not apply.
Ah, sorry, I was reading this Issue thinking this was bs58 repository. Indeed, with base64 the last paragraph is not relevant.
Would be nice to have an in-place decode function, i.e.:
Since output is never longer than input, the decoded value can be saved directly into the input buffer thus possibly saving on having to allocate a new output buffer, e.g.:
This would of course come with a caveat that if error is encountered the data in the buffer is in unspecified state (portion of it could have been overwritten).