Skip to content

Commit 74c82f8

Browse files
author
Devin Torres
committed
String.split("") should be [""], %w() should be []
1 parent f8dfc48 commit 74c82f8

File tree

6 files changed

+10
-1
lines changed

6 files changed

+10
-1
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,6 +3757,8 @@ defmodule Kernel do
37573757
end
37583758
end
37593759

3760+
defp split_words("", _modifiers), do: []
3761+
37603762
defp split_words(string, modifiers) do
37613763
mod = case modifiers do
37623764
[] -> ?b

lib/elixir/lib/string.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ defmodule String do
246246
@spec split(t, t | [t] | Regex.t, Keyword.t) :: [t]
247247
def split(binary, pattern, options // [])
248248

249+
def split("", _pattern, _options), do: [""]
250+
249251
def split(binary, pattern, options) when is_regex(pattern) do
250252
Regex.split(pattern, binary, global: options[:global])
251253
end

lib/elixir/priv/unicode.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ defmodule String.Unicode do
137137

138138
# Split
139139

140-
def split(""), do: ""
140+
def split(""), do: [""]
141141

142142
def split(string) when is_binary(string) do
143143
:lists.reverse do_split(string, "", [])

lib/elixir/test/elixir/kernel/sigils_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ defmodule Kernel.SigilsTest do
5050
end
5151
5252
test :sigil_w do
53+
assert %w() == []
5354
assert %w(foo bar baz) == ["foo", "bar", "baz"]
5455
assert %w(foo #{:bar} baz) == ["foo", "bar", "baz"]
5556

lib/elixir/test/elixir/regex_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ defmodule Regex.BinaryTest do
101101
end
102102

103103
test :split do
104+
assert Regex.split(%r",", "") == [""]
104105
assert Regex.split(%r" ", "foo bar baz") == ["foo", "bar", "baz"]
105106
assert Regex.split(%r" ", "foo bar baz", parts: 2) == ["foo", "bar baz"]
106107
assert Regex.split(%r"\s", "foobar") == ["foobar"]

lib/elixir/test/elixir/string_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ defmodule StringTest do
1616
end
1717

1818
test :split do
19+
assert String.split("") == [""]
1920
assert String.split("foo bar") == ["foo", "bar"]
2021
assert String.split(" foo bar") == ["foo", "bar"]
2122
assert String.split("foo bar ") == ["foo", "bar"]
@@ -24,6 +25,7 @@ defmodule StringTest do
2425
assert String.split("foo" <> <<31>> <> "bar") == ["foo", "bar"]
2526
assert String.split("foo" <> <<194, 133>> <> "bar") == ["foo", "bar"]
2627

28+
assert String.split("", ",") == [""]
2729
assert String.split("a,b,c", ",") == ["a", "b", "c"]
2830
assert String.split("a,b", ".") == ["a,b"]
2931
assert String.split("1,2 3,4", [" ", ","]) == ["1", "2", "3", "4"]
@@ -33,6 +35,7 @@ defmodule StringTest do
3335
end
3436

3537
test :split_with_regex do
38+
assert String.split("", %r{,}) == [""]
3639
assert String.split("a,b", %r{,}) == ["a", "b"]
3740
assert String.split("a,b,c", %r{,}) == ["a", "b", "c"]
3841
assert String.split("a,b,c", %r{,}, global: false) == ["a", "b,c"]

0 commit comments

Comments
 (0)