Closed nomeata closed 3 years ago
Mildly related to the Quo Vadis Blob issue at https://github.com/dfinity/motoko/issues/966
Text.decodeUTF8 : Blob -> ?Text
Only issue I see with this is we should return a more descriptive error than "did not encode to UTF-8".
I very briefly thought about it, but I am not sure if that’s needed or desirable for the target use cases of Motoko. It’s not like you would do different things depending on the kind of errors. And ?…
works nicely with do ? { … }
.
But it might help with debugging.
Also, soon people will want the decover variants that are more lenient… cans of worms everywhere.
Sounds good to me, except that the names should of course be spelled {en,de}codeUtf8
. ;-)
I kinda expected you to correct the capitalization, and thought you’d surely correct encodeUtf8
to encodeUTF8
, since UTF
is an acronym for Unicode Transformation Format, not like Wasm, which is not an acryonym. Guess I guessed wrong.
See the style guide. :) All-caps acronyms are often avoided with camelCase, because that doesn't work so well.
@hansl asked
The only existing way so far is:
Take the string, iterate over
.chars()
, manually implement utf8-encoding, encode that sequence of bytes as base54, add the dashes according to the textual format, turn that into anactor {}
using theactor (string expr)
form, then usePrincipal.fromActor
, then usePrincipal.toBlob
Probably we can do better. Any objections to adding
Text.encodeUTF8 : Text -> Blob
andText.decodeUTF8 : Blob -> ?Text
to base (via corresponding prims)?