Skip to content

Commit

Permalink
Add binary_to_atom/1 and binary_to_exiting_atom/1
Browse files Browse the repository at this point in the history
Both functions have been introduced with OTP23 and they default to utf8.

Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Feb 17, 2024
1 parent a00a45d commit 0d75edb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Support for utf8 encoding to `*_to_atom` and `atom_to_*` functions
- `binary_to_atom/1` and `atom_to_binary/1` that default to utf8 (they were introduced with OTP23)

### Fixed

Expand Down
23 changes: 23 additions & 0 deletions libs/estdlib/src/erlang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@
list_to_integer/1,
list_to_tuple/1,
iolist_to_binary/1,
binary_to_atom/1,
binary_to_atom/2,
binary_to_integer/1,
binary_to_list/1,
atom_to_binary/1,
atom_to_binary/2,
atom_to_list/1,
float_to_binary/1,
Expand Down Expand Up @@ -582,6 +584,16 @@ list_to_tuple(_List) ->
iolist_to_binary(_IOList) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param Binary Binary to convert to atom
%% @returns an atom from passed binary
%% @doc Convert a binary to atom, defaults to utf8.
%% @end
%%-----------------------------------------------------------------------------
-spec binary_to_atom(Binary :: binary()) -> atom().
binary_to_atom(_Binary) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param Binary Binary to convert to atom
%% @param Encoding encoding for conversion (any of latin1, utf8 or unicode)
Expand Down Expand Up @@ -613,6 +625,17 @@ binary_to_integer(_Binary) ->
binary_to_list(_Binary) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param Atom Atom to convert
%% @returns a binary with the atom's name
%% @doc Convert an atom to a binary, defaults to utf8.
%% Only latin1 encoding is supported.
%% @end
%%-----------------------------------------------------------------------------
-spec atom_to_binary(Atom :: atom()) -> binary().
atom_to_binary(_Atom) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param Atom Atom to convert
%% @param Encoding Encoding for conversion (any of latin1, utf8 or unicode)
Expand Down
4 changes: 1 addition & 3 deletions src/libAtomVM/nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1942,12 +1942,10 @@ static term nif_erlang_binary_to_existing_atom_2(Context *ctx, int argc, term ar

static term binary_to_atom(Context *ctx, int argc, term argv[], int create_new)
{
UNUSED(argc);

term a_binary = argv[0];
VALIDATE_VALUE(a_binary, term_is_binary);

term encoding = argv[1];
term encoding = (argc == 2) ? argv[1] : UTF8_ATOM;

const char *atom_string = term_binary_data(a_binary);
size_t atom_string_len = term_binary_size(a_binary);
Expand Down
2 changes: 2 additions & 0 deletions src/libAtomVM/nifs.gperf
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ binary:split/2, &binary_split_nif
calendar:system_time_to_universal_time/2, &system_time_to_universal_time_nif
erlang:atom_to_binary/2, &atom_to_binary_nif
erlang:atom_to_list/1, &atom_to_list_nif
erlang:binary_to_atom/1, &binary_to_atom_nif
erlang:binary_to_atom/2, &binary_to_atom_nif
erlang:binary_to_float/1, &binary_to_float_nif
erlang:binary_to_integer/1, &binary_to_integer_nif
erlang:binary_to_list/1, &binary_to_list_nif
erlang:binary_to_existing_atom/1, &binary_to_existing_atom_nif
erlang:binary_to_existing_atom/2, &binary_to_existing_atom_nif
erlang:delete_element/2, &delete_element_nif
erlang:erase/1, &erase_nif
Expand Down

0 comments on commit 0d75edb

Please sign in to comment.