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 InEncoding is to be specified
as latin1.
-
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() type,
but for unicode, parameter Data can
contain integers > 255
(Unicode characters beyond the ISO Latin-1 range), which
makes it invalid as iodata().
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 InEncoding is latin1,
an error occurs whenever an integer > 255 is found
in the lists.
If InEncoding is of a Unicode type,
an error occurs whenever either of the following is found:
-
Incorrect UTF encoding.
If InEncoding is one of the UTF types,
the bytes in any binaries must be valid in that encoding.
Errors can occur for various reasons, including the
following:
-
"Pure" decoding errors
(like the upper bits of the bytes being wrong).
-
The bytes are decoded to a too large number.
-
The bytes are decoded to a code point in the invalid
Unicode range.
-
Encoding is "overlong", meaning that a number
should have been encoded in fewer bytes.
The case of a truncated UTF is handled specially, see the
paragraph about incomplete binaries below.
If InEncoding is latin1, binaries are
always valid as long as they contain whole bytes,
as each byte falls into the valid ISO Latin-1 range.
A special type of error is when no actual invalid integers or
bytes are found, but a trailing binary() consists of too
few bytes to decode the last character. This error can occur
if bytes are read from a file in chunks or if binaries in other
ways are split on non-UTF character boundaries. An incomplete
tuple is then returned instead of the error tuple.
It consists of 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. This means
that a character can be decoded from a range of binaries as long
as the whole range is specified as input without errors occurring.
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.
A badarg exception is thrown for the following 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).