Skip to content

Commit

Permalink
Merge pull request #1291 from pguyot/w39/add-io_lib-latin1_char_list
Browse files Browse the repository at this point in the history
Add support for `io_lib:latin1_char_list/1`

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
  • Loading branch information
bettio committed Sep 29, 2024
2 parents 1e5e1e9 + d4343a0 commit 1705290
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
- Support for Elixir `List.Chars` protocol
- Support for `gen_server:start_monitor/3,4`
- Support for `code:ensure_loaded/1`
- Support for `io_lib:latin1_char_list/1`

### Changed

Expand Down
16 changes: 15 additions & 1 deletion libs/estdlib/src/io_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
%%-----------------------------------------------------------------------------
-module(io_lib).

-export([format/2]).
-export([format/2, latin1_char_list/1]).

%%-----------------------------------------------------------------------------
%% @param Format format string
Expand All @@ -50,6 +50,20 @@ format(Format, Args) ->
error(badarg)
end.

%%-----------------------------------------------------------------------------
%% @param Term term to test
%% @returns true if Term is a list of latin1 characters, false otherwise.
%% @doc Determine if passed term is a list of ISO-8859-1 characters (0-255).
%% @end
%%-----------------------------------------------------------------------------
-spec latin1_char_list(Term :: any()) -> boolean().
latin1_char_list([H | T]) when is_integer(H) andalso H >= 0 andalso H =< 255 ->
latin1_char_list(T);
latin1_char_list([]) ->
true;
latin1_char_list(_) ->
false.

%%
%% internal operations
%%
Expand Down
16 changes: 16 additions & 0 deletions tests/libs/estdlib/test_io_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
-define(FLT(L), lists:flatten(L)).

test() ->
test_format(),
test_latin1_char_list(),
ok.

test_format() ->
?ASSERT_MATCH(?FLT(io_lib:format("", [])), ""),
?ASSERT_MATCH(?FLT(io_lib:format("foo", [])), "foo"),
?ASSERT_MATCH(?FLT(io_lib:format("foo~n", [])), "foo\n"),
Expand Down Expand Up @@ -240,5 +245,16 @@ test() ->
ok.
test_latin1_char_list() ->
true = io_lib:latin1_char_list([]),
false = io_lib:latin1_char_list(foo),
false = io_lib:latin1_char_list(<<>>),
false = io_lib:latin1_char_list(<<"hello">>),
true = io_lib:latin1_char_list("hello"),
true = io_lib:latin1_char_list("été"),
false = io_lib:latin1_char_list(["hello"]),
false = io_lib:latin1_char_list([$h, $e, $l, $l | $o]),
ok.
id(X) ->
X.

0 comments on commit 1705290

Please sign in to comment.