dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
515 stars 97 forks source link

Conversions between Blob and Text #2406

Closed nomeata closed 3 years ago

nomeata commented 3 years ago

@hansl asked

Text to Blob how?

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 an actor {} using the actor (string expr) form, then use Principal.fromActor, then use Principal.toBlob

Probably we can do better. Any objections to adding Text.encodeUTF8 : Text -> Blob and Text.decodeUTF8 : Blob -> ?Text to base (via corresponding prims)?

nomeata commented 3 years ago

Mildly related to the Quo Vadis Blob issue at https://github.com/dfinity/motoko/issues/966

hansl commented 3 years ago

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".

nomeata commented 3 years ago

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.

rossberg commented 3 years ago

Sounds good to me, except that the names should of course be spelled {en,de}codeUtf8. ;-)

nomeata commented 3 years ago

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.

rossberg commented 3 years ago

See the style guide. :) All-caps acronyms are often avoided with camelCase, because that doesn't work so well.