diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index 30ceb165d03..95fc4c52d93 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -3659,15 +3659,15 @@ defmodule Kernel do case is_binary(string) do true -> case mod do - ?b -> String.split(string) - ?a -> lc p inlist String.split(string), do: binary_to_atom(p) - ?c -> lc p inlist String.split(string), do: String.to_char_list!(p) + ?b -> lc p inlist String.split(string), p != "", do: p + ?a -> lc p inlist String.split(string), p != "", do: binary_to_atom(p) + ?c -> lc p inlist String.split(string), p != "", do: String.to_char_list!(p) end false -> case mod do - ?b -> quote do: String.split(unquote(string)) - ?a -> quote do: lc(p inlist String.split(unquote(string)), do: binary_to_atom(p)) - ?c -> quote do: lc(p inlist String.split(unquote(string)), do: String.to_char_list!(p)) + ?b -> quote do: lc(p inlist String.split(unquote(string)), p != "", do: p) + ?a -> quote do: lc(p inlist String.split(unquote(string)), p != "", do: binary_to_atom(p)) + ?c -> quote do: lc(p inlist String.split(unquote(string)), p != "", do: String.to_char_list!(p)) end end end diff --git a/lib/elixir/lib/string.ex b/lib/elixir/lib/string.ex index 93c8b2f9421..209977a255e 100644 --- a/lib/elixir/lib/string.ex +++ b/lib/elixir/lib/string.ex @@ -140,7 +140,7 @@ defmodule String do @doc """ Splits a string on substrings at each Unicode whitespace - occurrence with leading and trailing whitespace ignored. + occurrence. ## Examples @@ -149,7 +149,7 @@ defmodule String do iex> String.split("foo" <> <<194, 133>> <> "bar") ["foo", "bar"] iex> String.split(" foo bar ") - ["foo", "bar"] + ["", "foo", "bar", ""] """ @spec split(t) :: [t] diff --git a/lib/elixir/priv/unicode.ex b/lib/elixir/priv/unicode.ex index f6849e63330..fc2ef35fc2a 100644 --- a/lib/elixir/priv/unicode.ex +++ b/lib/elixir/priv/unicode.ex @@ -145,11 +145,7 @@ defmodule String.Unicode do lc codepoint inlist whitespace do defp do_split(unquote(codepoint) <> rest, buffer, acc) do - if buffer != "" do - do_split(rest, "", [buffer | acc]) - else - do_split(rest, buffer, acc) - end + do_split(rest, "", [buffer | acc]) end end @@ -158,11 +154,7 @@ defmodule String.Unicode do end defp do_split(<<>>, buffer, acc) do - if buffer != "" do - [buffer | acc] - else - acc - end + [buffer | acc] end # Graphemes diff --git a/lib/elixir/test/elixir/string_test.exs b/lib/elixir/test/elixir/string_test.exs index f642ae61d8a..b6e0c3daec4 100644 --- a/lib/elixir/test/elixir/string_test.exs +++ b/lib/elixir/test/elixir/string_test.exs @@ -18,10 +18,10 @@ defmodule StringTest do test :split do assert String.split("") == [""] assert String.split("foo bar") == ["foo", "bar"] - assert String.split(" foo bar") == ["foo", "bar"] - assert String.split("foo bar ") == ["foo", "bar"] - assert String.split(" foo bar ") == ["foo", "bar"] - assert String.split("foo\t\n\v\f\r\sbar\n") == ["foo", "bar"] + assert String.split(" foo bar") == ["", "foo", "bar"] + assert String.split("foo bar ") == ["foo", "bar", ""] + assert String.split(" foo bar ") == ["", "foo", "bar", ""] + assert String.split("foo\t\n\v\f\r\sbar\n") == ["foo", "", "", "", "", "", "bar", ""] assert String.split("foo" <> <<31>> <> "bar") == ["foo", "bar"] assert String.split("foo" <> <<194, 133>> <> "bar") == ["foo", "bar"] diff --git a/lib/iex/test/iex/helpers_test.exs b/lib/iex/test/iex/helpers_test.exs index c86d451668a..6c4659929fa 100644 --- a/lib/iex/test/iex/helpers_test.exs +++ b/lib/iex/test/iex/helpers_test.exs @@ -133,7 +133,7 @@ defmodule IEx.HelpersTest do assert ["ebin", "lib", "mix.exs", "test"] = capture_io(fn -> ls end) |> String.split - |> Enum.map(String.strip(&1)) + |> Enum.filter(&(&1 != "")) |> Enum.sort assert capture_io(fn -> ls "~" end) == capture_io(fn -> ls System.user_home end) end