gleam icon indicating copy to clipboard operation
gleam copied to clipboard

utf16 serialization segment flag forgotten

Open sdancer opened this issue 1 year ago • 6 comments

  let name = "marimota"
  let padded_name =
    bit_array.slice(
      bit_array.append(<<name:utf16>>, bit_array_copy(<<0, 0>>, 22)),
      0,
      44,
    )

produces erlang

    Name@1 = <<"marimota"/utf8>>,
    Padded_name = begin
        _pipe = gleam_stdlib:bit_array_slice(
            gleam@bit_array:append(<<Name@1/binary>>, binary:copy(<<0, 0>>, 22)),
            0,
            44
        )

added extra context just in case

sdancer avatar Feb 09 '24 20:02 sdancer

Thank you.

lpil avatar Feb 10 '24 10:02 lpil

What should the generated code look like instead?

giacomocavalieri avatar Feb 12 '24 10:02 giacomocavalieri

<<Name@1/binary-utf16>> or similar also erlang doesn't automatically convert them, since on gleam we use strings rather than binaries, a good solution would be the compiler backend to do the conversion by calling unicode:characters_to_binary(Data, InEncoding, OutEncoding) similarly might want to fetch the right implementation on javascript side or at least error

sdancer avatar Feb 12 '24 11:02 sdancer

Thanks for the clarification!

giacomocavalieri avatar Feb 12 '24 13:02 giacomocavalieri

I think we should error in this case, like Erlang does.

lpil avatar Feb 12 '24 19:02 lpil

erlang errors after trying to serialize it from utf8, to preserve that behavior can just emit the utf-16 and let erlang error

sdancer avatar Feb 13 '24 02:02 sdancer