Open upsuper opened 1 year ago
Unfortunately this isn't really viable as far as I can tell.
str::from_utf8_unchecked
for the user is out.char::encode_utf8
isn't very helpful in this case. While we could conceivably create a mapping of base64 tokens to chars and then use that, it's variable length, which would destroy the performance of encoding.It seems that encode_engine
already does a UTF-8 check on the output.
I wonder if I can build a crate around ASCII chars which exposes a safe API to construct ASCII strings, would you accept a PR making use of that to eliminate the checks?
base64_simd::Base64::encode_as_str is exactly what you want. base64-simd also supports encoding/decoding with an uninitialized buffer, which is relatively difficult for a 100%-safe library.
@Nugine That looks great. I'll give it a try. Thanks!
I suggest that
encode_engine_slice
should have a signature likeApparently the encoded result is always a valid string, but the current API leaves a dilemma for some users:
encode_engine
: provides convenientstr
but allocates,encode_engine_slice
: doesn't allocate but doesn't providestr
unsafe
.If
encode_engine_slice
can return astr
, that dilemma would be resolved. The length can still be derived from.len()
of the returnedstr
.There is also a precedent in the Rust std:
char::encode_utf8
encodes into a buffer and returns astr
.This is a breaking change, though.