Is it possible to convert Erlang binary UTF-8 string (like <<"HELLO">>
) to lowercase without converting it to list and back?
2 Answers
If you know how to lowercase unicode character and key words here are "without converting it to list and back", then the answer could be:
<< <<(unicode_to_lower(C))/utf8>> || <<C/utf8>> <= <<"HELLO">> >>.
-
2@Kay: Having a working implementation of
unicode_to_lower/1
is implied by the answer. Commented May 25, 2011 at 6:49 -
-
Note: This will only work on a very small subset (the ASCII range)! For some "values" You have to peek at the following bytes (I think up to 6 bytes). en.wikipedia.org/wiki/UTF-8 Commented May 25, 2011 at 16:59
-
3@bsmr It will work not just for ASCII
1> [ C || <<C/utf8>> <= unicode:characters_to_binary("ПРИВЕТ") ]. [1055,1056,1048,1042,1045,1058]
Commented May 25, 2011 at 17:18
string:lowercase in Erlang 20 works with binaries:
1> string:lowercase(<<"HELLO">>).
<<"hello">>