Permalink
Browse files

Rename stringify to to_binary.

  • Loading branch information...
1 parent 4679970 commit d93926841d2dbe31b2e03540e91a5bf0fd1f7aa0 @josevalim josevalim committed Jan 9, 2012
View
5 lib/code.ex
@@ -75,7 +75,7 @@ defmodule Code do
# Finds the file given the relative_to path.
# If the file is found, returns its path in binary, fails otherwise.
defp find_file(file, relative_to) do
- file = to_bin(file)
+ file = to_binary(file)
file = if relative_to do
File.expand_path(file, relative_to)
@@ -95,9 +95,6 @@ defmodule Code do
end
end
- defp to_bin(bin) when is_binary(bin), do: bin
- defp to_bin(list) when is_list(list), do: list_to_binary(list)
-
defp to_list(list) when is_list(list), do: list
defp to_list(bin) when is_binary(bin), do: binary_to_list(bin)
View
6 lib/elixir/macros.ex
@@ -599,11 +599,11 @@ defmodule Elixir::Macros do
#
# ## Examples
#
- # stringify(:foo)
+ # to_binary(:foo)
# #=> "foo"
#
- defmacro stringify(arg) do
- quote { ::String::Inspect.stringify(unquote(arg)) }
+ defmacro to_binary(arg) do
+ quote { ::String::Inspect.to_binary(unquote(arg)) }
end
# Define elem to get Tuple element according to Elixir conventions.
View
4 lib/enum.ex
@@ -285,7 +285,7 @@ defmodule Enum do
# The first item is simply stringified unless ...
defp _join({ h, next }, joiner, nil) do
- _join(next.(), joiner, stringify(h))
+ _join(next.(), joiner, to_binary(h))
end
# The first item is __STOP_ITERATOR__, then we return an empty string;
@@ -295,7 +295,7 @@ defmodule Enum do
# All other items are concatenated to acc, by first adding the joiner;
defp _join({ h, next }, joiner, acc) do
- acc = << acc | :binary, joiner | :binary, stringify(h) | :binary >>
+ acc = << acc | :binary, joiner | :binary, to_binary(h) | :binary >>
_join(next.(), joiner, acc)
end
View
2 lib/io.ex
@@ -1,6 +1,6 @@
defmodule IO do
def print(device // :standard_io, item) do
- Erlang.io.format device, stringify(item), []
+ Erlang.io.format device, to_binary(item), []
end
def puts(device // :standard_io, item) do
View
36 lib/inspect.ex → lib/string/inspect.ex
@@ -1,6 +1,6 @@
-require ::Elixir::Macros, except: [stringify: 1, inspect: 1]
+require ::Elixir::Macros, except: [to_binary: 1, inspect: 1]
-defprotocol String::Inspect, [stringify(thing), inspect(thing)],
+defprotocol String::Inspect, [to_binary(thing), inspect(thing)],
only: [BitString, Tuple, Atom, Number, List]
defimpl String::Inspect, for: Atom do
@@ -21,11 +21,11 @@ defimpl String::Inspect, for: Atom do
end
end
- def stringify(nil) do
+ def to_binary(nil) do
""
end
- def stringify(atom) do
+ def to_binary(atom) do
atom_to_binary(atom, :utf8)
end
@@ -63,11 +63,11 @@ defimpl String::Inspect, for: BitString do
as_bitstring(thing)
end
- def stringify(thing) when is_binary(thing) do
+ def to_binary(thing) when is_binary(thing) do
thing
end
- def stringify(thing) do
+ def to_binary(thing) do
as_bitstring(thing)
end
@@ -85,20 +85,18 @@ defimpl String::Inspect, for: BitString do
end
defimpl String::Inspect, for: List do
- def inspect(thing) do
- stringify(thing)
- end
-
- def stringify([]), do: "[]"
+ def inspect([]), do: "[]"
- def stringify(thing) do
+ def inspect(thing) do
if Erlang.io_lib.printable_list(thing) do
list_to_binary String.escape(thing, ?')
else:
container_join(thing, "[", "]")
end
end
+ def to_binary(thing), do: list_to_binary(thing)
+
## Helpers
def container_join([h], acc, last) do
@@ -116,29 +114,29 @@ defimpl String::Inspect, for: List do
end
defimpl String::Inspect, for: Tuple do
- def inspect(thing), do: stringify(thing)
+ def inspect(thing), do: to_binary(thing)
- def stringify(thing) do
+ def to_binary(thing) do
String::Inspect::List.container_join(tuple_to_list(thing), "{", "}")
end
end
defimpl String::Inspect, for: Number do
- def inspect(thing), do: stringify(thing)
+ def inspect(thing), do: to_binary(thing)
- def stringify(thing) when is_integer(thing) do
+ def to_binary(thing) when is_integer(thing) do
list_to_binary integer_to_list(thing)
end
- def stringify(thing) do
+ def to_binary(thing) do
list_to_binary float_to_list(thing)
end
end
defimpl String::Inspect, for: Any do
- def inspect(thing), do: stringify(thing)
+ def inspect(thing), do: to_binary(thing)
- def stringify(thing) do
+ def to_binary(thing) do
list_to_binary Erlang.io_lib.format('~p', [thing])
end
end
View
2 src/elixir_compiler.erl
@@ -52,5 +52,5 @@ compile_main() ->
"lib/protocol.ex",
"lib/enum.ex",
"lib/record.ex",
- "lib/inspect.ex"
+ "lib/string/inspect.ex"
].
View
2 src/elixir_interpolation.erl
@@ -127,7 +127,7 @@ wrap_interpol(_Line, Form) when is_binary(Form) ->
Form;
wrap_interpol(Line, Form) ->
- { '|', Line, [{stringify, Line, [Form]}, binary]}.
+ { '|', Line, [{to_binary, Line, [Form]}, binary]}.
extract_integers([H|T], Acc) when H >= 48 andalso H =< 57 ->
extract_integers(T, [H|Acc]);
View
2 test/elixir/elixir/cli_test.exs
@@ -11,7 +11,7 @@ defmodule Elixir::CLI::InitTest do
'5\n3\n1\n' = OS.cmd('bin/elixir -f "IO.puts 1" -e "IO.puts 3 + 2" test/elixir/fixtures/init_sample.exs')
expected = '#{inspect ['-o', '1', '2', '3']}\n3\n'
- ^expected = OS.cmd('bin/elixir -e "IO.puts Code.argv" test/elixir/fixtures/init_sample.exs -o 1 2 3')
+ ^expected = OS.cmd('bin/elixir -e "IO.puts inspect(Code.argv)" test/elixir/fixtures/init_sample.exs -o 1 2 3')
end
end
View
56 test/elixir/inspect_test.exs
@@ -1,132 +1,132 @@
Code.require_file "../test_helper", __FILE__
-defmodule Inspect::AtomTest do
+defmodule String::Inspect::AtomTest do
use ExUnit::Case
def test_basic do
":foo" = inspect(:foo)
- "foo" = stringify(:foo)
+ "foo" = to_binary(:foo)
end
def test_empty do
":\"\"" = inspect(:"")
- "" = stringify(:"")
+ "" = to_binary(:"")
end
def test_true_false_nil do
"false" = inspect(false)
"true" = inspect(true)
"nil" = inspect(nil)
- "false" = stringify(false)
- "true" = stringify(true)
- "" = stringify(nil)
+ "false" = to_binary(false)
+ "true" = to_binary(true)
+ "" = to_binary(nil)
end
def test_with_uppercase do
":fOO" = inspect(:fOO)
- "fOO" = stringify(:fOO)
+ "fOO" = to_binary(:fOO)
":FOO" = inspect(:FOO)
- "FOO" = stringify(:FOO)
+ "FOO" = to_binary(:FOO)
end
def test_reference_atom do
"::Foo::Bar" = inspect(::Foo::Bar)
- "::Foo::Bar" = stringify(::Foo::Bar)
+ "::Foo::Bar" = to_binary(::Foo::Bar)
end
def test_impl do
String::Inspect = String::Inspect::Atom.__impl__
end
end
-defmodule Inspect::BitStringTest do
+defmodule String::Inspect::BitStringTest do
use ExUnit::Case
def test_bitstring do
"<<0,1|4>>" = inspect(<<1|12-integer-signed>>)
- "<<0,1|4>>" = stringify(<<1|12-integer-signed>>)
+ "<<0,1|4>>" = to_binary(<<1|12-integer-signed>>)
end
def test_binary do
"\"foo\"" = inspect("foo")
- "foo" = stringify("foo")
+ "foo" = to_binary("foo")
"\"abc\"" = inspect(<<?a, ?b, ?c>>)
- "abc" = stringify(<<?a, ?b, ?c>>)
+ "abc" = to_binary(<<?a, ?b, ?c>>)
end
def test_unprintable do
"<<1>>" = inspect(<<1>>)
end
end
-defmodule Inspect::NumberTest do
+defmodule String::Inspect::NumberTest do
use ExUnit::Case
def test_integer do
"100" = inspect(100)
- "100" = stringify(100)
+ "100" = to_binary(100)
end
def test_float do
"1.00000000000000000000e+00" = inspect(1.0)
- "1.00000000000000000000e+00" = stringify(1.0)
+ "1.00000000000000000000e+00" = to_binary(1.0)
"1.00000000000000000000e+10" = inspect(1.0e10)
- "1.00000000000000000000e+10" = stringify(1.0e10)
+ "1.00000000000000000000e+10" = to_binary(1.0e10)
"1.00000000000000000000e+10" = inspect(1.0e+10)
- "1.00000000000000000000e+10" = stringify(1.0e+10)
+ "1.00000000000000000000e+10" = to_binary(1.0e+10)
end
end
-defmodule Inspect::TupleTest do
+defmodule String::Inspect::TupleTest do
use ExUnit::Case
def test_basic do
"{1, \"b\", 3}" = inspect({ 1, "b", 3 })
- "{1, \"b\", 3}" = stringify({ 1, "b", 3 })
+ "{1, \"b\", 3}" = to_binary({ 1, "b", 3 })
end
def test_record_like do
"{:foo, :bar}" = inspect({ :foo, :bar })
- "{:foo, :bar}" = stringify({ :foo, :bar })
+ "{:foo, :bar}" = to_binary({ :foo, :bar })
end
def test_empty do
"{}" = inspect({})
- "{}" = stringify({})
+ "{}" = to_binary({})
end
end
-defmodule Inspect::ListTest do
+defmodule String::Inspect::ListTest do
use ExUnit::Case
def test_basic do
"[1, \"b\", 3]" = inspect([ 1, "b", 3 ])
- "[1, \"b\", 3]" = stringify([ 1, "b", 3 ])
+ <<1,98,3>> = to_binary([ 1, "b", 3 ])
end
def test_printable do
"'abc'" = inspect('abc')
- "'abc'" = stringify('abc')
+ "abc" = to_binary('abc')
end
def test_empty do
"[]" = inspect([])
- "[]" = stringify([])
+ "" = to_binary([])
end
end
-defmodule Inspect::AnyTest do
+defmodule String::Inspect::AnyTest do
use ExUnit::Case
def test_funs do
bin = inspect(fn(x){ x + 1 })
'#Fun<' ++ _ = binary_to_list(bin)
- bin = stringify(fn(x){ x + 1 })
+ bin = to_binary(fn(x){ x + 1 })
'#Fun<' ++ _ = binary_to_list(bin)
end
end
View
10 test/erlang/string_test.erl
@@ -14,19 +14,19 @@ extract_interpolations_with_escaped_interpolation_test() ->
["f#{o}o"] = extract_interpolations("f\\#{o}o").
extract_interpolations_with_interpolation_test() ->
- ["f", {'|',1,[{stringify,1,[o]}, binary]}, "o"] = extract_interpolations("f#{:o}o").
+ ["f", {'|',1,[{to_binary,1,[o]}, binary]}, "o"] = extract_interpolations("f#{:o}o").
extract_interpolations_with_two_interpolations_test() ->
- ["f", {'|',1,[{stringify,1,[o]}, binary]}, {'|',1,[{stringify,1,[o]}, binary]}, "o"] = extract_interpolations("f#{:o}#{:o}o").
+ ["f", {'|',1,[{to_binary,1,[o]}, binary]}, {'|',1,[{to_binary,1,[o]}, binary]}, "o"] = extract_interpolations("f#{:o}#{:o}o").
extract_interpolations_with_only_two_interpolations_test() ->
- [{'|',1,[{stringify,1,[o]}, binary]}, {'|',1,[{stringify,1,[o]}, binary]}] = extract_interpolations("#{:o}#{:o}").
+ [{'|',1,[{to_binary,1,[o]}, binary]}, {'|',1,[{to_binary,1,[o]}, binary]}] = extract_interpolations("#{:o}#{:o}").
extract_interpolations_with_tuple_inside_interpolation_test() ->
- ["f", {'|',1,[{stringify,1,[{'{}',1,[1]}]}, binary]}, "o"] = extract_interpolations("f#{{1}}o").
+ ["f", {'|',1,[{to_binary,1,[{'{}',1,[1]}]}, binary]}, "o"] = extract_interpolations("f#{{1}}o").
extract_interpolations_with_many_expressions_inside_interpolation_test() ->
- ["f", {'|',2,[{stringify,2,[{block,2,[1,2]}]}, binary]}, "o"] = extract_interpolations("f#{1\n2}o").
+ ["f", {'|',2,[{to_binary,2,[{block,2,[1,2]}]}, binary]}, "o"] = extract_interpolations("f#{1\n2}o").
extract_interpolations_with_string_inside_interpolation_test() ->
["f", <<"foo">>, "o"] = extract_interpolations("f#{\"foo\"}o").

0 comments on commit d939268

Please sign in to comment.