Skip to content

Commit 7e9c5c9

Browse files
author
José Valim
committed
Merge pull request #914 from yrashk/binary_to_updates
Update binary_to_ macros to use binary_to_ functions in R16B.
2 parents a114702 + 25bc992 commit 7e9c5c9

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,8 +2440,12 @@ defmodule Kernel do
24402440
24412441
"""
24422442
defmacro binary_to_integer(some_binary) do
2443-
quote do
2444-
list_to_integer(binary_to_list(unquote(some_binary)))
2443+
case :proplists.get_value(:binary_to_integer,
2444+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2445+
2 ->
2446+
quote do: :erlang.binary_to_integer(unquote(some_binary))
2447+
:undefined ->
2448+
quote do: list_to_integer(binary_to_list(unquote(some_binary)))
24452449
end
24462450
end
24472451

@@ -2455,8 +2459,12 @@ defmodule Kernel do
24552459
24562460
"""
24572461
defmacro binary_to_integer(some_binary, base) do
2458-
quote do
2459-
list_to_integer(binary_to_list(unquote(some_binary)), unquote(base))
2462+
case :proplists.get_value(:binary_to_integer,
2463+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2464+
2 ->
2465+
quote do: :erlang.binary_to_integer(unquote(some_binary), unquote(base))
2466+
:undefined ->
2467+
quote do: list_to_integer(binary_to_list(unquote(some_binary)), unquote(base))
24602468
end
24612469
end
24622470

@@ -2469,8 +2477,12 @@ defmodule Kernel do
24692477
24702478
"""
24712479
defmacro binary_to_float(some_binary) do
2472-
quote do
2473-
list_to_float(binary_to_list(unquote(some_binary)))
2480+
case :proplists.get_value(:binary_to_float,
2481+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2482+
1 ->
2483+
quote do: :erlang.binary_to_float(unquote(some_binary))
2484+
:undefined ->
2485+
quote do: list_to_float(binary_to_list(unquote(some_binary)))
24742486
end
24752487
end
24762488

@@ -2484,8 +2496,12 @@ defmodule Kernel do
24842496
24852497
"""
24862498
defmacro integer_to_binary(some_integer) do
2487-
quote do
2488-
list_to_binary(integer_to_list(unquote(some_integer)))
2499+
case :proplists.get_value(:integer_to_binary,
2500+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2501+
2 ->
2502+
quote do: :erlang.integer_to_binary(unquote(some_integer))
2503+
:undefined ->
2504+
quote do: list_to_binary(integer_to_list(unquote(some_integer)))
24892505
end
24902506
end
24912507

@@ -2499,8 +2515,12 @@ defmodule Kernel do
24992515
25002516
"""
25012517
defmacro integer_to_binary(some_integer, base) do
2502-
quote do
2503-
list_to_binary(integer_to_list(unquote(some_integer), unquote(base)))
2518+
case :proplists.get_value(:integer_to_binary,
2519+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2520+
2 ->
2521+
quote do: :erlang.integer_to_binary(unquote(some_integer), unquote(base))
2522+
:undefined ->
2523+
quote do: list_to_binary(integer_to_list(unquote(some_integer), unquote(base)))
25042524
end
25052525
end
25062526

@@ -2514,8 +2534,47 @@ defmodule Kernel do
25142534
25152535
"""
25162536
defmacro float_to_binary(some_float) do
2517-
quote do
2518-
list_to_binary(float_to_list(unquote(some_float)))
2537+
case :proplists.get_value(:float_to_binary,
2538+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2539+
2 ->
2540+
quote do: :erlang.float_to_binary(unquote(some_float))
2541+
:undefined ->
2542+
quote do: list_to_binary(float_to_list(unquote(some_float)))
2543+
end
2544+
end
2545+
2546+
@doc """
2547+
Returns a binary which corresponds to the text representation
2548+
of `some_float`.
2549+
2550+
## Options
2551+
2552+
* `:decimals` — number of decimal points to show
2553+
* `:scientific` — number of decimal points to show, in scientific format
2554+
* `:compact` — If true, use the most compact representation. Ignored with the `scientific` option
2555+
2556+
## Examples
2557+
2558+
float_to_binary 7.1, [decimals: 2, compact: true] #=> "7.1"
2559+
2560+
"""
2561+
defmacro float_to_binary(some_float, options) do
2562+
options = :lists.reverse(:lists.foldl(fn({:compact, false}, acc) -> acc -- [:compact]
2563+
({:compact, true}, acc) -> [:compact|acc]
2564+
(x, acc) -> [x|acc]
2565+
end, [], options))
2566+
case :proplists.get_value(:float_to_binary,
2567+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2568+
2 ->
2569+
quote do: :erlang.float_to_binary(unquote(some_float), unquote(options))
2570+
:undefined ->
2571+
case :proplists.get_value(:float_to_list,
2572+
:proplists.get_value(:exports, :erlang.module_info, [])) do
2573+
2 ->
2574+
quote do: list_to_binary(float_to_list(unquote(some_float), unquote(options)))
2575+
1 ->
2576+
throw(:badarg)
2577+
end
25192578
end
25202579
end
25212580

lib/elixir/test/elixir/kernel_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ defmodule KernelTest do
9393
assert float_to_binary(7.0) == "7.00000000000000000000e+00"
9494
end
9595

96+
case :proplists.get_value(:float_to_binary,
97+
:proplists.get_value(:exports, :erlang.module_info, [])) do
98+
2 ->
99+
# We can only test this where this functionality is available
100+
test :float_to_binary_with_options do
101+
assert float_to_binary(7.1, [decimals: 2]) == "7.10"
102+
assert float_to_binary(7.1, [scientific: 2]) == "7.10e+00"
103+
assert float_to_binary(7.1, [decimals: 2, compact: true]) == "7.1"
104+
assert float_to_binary(7.1, [scientific: 2, compact: true]) == "7.10e+00"
105+
assert float_to_binary(7.1, [decimals: 2, compact: false]) == "7.10"
106+
end
107+
_ ->
108+
:ok
109+
end
96110
test :atom_to_binary_defaults_to_utf8 do
97111
expected = atom_to_binary :some_binary, :utf8
98112
actual = atom_to_binary :some_binary

0 commit comments

Comments
 (0)