Permalink
Browse files

Add binary_to_(integer|float) to (integer|float)_to_binary

  • Loading branch information...
1 parent ab4c932 commit 17b5ede88f6f983b10c4e10e3e0d882008f6cb77 @devinus committed Oct 17, 2012
Showing with 116 additions and 8 deletions.
  1. +0 −4 lib/elixir/lib/binary/inspect.ex
  2. +90 −4 lib/elixir/lib/kernel.ex
  3. +26 −0 lib/elixir/test/elixir/kernel_test.exs
@@ -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
@@ -2349,9 +2349,96 @@ defmodule Kernel do
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
@@ -2366,7 +2453,6 @@ defmodule Kernel do
@doc """
Works like `binary_to_atom` but the atom must exist.
- Allowed in guard clauses.
## Examples
@@ -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
@@ -2396,7 +2482,7 @@ defmodule Kernel do
end
@doc """
- Concatenates two binaries. Allowed in guard clauses.
+ Concatenates two binaries.
## Examples
@@ -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

0 comments on commit 17b5ede

Please sign in to comment.