gleam
gleam copied to clipboard
Support \x escape sequence in strings
I haven't researched this much, but it looks like both Erlang and JavaScript support the "\x"
escape sequence, e.g. "\x1b[36mCYAN STRING\x1b[0m"
.
Currently, Gleam only allows "\e"
, but Node.js doesn't seem to recognize it.
We should investigate further and document findings.
Sounds good, but we'll need to ensure that Erlang and JS have exactly the same behaviour for the new escape code.
I came across \u
while playing with JavaScript as well (via gleam_community/ansi), more specifically \u001b
What does that \u
one do? Is it something that can't be expressed in Gleam today?
What does that
\u
one do? Is it something that can't be expressed in Gleam today?
\u
is used for representing Unicode characters in strings without using the character literal.
In addition, JavaScript allows using Unicode escape sequences in the form of
\u0000
or\u{000000}
in identifiers, which encode the same string value as the actual Unicode characters. For example,你好
and\u4f60\u597d
are the same identifiers:— MDN
It can be useful in situations where the Unicode literal is hard to distinguish visually (invisible character, visually similar to another unicode character):
let zero_width_literal = ""
// Does not compile today:
let zero_width_unicode = "\u200b"
Certain editors like VS Code will also complain when you use certain Unicode literals:
I'm gonna take a whack at this
So what is the design here? Is \x redunant if we have \u?
Hmmm. So for starters I don't thing erlang has \u
syntax (correct me if I'm wrong people who actually speak erlang), so the impl there isn't straightforward.
I think there's probably a solid argument to scrap all of it and favor bitstrings as the way to handle all of this. Then maybe a library could provide a set of named unicode characters to be used with <>
Going to retract my whack for now, as I have other things that require whacking.
How about this syntax?
\u{HH} - unicode (U+00HH)
\u{HHHH} - unicode (U+HHHH)
\u{HHHHHHHH} - unicode (U+HHHHHHHH)
I'm working on it right now. Doesn't seem very hard.
Implemented it the way @lpil proposed.