Skip to content

Commit 891564f

Browse files
committed
Keep empty strings when splitting
To stay consistent with both `String.split/2` and `:binary.split` we need to keep around empty strings that are accumulated while splitting. In the future we could support the `:trim` option to consistently remove these in call calls to `String.split`. Fixes #1636
1 parent cae9b05 commit 891564f

File tree

5 files changed

+15
-23
lines changed

5 files changed

+15
-23
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,15 +3659,15 @@ defmodule Kernel do
36593659
case is_binary(string) do
36603660
true ->
36613661
case mod do
3662-
?b -> String.split(string)
3663-
?a -> lc p inlist String.split(string), do: binary_to_atom(p)
3664-
?c -> lc p inlist String.split(string), do: String.to_char_list!(p)
3662+
?b -> lc p inlist String.split(string), p != "", do: p
3663+
?a -> lc p inlist String.split(string), p != "", do: binary_to_atom(p)
3664+
?c -> lc p inlist String.split(string), p != "", do: String.to_char_list!(p)
36653665
end
36663666
false ->
36673667
case mod do
3668-
?b -> quote do: String.split(unquote(string))
3669-
?a -> quote do: lc(p inlist String.split(unquote(string)), do: binary_to_atom(p))
3670-
?c -> quote do: lc(p inlist String.split(unquote(string)), do: String.to_char_list!(p))
3668+
?b -> quote do: lc(p inlist String.split(unquote(string)), p != "", do: p)
3669+
?a -> quote do: lc(p inlist String.split(unquote(string)), p != "", do: binary_to_atom(p))
3670+
?c -> quote do: lc(p inlist String.split(unquote(string)), p != "", do: String.to_char_list!(p))
36713671
end
36723672
end
36733673
end

lib/elixir/lib/string.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ defmodule String do
140140

141141
@doc """
142142
Splits a string on substrings at each Unicode whitespace
143-
occurrence with leading and trailing whitespace ignored.
143+
occurrence.
144144
145145
## Examples
146146
@@ -149,7 +149,7 @@ defmodule String do
149149
iex> String.split("foo" <> <<194, 133>> <> "bar")
150150
["foo", "bar"]
151151
iex> String.split(" foo bar ")
152-
["foo", "bar"]
152+
["", "foo", "bar", ""]
153153
154154
"""
155155
@spec split(t) :: [t]

lib/elixir/priv/unicode.ex

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ defmodule String.Unicode do
145145

146146
lc codepoint inlist whitespace do
147147
defp do_split(unquote(codepoint) <> rest, buffer, acc) do
148-
if buffer != "" do
149-
do_split(rest, "", [buffer | acc])
150-
else
151-
do_split(rest, buffer, acc)
152-
end
148+
do_split(rest, "", [buffer | acc])
153149
end
154150
end
155151

@@ -158,11 +154,7 @@ defmodule String.Unicode do
158154
end
159155

160156
defp do_split(<<>>, buffer, acc) do
161-
if buffer != "" do
162-
[buffer | acc]
163-
else
164-
acc
165-
end
157+
[buffer | acc]
166158
end
167159

168160
# Graphemes

lib/elixir/test/elixir/string_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ defmodule StringTest do
1818
test :split do
1919
assert String.split("") == [""]
2020
assert String.split("foo bar") == ["foo", "bar"]
21-
assert String.split(" foo bar") == ["foo", "bar"]
22-
assert String.split("foo bar ") == ["foo", "bar"]
23-
assert String.split(" foo bar ") == ["foo", "bar"]
24-
assert String.split("foo\t\n\v\f\r\sbar\n") == ["foo", "bar"]
21+
assert String.split(" foo bar") == ["", "foo", "bar"]
22+
assert String.split("foo bar ") == ["foo", "bar", ""]
23+
assert String.split(" foo bar ") == ["", "foo", "bar", ""]
24+
assert String.split("foo\t\n\v\f\r\sbar\n") == ["foo", "", "", "", "", "", "bar", ""]
2525
assert String.split("foo" <> <<31>> <> "bar") == ["foo", "bar"]
2626
assert String.split("foo" <> <<194, 133>> <> "bar") == ["foo", "bar"]
2727

lib/iex/test/iex/helpers_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ defmodule IEx.HelpersTest do
133133
assert ["ebin", "lib", "mix.exs", "test"]
134134
= capture_io(fn -> ls end)
135135
|> String.split
136-
|> Enum.map(String.strip(&1))
136+
|> Enum.filter(&(&1 != ""))
137137
|> Enum.sort
138138
assert capture_io(fn -> ls "~" end) == capture_io(fn -> ls System.user_home end)
139139
end

0 commit comments

Comments
 (0)