Skip to content

Commit

Permalink
Add binary_to_(integer|float) to (integer|float)_to_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Torres committed Oct 17, 2012
1 parent ab4c932 commit 17b5ede
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
4 changes: 0 additions & 4 deletions lib/elixir/lib/binary/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ defimpl Binary.Inspect, for: BitString do
integer_to_binary(h) <> "::size(" <> integer_to_binary(size) <> ")"
end

defp integer_to_binary(integer) do
integer /> integer_to_list /> list_to_binary
end

defp decrement(:infinity), do: :infinity
defp decrement(counter), do: counter - 1
end
Expand Down
94 changes: 90 additions & 4 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2348,10 +2348,97 @@ defmodule Kernel do
end
end

@doc """
Returns a integer whose text representation is `some_binary`.
## Examples
binary_to_integer "123" #=> 123
"""
defmacro binary_to_integer(some_binary) do
quote do
list_to_integer(binary_to_list(unquote(some_binary)))
end
end

@doc """
Returns an integer whose text representation in base `base`
is `some_binary`.
## Examples
binary_to_integer("3FF", 16) #=> 1023
"""
defmacro binary_to_integer(some_binary, base) do
quote do
list_to_integer(binary_to_list(unquote(some_binary)), unquote(base))
end
end

@doc """
Returns a float whose text representation is `some_binary`.
## Examples
binary_to_float "2.2017764e+0" #=> 2.2017764
"""
defmacro binary_to_float(some_binary) do
quote do
list_to_float(binary_to_list(unquote(some_binary)))
end
end

@doc """
Returns a binary which corresponds to the text representation
of `some_integer`.
## Examples
integer_to_binary 123 #=> "123"
"""
defmacro integer_to_binary(some_integer) do
quote do
list_to_binary(integer_to_list(unquote(some_integer)))
end
end

@doc """
Returns a binary which corresponds to the text representation
of `some_integer` in base `base`.
## Examples
integer_to_binary 77 #=> "77"
"""
defmacro integer_to_binary(some_integer, base) do
quote do
list_to_binary(integer_to_list(unquote(some_integer), unquote(base)))
end
end

@doc """
Returns a binary which corresponds to the text representation
of `some_float`.
## Examples
float_to_binary 7.0 #=> "7.00000000000000000000e+00"
"""
defmacro float_to_binary(some_float) do
quote do
list_to_binary(float_to_list(unquote(some_float)))
end
end

@doc """
Returns the atom whose text representation is
`some_binary` in UTF8 encoding.
Allowed in guard clauses.
## Examples
Expand All @@ -2366,7 +2453,6 @@ defmodule Kernel do

@doc """
Works like `binary_to_atom` but the atom must exist.
Allowed in guard clauses.
## Examples
Expand All @@ -2382,7 +2468,7 @@ defmodule Kernel do

@doc """
Returns a binary which corresponds to the text representation
of `some_atom` in UTF8 encoding. Allowed in guard clauses.
of `some_atom` in UTF8 encoding.
## Examples
Expand All @@ -2396,7 +2482,7 @@ defmodule Kernel do
end

@doc """
Concatenates two binaries. Allowed in guard clauses.
Concatenates two binaries.
## Examples
Expand Down
26 changes: 26 additions & 0 deletions lib/elixir/test/elixir/kernel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ defmodule KernelTest do
defmodule Conversions do
use ExUnit.Case, async: true

test :binary_to_integer do
assert binary_to_integer("123") == 123
end

test :binary_to_integer_with_base do
assert binary_to_integer("755", 8) == 493
assert binary_to_integer("3FF", 16) == 1023
end

test :binary_to_float do
assert binary_to_float("2.2017764e+0") == 2.2017764
end

test :integer_to_binary do
assert integer_to_binary(77) == "77"
end

test :integer_to_binary_with_base do
assert integer_to_binary(493, 8) == "755"
assert integer_to_binary(1023, 16) == "3FF"
end

test :float_to_binary do
assert float_to_binary(7.0) == "7.00000000000000000000e+00"
end

test :atom_to_binary_defaults_to_utf8 do
expected = atom_to_binary :some_binary, :utf8
actual = atom_to_binary :some_binary
Expand Down

0 comments on commit 17b5ede

Please sign in to comment.