aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src
diff options
context:
space:
mode:
authorJohn Högberg <[email protected]>2018-02-28 09:21:08 +0100
committerJohn Högberg <[email protected]>2018-02-28 09:21:08 +0100
commit0c9475586ca0c057a237287eca50e6ebb6aec0e5 (patch)
tree2c075cdbd0ad012e6a7c14f21957fb399cf23574 /lib/stdlib/src
parent3b62e9dde9153fe8dcc1f1f5bfa97b7cb591c901 (diff)
parent2627297ad239697c7df297b8fde35f9827fa962f (diff)
downloadotp-0c9475586ca0c057a237287eca50e6ebb6aec0e5.tar.gz
otp-0c9475586ca0c057a237287eca50e6ebb6aec0e5.tar.bz2
otp-0c9475586ca0c057a237287eca50e6ebb6aec0e5.zip
Merge branch 'john/erts/binary-bin_to_list-performance-fix/OTP-14741'
* john/erts/binary-bin_to_list-performance-fix/OTP-14741: Replace binary:bin_to_list CIF implementation with binary_to_list
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r--lib/stdlib/src/binary.erl28
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/stdlib/src/binary.erl b/lib/stdlib/src/binary.erl
index 6a64133b45..7d0e42489e 100644
--- a/lib/stdlib/src/binary.erl
+++ b/lib/stdlib/src/binary.erl
@@ -47,23 +47,39 @@ at(_, _) ->
-spec bin_to_list(Subject) -> [byte()] when
Subject :: binary().
-bin_to_list(_) ->
- erlang:nif_error(undef).
+bin_to_list(Subject) ->
+ binary_to_list(Subject).
-spec bin_to_list(Subject, PosLen) -> [byte()] when
Subject :: binary(),
PosLen :: part().
-bin_to_list(_, _) ->
- erlang:nif_error(undef).
+bin_to_list(Subject, {Pos, Len}) ->
+ bin_to_list(Subject, Pos, Len);
+bin_to_list(_Subject, _BadArg) ->
+ erlang:error(badarg).
-spec bin_to_list(Subject, Pos, Len) -> [byte()] when
Subject :: binary(),
Pos :: non_neg_integer(),
Len :: integer().
-bin_to_list(_, _, _) ->
- erlang:nif_error(undef).
+bin_to_list(Subject, Pos, Len) when not is_binary(Subject);
+ not is_integer(Pos);
+ not is_integer(Len) ->
+ %% binary_to_list/3 allows bitstrings as long as the slice fits, and we
+ %% want to badarg when Pos/Len aren't integers instead of raising badarith
+ %% when adjusting args for binary_to_list/3.
+ erlang:error(badarg);
+bin_to_list(Subject, Pos, 0) when Pos >= 0, Pos =< byte_size(Subject) ->
+ %% binary_to_list/3 doesn't handle this case.
+ [];
+bin_to_list(_Subject, _Pos, 0) ->
+ erlang:error(badarg);
+bin_to_list(Subject, Pos, Len) when Len < 0 ->
+ bin_to_list(Subject, Pos + Len, -Len);
+bin_to_list(Subject, Pos, Len) when Len > 0 ->
+ binary_to_list(Subject, Pos + 1, Pos + Len).
-spec compile_pattern(Pattern) -> cp() when
Pattern :: binary() | [binary()].