|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2023 Paul Guyot <[email protected]> |
| 5 | +% |
| 6 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +% you may not use this file except in compliance with the License. |
| 8 | +% You may obtain a copy of the License at |
| 9 | +% |
| 10 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +% |
| 12 | +% Unless required by applicable law or agreed to in writing, software |
| 13 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +% See the License for the specific language governing permissions and |
| 16 | +% limitations under the License. |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | + |
| 21 | +%%----------------------------------------------------------------------------- |
| 22 | +%% @doc An implementation of the Erlang/OTP unicode interface. |
| 23 | +%% |
| 24 | +%% This module implements a strict subset of the Erlang/OTP unicode |
| 25 | +%% interface. |
| 26 | +%% @end |
| 27 | +%%----------------------------------------------------------------------------- |
| 28 | +-module(unicode). |
| 29 | + |
| 30 | +-export([ |
| 31 | + characters_to_list/1, |
| 32 | + characters_to_list/2, |
| 33 | + characters_to_binary/1, |
| 34 | + characters_to_binary/2, |
| 35 | + characters_to_binary/3 |
| 36 | +]). |
| 37 | + |
| 38 | +%% A UTF-8 encoded binary. |
| 39 | +-type unicode_binary() :: binary(). |
| 40 | + |
| 41 | +%% Latin-1 encoded data |
| 42 | +-type latin1_chardata() :: iodata(). |
| 43 | + |
| 44 | +%% Unicode or UTF-8 encoded data |
| 45 | +-type chardata() :: charlist() | unicode_binary(). |
| 46 | +-type charlist() :: maybe_improper_list( |
| 47 | + char() | unicode_binary() | charlist(), unicode_binary() | [] |
| 48 | +). |
| 49 | + |
| 50 | +-type encoding() :: utf8 | latin1. |
| 51 | + |
| 52 | +-export_type([ |
| 53 | + unicode_binary/0, |
| 54 | + latin1_chardata/0, |
| 55 | + chardata/0, |
| 56 | + charlist/0, |
| 57 | + encoding/0 |
| 58 | +]). |
| 59 | + |
| 60 | +%% @doc Convert UTF-8 data to a list of Unicode characters. |
| 61 | +%% <p>If conversion fails, the function returns a tuple with three elements:</p> |
| 62 | +%% <ul> |
| 63 | +%% <li>First element is <code>error</code> or <code>incomplete</code>. <code>incomplete</code> means the conversion failed because of an incomplete unicode transform at the very end of data.</li> |
| 64 | +%% <li>Second element is what has been converted so far.</li> |
| 65 | +%% <li>Third element is the remaining data to be converted, for debugging purposes. This remaining data can differ with what Erlang/OTP returns.</li> |
| 66 | +%% </ul> |
| 67 | +%% @param Data data to convert to Unicode |
| 68 | +%% @return a list of characters or a tuple if conversion failed. |
| 69 | +-spec characters_to_list(Data :: chardata() | latin1_chardata()) -> |
| 70 | + list() | {error, list(), chardata() | latin1_chardata()} | {incomplete, list(), binary()}. |
| 71 | +characters_to_list(_Data) -> |
| 72 | + erlang:nif_error(undefined). |
| 73 | + |
| 74 | +%% @doc Convert UTF-8 or Latin1 data to a list of Unicode characters. |
| 75 | +%% @see characters_to_list/1 |
| 76 | +%% @param Data data to convert |
| 77 | +%% @param Encoding encoding of data to convert |
| 78 | +%% @return a list of characters or a tuple if conversion failed. |
| 79 | +-spec characters_to_list(Data :: chardata() | latin1_chardata(), Encoding :: encoding()) -> |
| 80 | + list() |
| 81 | + | {error, list(), chardata() | latin1_chardata()} |
| 82 | + | {incomplete, list(), chardata() | latin1_chardata()}. |
| 83 | +characters_to_list(_Data, _Encoding) -> |
| 84 | + erlang:nif_error(undefined). |
| 85 | + |
| 86 | +%% @doc Convert character data to an UTF8 binary |
| 87 | +%% @equiv characters_to_binary(Data, utf8, utf8) |
| 88 | +%% @param Data data to convert to UTF8 |
| 89 | +%% @return an utf8 binary or a tuple if conversion failed. |
| 90 | +-spec characters_to_binary(Data :: chardata() | latin1_chardata()) -> |
| 91 | + unicode_binary() |
| 92 | + | {error, list(), chardata() | latin1_chardata()} |
| 93 | + | {incomplete, unicode_binary(), chardata() | latin1_chardata()}. |
| 94 | +characters_to_binary(_Data) -> |
| 95 | + erlang:nif_error(undefined). |
| 96 | + |
| 97 | +%% @doc Convert character data in a given encoding to an UTF8 binary |
| 98 | +%% @equiv characters_to_binary(Data, InEncoding, utf8) |
| 99 | +%% @param Data data to convert to UTF8 |
| 100 | +%% @param InEncoding encoding of data |
| 101 | +%% @return an utf8 binary or a tuple if conversion failed. |
| 102 | +-spec characters_to_binary(Data :: chardata() | latin1_chardata(), InEncoding :: encoding()) -> |
| 103 | + unicode_binary() |
| 104 | + | {error, list(), chardata() | latin1_chardata()} |
| 105 | + | {incomplete, unicode_binary(), chardata() | latin1_chardata()}. |
| 106 | +characters_to_binary(_Data, _InEncoding) -> |
| 107 | + erlang:nif_error(undefined). |
| 108 | + |
| 109 | +%% @doc Convert character data in a given encoding to a binary in a given encoding. |
| 110 | +%% <p>If conversion fails, the function returns a tuple with three elements:</p> |
| 111 | +%% <ul> |
| 112 | +%% <li>First element is <code>error</code> or <code>incomplete</code>. <code>incomplete</code> means the conversion failed because of an incomplete unicode transform at the very end of data.</li> |
| 113 | +%% <li>Second element is what has been converted so far.</li> |
| 114 | +%% <li>Third element is the remaining data to be converted, for debugging purposes. This remaining data can differ with what Erlang/OTP returns.</li> |
| 115 | +%% </ul> |
| 116 | +%% <p>Also, Erlang/OTP's implementation may error with <code>badarg</code> for parameters |
| 117 | +%% for which this function merely returns an error tuple.</p> |
| 118 | +%% @param Data data to convert to UTF8 |
| 119 | +%% @param InEncoding encoding of input data |
| 120 | +%% @param InEncoding output encoding |
| 121 | +%% @return an encoded binary or a tuple if conversion failed. |
| 122 | +-spec characters_to_binary( |
| 123 | + Data :: chardata() | latin1_chardata(), InEncoding :: encoding(), OutEncoding :: encoding() |
| 124 | +) -> |
| 125 | + unicode_binary() |
| 126 | + | {error, list(), chardata() | latin1_chardata()} |
| 127 | + | {incomplete, unicode_binary(), chardata() | latin1_chardata()}. |
| 128 | +characters_to_binary(_Data, _InEncoding, _OutEncoding) -> |
| 129 | + erlang:nif_error(undefined). |
0 commit comments