diff options
author | Sverker Eriksson <sverker@erlang.org> | 2019-08-22 18:52:23 +0200 |
---|---|---|
committer | Sverker Eriksson <sverker@erlang.org> | 2019-08-29 15:16:24 +0200 |
commit | 2921dfa43a67e93b65a79914f1ee0fbf82b220b5 (patch) | |
tree | ff7c52152badcd0af5d20646c6816c2a2d67fcd4 | |
parent | 0b059106055e33241c4d259166bf892253814fbb (diff) | |
download | erlang-2921dfa43a67e93b65a79914f1ee0fbf82b220b5.tar.gz |
erts: Add atom_to_binary/1 and binary_to_atom/1
and binary_to_existing_atom/1
All with default utf8 encoding.
-rw-r--r-- | erts/doc/src/erlang.xml | 36 | ||||
-rw-r--r-- | erts/emulator/test/bif_SUITE.erl | 56 | ||||
-rw-r--r-- | erts/preloaded/ebin/erlang.beam | bin | 100200 -> 100508 bytes | |||
-rw-r--r-- | erts/preloaded/src/erlang.erl | 25 | ||||
-rw-r--r-- | lib/compiler/src/erl_bifs.erl | 2 | ||||
-rw-r--r-- | lib/stdlib/src/erl_internal.erl | 3 |
6 files changed, 108 insertions, 14 deletions
diff --git a/erts/doc/src/erlang.xml b/erts/doc/src/erlang.xml index 2183f75487..376deb2253 100644 --- a/erts/doc/src/erlang.xml +++ b/erts/doc/src/erlang.xml @@ -337,6 +337,16 @@ Z = erlang:adler32_combine(X,Y,iolist_size(Data2)).</code> </func> <func> + <name name="atom_to_binary" arity="1" since="OTP @OTP-15995@"/> + <fsummary>Return the binary representation of an atom.</fsummary> + <desc> + <p> + The same as <seealso marker="#atom_to_binary/2"><c>atom_to_binary</c> + </seealso><c>(<anno>Atom</anno>, utf8)</c>.</p> + </desc> + </func> + + <func> <name name="atom_to_binary" arity="2" since=""/> <fsummary>Return the binary representation of an atom.</fsummary> <desc> @@ -409,14 +419,21 @@ Z = erlang:adler32_combine(X,Y,iolist_size(Data2)).</code> </func> <func> + <name name="binary_to_atom" arity="1" since="OTP @OTP-15995@"/> + <fsummary>Convert from text representation to an atom.</fsummary> + <desc> + <p> + The same as <seealso marker="#binary_to_atom/2"><c>binary_to_atom</c> + </seealso><c>(<anno>Binary</anno>, utf8)</c>.</p> + </desc> + </func> + + <func> <name name="binary_to_atom" arity="2" since=""/> <fsummary>Convert from text representation to an atom.</fsummary> <desc> <p>Returns the atom whose text representation is - <c><anno>Binary</anno></c>. - If <c><anno>Encoding</anno></c> is <c>latin1</c>, no - translation of bytes in the binary is done. - If <c><anno>Encoding</anno></c> + <c><anno>Binary</anno></c>. If <c><anno>Encoding</anno></c> is <c>utf8</c> or <c>unicode</c>, the binary must contain valid UTF-8 sequences.</p> <note> @@ -438,6 +455,17 @@ Z = erlang:adler32_combine(X,Y,iolist_size(Data2)).</code> </func> <func> + <name name="binary_to_existing_atom" arity="1" since="OTP @OTP-15995@"/> + <fsummary>Convert from text representation to an atom.</fsummary> + <desc> + <p> + The same as <seealso marker="#binary_to_existing_atom/2"> + <c>binary_to_existing_atom</c></seealso> + <c>(<anno>Binary</anno>, utf8)</c>.</p> + </desc> + </func> + + <func> <name name="binary_to_existing_atom" arity="2" since=""/> <fsummary>Convert from text representation to an atom.</fsummary> <desc> diff --git a/erts/emulator/test/bif_SUITE.erl b/erts/emulator/test/bif_SUITE.erl index 2a965d2095..cde2bbfa7c 100644 --- a/erts/emulator/test/bif_SUITE.erl +++ b/erts/emulator/test/bif_SUITE.erl @@ -29,8 +29,8 @@ shadow_comments/1,list_to_utf8_atom/1, specs/1,improper_bif_stubs/1,auto_imports/1, t_list_to_existing_atom/1,os_env/1,otp_7526/1, - binary_to_atom/1,binary_to_existing_atom/1, - atom_to_binary/1,min_max/1, erlang_halt/1, + t_binary_to_atom/1,t_binary_to_existing_atom/1, + t_atom_to_binary/1,min_max/1, erlang_halt/1, erl_crash_dump_bytes/1, is_builtin/1, error_stacktrace/1, error_stacktrace_during_call_trace/1, @@ -50,7 +50,7 @@ all() -> specs, improper_bif_stubs, auto_imports, t_list_to_existing_atom, os_env, otp_7526, display, display_string, list_to_utf8_atom, - atom_to_binary, binary_to_atom, binary_to_existing_atom, + t_atom_to_binary, t_binary_to_atom, t_binary_to_existing_atom, erl_crash_dump_bytes, min_max, erlang_halt, is_builtin, error_stacktrace, error_stacktrace_during_call_trace, group_leader_prio, group_leader_prio_dirty, @@ -496,7 +496,7 @@ test_7526(N) -> -define(BADARG(E), {'EXIT',{badarg,_}} = (catch E)). -define(SYS_LIMIT(E), {'EXIT',{system_limit,_}} = (catch E)). -binary_to_atom(Config) when is_list(Config) -> +t_binary_to_atom(Config) when is_list(Config) -> HalfLong = lists:seq(0, 127), HalfLongAtom = list_to_atom(HalfLong), HalfLongBin = list_to_binary(HalfLong), @@ -524,8 +524,10 @@ binary_to_atom(Config) when is_list(Config) -> test_binary_to_atom(<<C/utf8>>, utf8) end], - <<"こんにちは"/utf8>> = - atom_to_binary(test_binary_to_atom(<<"こんにちは"/utf8>>, utf8), utf8), + ExoticBin = <<"こんにちは"/utf8>>, + ExoticAtom = test_binary_to_atom(ExoticBin, utf8), + ExoticBin = atom_to_binary(ExoticAtom, utf8), + ExoticBin = atom_to_binary(ExoticAtom), %% badarg failures. fail_binary_to_atom(atom), @@ -543,6 +545,7 @@ binary_to_atom(Config) when is_list(Config) -> %% Bad UTF8 sequences. ?BADARG(binary_to_atom(id(<<255>>), utf8)), + ?BADARG(binary_to_atom(id(<<255>>))), ?BADARG(binary_to_atom(id(<<255,0>>), utf8)), ?BADARG(binary_to_atom(id(<<16#C0,16#80>>), utf8)), %Overlong 0. <<B:1/binary, _/binary>> = id(<<194, 163>>), %Truncated character ERL-474 @@ -550,6 +553,7 @@ binary_to_atom(Config) when is_list(Config) -> %% system_limit failures. ?SYS_LIMIT(binary_to_atom(id(<<0:512/unit:8,255>>), utf8)), + ?SYS_LIMIT(binary_to_atom(id(<<0:512/unit:8,255>>))), ?SYS_LIMIT(binary_to_atom(id(<<0:512/unit:8,255,0>>), utf8)), ?SYS_LIMIT(binary_to_atom(<<0:256/unit:8>>, latin1)), ?SYS_LIMIT(binary_to_atom(<<0:257/unit:8>>, latin1)), @@ -562,6 +566,14 @@ binary_to_atom(Config) when is_list(Config) -> test_binary_to_atom(Bin0, Encoding) -> Res = binary_to_atom(Bin0, Encoding), Res = binary_to_existing_atom(Bin0, Encoding), + if + Encoding =:= utf8; + Encoding =:= unicode -> + Res = binary_to_atom(Bin0), + Res = binary_to_existing_atom(Bin0); + true -> + ok + end, Bin1 = id(<<7:3,Bin0/binary,32:5>>), Sz = byte_size(Bin0), <<_:3,UnalignedBin:Sz/binary,_:5>> = Bin1, @@ -581,6 +593,12 @@ fail_binary_to_atom(Bin) -> ok end, try + binary_to_atom(Bin) + catch + error:badarg -> + ok + end, + try binary_to_existing_atom(Bin, latin1) catch error:badarg -> @@ -591,10 +609,16 @@ fail_binary_to_atom(Bin) -> catch error:badarg -> ok + end, + try + binary_to_existing_atom(Bin) + catch + error:badarg -> + ok end. -binary_to_existing_atom(Config) when is_list(Config) -> +t_binary_to_existing_atom(Config) when is_list(Config) -> UnlikelyBin = <<"ou0897979655678dsfj923874390867er869fds973qerueoru">>, try binary_to_existing_atom(UnlikelyBin, latin1), @@ -609,6 +633,12 @@ binary_to_existing_atom(Config) when is_list(Config) -> catch error:badarg -> ok end, + try + binary_to_existing_atom(UnlikelyBin), + ct:fail(atom_exists) + catch + error:badarg -> ok + end, UnlikelyAtom = binary_to_atom(id(UnlikelyBin), latin1), UnlikelyAtom = binary_to_existing_atom(UnlikelyBin, latin1), @@ -625,7 +655,7 @@ binary_to_existing_atom(Config) when is_list(Config) -> ok. -atom_to_binary(Config) when is_list(Config) -> +t_atom_to_binary(Config) when is_list(Config) -> HalfLong = lists:seq(0, 127), HalfLongAtom = list_to_atom(HalfLong), HalfLongBin = list_to_binary(HalfLong), @@ -641,12 +671,15 @@ atom_to_binary(Config) when is_list(Config) -> LongBin = atom_to_binary(LongAtom, latin1), %% utf8. + <<>> = atom_to_binary(''), <<>> = atom_to_binary('', utf8), <<>> = atom_to_binary('', unicode), <<127>> = atom_to_binary('\177', utf8), <<"abcdef">> = atom_to_binary(abcdef, utf8), HalfLongBin = atom_to_binary(HalfLongAtom, utf8), + HalfLongBin = atom_to_binary(HalfLongAtom), LongAtomBin = atom_to_binary(LongAtom, utf8), + LongAtomBin = atom_to_binary(LongAtom), verify_long_atom_bin(LongAtomBin, 0), %% Failing cases. @@ -678,8 +711,15 @@ fail_atom_to_binary(Term) -> catch error:badarg -> ok + end, + try + atom_to_binary(Term) + catch + error:badarg -> + ok end. + min_max(Config) when is_list(Config) -> a = erlang:min(id(a), a), a = erlang:min(id(a), b), diff --git a/erts/preloaded/ebin/erlang.beam b/erts/preloaded/ebin/erlang.beam Binary files differindex 866f9df79f..78a9a4eef2 100644 --- a/erts/preloaded/ebin/erlang.beam +++ b/erts/preloaded/ebin/erlang.beam diff --git a/erts/preloaded/src/erlang.erl b/erts/preloaded/src/erlang.erl index 12c256169c..f1a1de4ab2 100644 --- a/erts/preloaded/src/erlang.erl +++ b/erts/preloaded/src/erlang.erl @@ -109,8 +109,11 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -export([adler32/1, adler32/2, adler32_combine/3, append_element/2]). --export([atom_to_binary/2, atom_to_list/1, binary_part/2, binary_part/3]). --export([binary_to_atom/2, binary_to_existing_atom/2, binary_to_float/1]). +-export([atom_to_binary/1, atom_to_binary/2]). +-export([atom_to_list/1, binary_part/2, binary_part/3]). +-export([binary_to_atom/1, binary_to_atom/2]). +-export([binary_to_existing_atom/1, binary_to_existing_atom/2]). +-export([binary_to_float/1]). -export([binary_to_integer/1,binary_to_integer/2]). -export([binary_to_list/1]). -export([binary_to_list/3, binary_to_term/1, binary_to_term/2]). @@ -341,6 +344,12 @@ adler32_combine(_FirstAdler, _SecondAdler, _SecondSize) -> append_element(_Tuple1, _Term) -> erlang:nif_error(undefined). +%% atom_to_binary/1 +-spec atom_to_binary(Atom) -> binary() when + Atom :: atom(). +atom_to_binary(Atom) -> + erlang:atom_to_binary(Atom, utf8). + %% atom_to_binary/2 -spec atom_to_binary(Atom, Encoding) -> binary() when Atom :: atom(), @@ -371,6 +380,12 @@ binary_part(_Subject, _PosLen) -> binary_part(_Subject, _Start, _Length) -> erlang:nif_error(undefined). +%% binary_to_atom/1 +-spec binary_to_atom(Binary) -> atom() when + Binary :: binary(). +binary_to_atom(Binary) -> + erlang:binary_to_atom(Binary, utf8). + %% binary_to_atom/2 -spec binary_to_atom(Binary, Encoding) -> atom() when Binary :: binary(), @@ -378,6 +393,12 @@ binary_part(_Subject, _Start, _Length) -> binary_to_atom(_Binary, _Encoding) -> erlang:nif_error(undefined). +%% binary_to_existing_atom/1 +-spec binary_to_existing_atom(Binary) -> atom() when + Binary :: binary(). +binary_to_existing_atom(Binary) -> + erlang:binary_to_existing_atom(Binary, utf8). + %% binary_to_existing_atom/2 -spec binary_to_existing_atom(Binary, Encoding) -> atom() when Binary :: binary(), diff --git a/lib/compiler/src/erl_bifs.erl b/lib/compiler/src/erl_bifs.erl index 94a5dfe012..1b60995de9 100644 --- a/lib/compiler/src/erl_bifs.erl +++ b/lib/compiler/src/erl_bifs.erl @@ -80,10 +80,12 @@ is_pure(erlang, 'or', 2) -> true; is_pure(erlang, 'rem', 2) -> true; is_pure(erlang, 'xor', 2) -> true; is_pure(erlang, abs, 1) -> true; +is_pure(erlang, atom_to_binary, 1) -> true; is_pure(erlang, atom_to_binary, 2) -> true; is_pure(erlang, atom_to_list, 1) -> true; is_pure(erlang, binary_part, 2) -> true; is_pure(erlang, binary_part, 3) -> true; +is_pure(erlang, binary_to_atom, 1) -> true; is_pure(erlang, binary_to_atom, 2) -> true; is_pure(erlang, binary_to_float, 1) -> true; is_pure(erlang, binary_to_integer, 1) -> true; diff --git a/lib/stdlib/src/erl_internal.erl b/lib/stdlib/src/erl_internal.erl index 939abaff00..e1f8fe151e 100644 --- a/lib/stdlib/src/erl_internal.erl +++ b/lib/stdlib/src/erl_internal.erl @@ -245,11 +245,14 @@ bif(M, F, A) when is_atom(M), is_atom(F), is_integer(A) -> false. bif(abs, 1) -> true; bif(apply, 2) -> true; bif(apply, 3) -> true; +bif(atom_to_binary, 1) -> true; bif(atom_to_binary, 2) -> true; bif(atom_to_list, 1) -> true; bif(binary_part, 2) -> true; bif(binary_part, 3) -> true; +bif(binary_to_atom, 1) -> true; bif(binary_to_atom, 2) -> true; +bif(binary_to_existing_atom, 1) -> true; bif(binary_to_existing_atom, 2) -> true; bif(binary_to_integer, 1) -> true; bif(binary_to_integer, 2) -> true; |