unicode(3)
unicode(3) β Erlang Module Definition π¦
β¨ NAME β¨
unicode β Functions for converting and classifying Unicode characters. π
πΈ DESCRIPTION πΈ
Okay bestie, so this module is literally your go-to for converting between different character representations. It converts between ISO Latin-1 characters and Unicode characters, but it can also convert between different Unicode encodings (like UTF-8, UTF-16, and UTF-32). No cap. π
The default Unicode encoding in Erlang binaries is UTF-8, which is also the format in which built-in functions and libraries in OTP expect to find binary Unicode data. In lists, Unicode data is encoded as integers, each integer representing one character and encoded simply as the Unicode code point for the character. So if you see a list of ints, that's just code points, periodt. β¨
Other Unicode encodings than integers representing code points or UTF-8 in binaries are referred to as "external encodings". The ISO Latin-1 encoding is in binaries and lists referred to as latin1-encoding. Keep that in mind. π§
It is recommended to only use external encodings for communication with external entities where this is required. When working inside the Erlang/OTP environment, it is recommended to keep binaries in UTF-8 when representing Unicode characters. ISO Latin-1 encoding is supported both for backward compatibility and for communication with external entities not supporting Unicode character sets. So like, use external encodings at the borders, not inside. π«Ά
Programs should always operate on a normalized form and compare canonical-equivalent Unicode characters as equal. All characters should thus be normalized to one form once on the system borders. One of the following functions can convert characters to their normalized forms characters_to_nfc_list/1, characters_to_nfc_binary/1, characters_to_nfd_list/1 or characters_to_nfd_binary/1. For general text characters_to_nfc_list/1 or characters_to_nfc_binary/1 is preferred, and for identifiers one of the compatibility normalization functions, such as characters_to_nfkc_list/1, is preferred for security reasons. The normalization functions where introduced in OTP 20. Additional information on normalization can be found in the Unicode FAQ. Normalize once, slay always. π
π¦ DATA TYPES π¦
category()
category() ::
{letter, uppercase | lowercase | titlecase | modifier | other} |
{mark, non_spacing | spacing_combining | enclosing} |
{number, decimal | letter | other} |
{separator, space | line | paragraph} |
{other, control | format | surrogate | private | not_assigned} |
{punctuation, connector | dash | open | close | initial | final | other} |
{symbol, math | currency | modifier | other}.
Character category
chardata()
chardata() :: charlist() | unicode_binary().
charlist()
charlist() ::
maybe_improper_list(char() | unicode_binary() | charlist(),
unicode_binary() | []).
encoding()
encoding() ::
latin1 | unicode | utf8 | utf16 |
{utf16, endian()} |
utf32 |
{utf32, endian()}.
endian()
endian() :: big | little.
external_chardata()
external_chardata() ::
external_charlist() | external_unicode_binary().
external_charlist()
external_charlist() ::
maybe_improper_list(char() |
external_unicode_binary() |
external_charlist(),
external_unicode_binary() | []).
external_unicode_binary()
external_unicode_binary() :: binary().
A binary/0 with characters coded in a user-specified Unicode encoding other than UTF-8 (that is, UTF-16 or UTF-32).
latin1_binary()
latin1_binary() :: binary().
A binary/0 with characters coded in ISO Latin-1.
latin1_char()
latin1_char() :: byte().
An integer/0 representing a valid ISO Latin-1 character (0-255).
latin1_chardata()
latin1_chardata() :: latin1_charlist() | latin1_binary().
Equivalent to iodata/0.
latin1_charlist()
latin1_charlist() ::
maybe_improper_list(latin1_char() |
latin1_binary() |
latin1_charlist(),
latin1_binary() | []).
Equivalent to iolist/0.
unicode_binary()
unicode_binary() :: binary().
A binary/0 with characters encoded in the UTF-8 coding standard.
π₯ FUNCTIONS π₯
bom_to_encoding
bom_to_encoding(Bin) -> {Encoding, Length}
when
Bin :: binary(),
Encoding ::
latin1 | utf8 |
{utf16, endian()} |
{utf32, endian()},
Length :: non_neg_integer().
Checks for a UTF Byte Order Mark (BOM) in the beginning of a binary. π
If the supplied binary Bin begins with a valid BOM for either UTF-8, UTF-16, or UTF-32, the function returns the encoding identified along with the BOM length in bytes. β¨
If no BOM is found, the function returns {latin1,0}. π
category
category(char()) -> category().
Returns the Char category. π
1> unicode:category($a).
{letter,lowercase}
2> unicode:category($Γ).
{letter,uppercase}
3> unicode:category($π).
{symbol,other}
4> unicode:category($β¬).
{symbol,currency}
5> unicode:category($[).
{punctuation,open}
characters_to_binary
characters_to_binary(Data, InEncoding, OutEncoding) -> Result
when
Data ::
latin1_chardata() |
chardata() |
external_chardata(),
InEncoding :: encoding(),
OutEncoding :: encoding(),
Result ::
binary() |
{error, binary(), RestData} |
{incomplete, binary(), binary()},
RestData ::
latin1_chardata() |
chardata() |
external_chardata().
Behaves as characters_to_list/2, but produces a binary instead of a Unicode list. π
InEncoding defines how input is to be interpreted if binaries are present in Data. πΈ
OutEncoding defines in what format output is to be generated. β¨
Options:
unicodeβ An alias forutf8, as this is the preferred encoding for Unicode characters in binaries.utf16β An alias for{utf16,big}.utf32β An alias for{utf32,big}.
The atoms big and little denote big- or little-endian encoding. π¦
Errors and exceptions occur as in characters_to_list/2, but the second element in tuple error or incomplete is a binary/0 and not a list/0. π
characters_to_list
characters_to_list(Data, InEncoding) -> Result
when
Data ::
latin1_chardata() |
chardata() |
external_chardata(),
InEncoding :: encoding(),
Result ::
string() |
{error, string(), RestData} |
{incomplete, string(), binary()},
RestData ::
latin1_chardata() |
chardata() |
external_chardata().
Converts a possibly deep list of integers and binaries into a list of integers representing Unicode characters. The binaries in the input can have characters encoded as one of the following: π§
- ISO Latin-1 (0-255, one character per byte). Here, case parameter
InEncodingis to be specified aslatin1. - One of the UTF-encodings, which is specified as parameter
InEncoding.
Note that integers in the list always represent code points regardless of InEncoding passed. If InEncoding latin1 is passed, only code points < 256 are allowed; otherwise, all valid unicode code points are allowed. π₯
If InEncoding is latin1, parameter Data corresponds to the iodata/0 type, but for unicode, parameter Data can contain integers > 255 (Unicode characters beyond the ISO Latin-1 range), which makes it invalid as iodata/0. π
The purpose of the function is mainly to convert combinations of Unicode characters into a pure Unicode string in list representation for further processing. For writing the data to an external entity, the reverse function characters_to_binary/3 comes in handy. π«Ά
Option unicode is an alias for utf8, as this is the preferred encoding for Unicode characters in binaries. utf16 is an alias for {utf16,big} and utf32 is an alias for {utf32,big}. The atoms big and little denote big- or little-endian encoding. β¨
If the data cannot be converted, either because of illegal Unicode/ISO Latin-1 characters in the list, or because of invalid UTF encoding in any binaries, an error tuple is returned. The error tuple contains the tag error, a list representing the characters that could be converted before the error occurred and a representation of the characters including and after the offending integer/bytes. The last part is mostly for debugging, as it still constitutes a possibly deep or mixed list, or both, not necessarily of the same depth as the original data. The error occurs when traversing the list and whatever is left to decode is returned "as is". π
However, if the input Data is a pure binary, the third part of the error tuple is guaranteed to be a binary as well. π
Errors occur for the following reasons:
- Integers out of range.
- If
InEncodingislatin1, an error occurs whenever an integer > 255 is found in the lists. - If
InEncodingis of a Unicode type, an error occurs whenever either of the following is found:- An integer >
16#10FFFF(the maximum Unicode character) - An integer in the range
16#D800to16#DFFF(invalid range reserved for UTF-16 surrogate pairs)
- An integer >
- If
- Incorrect UTF encoding.
okay so if InEncoding is one of the UTF types, then any binaries you pass in have to be valid in that encoding, no cap. π
Errors can pop off for a bunch of reasons, including these lil guys:
- "Pure" decoding errors (like when the upper bits of the bytes are just wrong π).
- The bytes decode to a number that's too large.
- The bytes decode to a code point in the invalid Unicode range.
- Encoding is "overlong", meaning that number should have been encoded in fewer bytes, bestie. Like, why are you using extra bytes? π
The case of a truncated UTF is handled specially β see the paragraph about incomplete binaries below, okay? π«Ά
If InEncoding is latin1, binaries are always valid as long as they contain whole bytes, because every byte falls into the valid ISO Latin-1 range. Slay. β¨
A special type of error is when no actual invalid integers or bytes are found, but a trailing binary/0 consists of too few bytes to decode the last character. This can happen if bytes are read from a file in chunks or if binaries are split on non-UTF character boundaries in other ways. In that case, an incomplete tuple is returned instead of the error tuple. It has the same parts as the error tuple, but the tag is incomplete instead of error, and the last element is always guaranteed to be a binary consisting of the first part of a (so far) valid UTF character. π
If one UTF character is split over two consecutive binaries in the Data, the conversion succeeds. That means a character can be decoded from a range of binaries as long as the whole range is specified as input and no errors happen. Iconic. π
Example:
decode_data(Data) ->
case unicode:characters_to_list(Data,unicode) of
{incomplete,Encoded, Rest} ->
More = get_some_more_data(),
Encoded ++ decode_data([Rest, More]);
{error,Encoded,Rest} ->
handle_error(Encoded,Rest);
List ->
List
end.
However, bit strings that are not whole bytes are not allowed, so a UTF character must be split along 8-bit boundaries to ever be decoded. Periodt. π
A badarg exception is thrown for these cases:
- Any parameters are of the wrong type.
- The list structure is invalid (a number as tail).
- The binaries do not contain whole bytes (bit strings).
characters_to_nfc_binary(chardata()) ->
unicode_binary() |
{error, unicode_binary(), chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of canonical equivalent Composed characters according to the Unicode standard. π
Any binaries in the input must be encoded with utf8 encoding.
The result is an utf8 encoded binary.
Example:
4> unicode:characters_to_nfc_binary([<<"abc..a">>,[778],$a,[776],$o,[776]]).
<<"abc..Γ₯Àâ"/utf8>>
characters_to_nfc_list(chardata()) ->
[char()] | {error, [char()],
chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of canonical equivalent Composed characters according to the Unicode standard. π
Any binaries in the input must be encoded with utf8 encoding.
The result is a list of characters.
Example:
3> unicode:characters_to_nfc_list([<<"abc..a">>,[778],$a,[776],$o,[776]]).
"abc..Γ₯Àâ"
characters_to_nfd_binary(chardata()) ->
unicode_binary() |
{error, unicode_binary(), chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of canonical equivalent Decomposed characters according to the Unicode standard. π¦
Any binaries in the input must be encoded with utf8 encoding.
The result is an utf8 encoded binary.
Example:
2> unicode:characters_to_nfd_binary("abc..Γ₯Àâ").
<<97,98,99,46,46,97,204,138,97,204,136,111,204,136>>
characters_to_nfd_list(chardata()) ->
[char()] | {error, [char()],
chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of canonical equivalent Decomposed characters according to the Unicode standard. πΈ
Any binaries in the input must be encoded with utf8 encoding.
The result is a list of characters.
Example:
1> unicode:characters_to_nfd_list("abc..Γ₯Àâ").
[97,98,99,46,46,97,778,97,776,111,776]
characters_to_nfkc_binary(chardata()) ->
unicode_binary() |
{error, unicode_binary(),
chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of compatibly equivalent Composed characters according to the Unicode standard. β¨
Any binaries in the input must be encoded with utf8 encoding.
The result is an utf8 encoded binary.
Example:
4> unicode:characters_to_nfkc_binary([<<"abc..a">>,[778],$a,[776],$o,[776],[65299,65298]]).
<<"abc..Γ₯Àâ32"/utf8>>
characters_to_nfkc_list(chardata()) ->
[char()] |
{error, [char()], chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of compatibly equivalent Composed characters according to the Unicode standard. π
Any binaries in the input must be encoded with utf8 encoding.
The result is a list of characters.
Example:
3> unicode:characters_to_nfkc_list([<<"abc..a">>,[778],$a,[776],$o,[776],[65299,65298]]).
"abc..Γ₯Àâ32"
characters_to_nfkd_binary(chardata()) ->
unicode_binary() |
{error, unicode_binary(),
chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of compatibly equivalent Decomposed characters according to the Unicode standard. π
Any binaries in the input must be encoded with utf8 encoding.
The result is an utf8 encoded binary.
Example:
2> unicode:characters_to_nfkd_binary(["abc..Γ₯Àâ",[65299,65298]]).
<<97,98,99,46,46,97,204,138,97,204,136,111,204,136,51,50>>
characters_to_nfkd_list(chardata()) ->
[char()] |
{error, [char()], chardata()}.
Converts a possibly deep list of characters and binaries into a Normalized Form of compatibly equivalent Decomposed characters according to the Unicode standard. π«Ά
Any binaries in the input must be encoded with utf8 encoding.
The result is a list of characters.
Example:
1> unicode:characters_to_nfkd_list(["abc..Γ₯Àâ",[65299,65298]]).
[97,98,99,46,46,97,778,97,776,111,776,51,50]
encoding_to_bom(InEncoding) -> Bin
when Bin :: binary(), InEncoding :: encoding().
Creates a UTF Byte Order Mark (BOM) as a binary from the supplied InEncoding. π
The BOM is, if supported at all, expected to be placed first in UTF encoded files or messages.
The function returns <<>> for latin1 encoding, because there is no BOM for ISO Latin-1. π€·βοΈ
Notice that the BOM for UTF-8 is seldom used, and it's really not a byte order mark. There are obviously no byte order issues with UTF-8, so the BOM is only there to differentiate UTF-8 encoding from other UTF formats. Iykyk. π
is_id_continue(char()) -> boolean().
Returns true if Char is an identifier continuation. β¨
Identifier continuation is defined by the ID_Continue property in Unicode Standard Annex #31.
Example:
1> unicode:is_id_continue($a).
true
2> unicode:is_id_continue($_).
true
3> unicode:is_id_continue($-).
false
is_id_start(char()) -> boolean().
Returns true if Char is an identifier start. π
Identifier start is defined by the ID_Start property in Unicode Standard Annex #31.
Example:
1> unicode:is_id_start($a).
true
2> unicode:is_id_start($_).
false
3> unicode:is_id_start($-).
false
is_whitespace(char()) -> boolean().
Returns true if Char is a whitespace. πΈ
Whitespace is defined in Unicode Standard Annex #44.
Example:
1> unicode:is_whitespace($\s).
true
2> unicode:is_whitespace($π).
false
And that's a wrap on the unicode(3) man page, bestie! Big love to Ericsson AB for this one, it's from stdlib 8.0.4, and you're reading the unicode(3) page π β¨. Stay iconic. π«Ά
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms