Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3757,6 +3757,8 @@ defmodule Kernel do
end
end

defp split_words("", _modifiers), do: []

defp split_words(string, modifiers) do
mod = case modifiers do
[] -> ?b
Expand Down
2 changes: 2 additions & 0 deletions lib/elixir/lib/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ defmodule String do
@spec split(t, t | [t] | Regex.t, Keyword.t) :: [t]
def split(binary, pattern, options // [])

def split("", _pattern, _options), do: [""]

def split(binary, pattern, options) when is_regex(pattern) do
Regex.split(pattern, binary, global: options[:global])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/priv/unicode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defmodule String.Unicode do

# Split

def split(""), do: ""
def split(""), do: [""]

def split(string) when is_binary(string) do
:lists.reverse do_split(string, "", [])
Expand Down
1 change: 1 addition & 0 deletions lib/elixir/test/elixir/kernel/sigils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule Kernel.SigilsTest do
end

test :sigil_w do
assert %w() == []
assert %w(foo bar baz) == ["foo", "bar", "baz"]
assert %w(foo #{:bar} baz) == ["foo", "bar", "baz"]

Expand Down
1 change: 1 addition & 0 deletions lib/elixir/test/elixir/regex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ defmodule Regex.BinaryTest do
end

test :split do
assert Regex.split(%r",", "") == [""]
assert Regex.split(%r" ", "foo bar baz") == ["foo", "bar", "baz"]
assert Regex.split(%r" ", "foo bar baz", parts: 2) == ["foo", "bar baz"]
assert Regex.split(%r"\s", "foobar") == ["foobar"]
Expand Down
3 changes: 3 additions & 0 deletions lib/elixir/test/elixir/string_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule StringTest do
end

test :split do
assert String.split("") == [""]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test for String.split with a regex and Regex.split too? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Although, this just pointed out to me that Regex.split is a reversed arg order from String.split!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok, the "subject" of a Regex is a regex, not a string. :)

assert String.split("foo bar") == ["foo", "bar"]
assert String.split(" foo bar") == ["foo", "bar"]
assert String.split("foo bar ") == ["foo", "bar"]
Expand All @@ -24,6 +25,7 @@ defmodule StringTest do
assert String.split("foo" <> <<31>> <> "bar") == ["foo", "bar"]
assert String.split("foo" <> <<194, 133>> <> "bar") == ["foo", "bar"]

assert String.split("", ",") == [""]
assert String.split("a,b,c", ",") == ["a", "b", "c"]
assert String.split("a,b", ".") == ["a,b"]
assert String.split("1,2 3,4", [" ", ","]) == ["1", "2", "3", "4"]
Expand All @@ -33,6 +35,7 @@ defmodule StringTest do
end

test :split_with_regex do
assert String.split("", %r{,}) == [""]
assert String.split("a,b", %r{,}) == ["a", "b"]
assert String.split("a,b,c", %r{,}) == ["a", "b", "c"]
assert String.split("a,b,c", %r{,}, global: false) == ["a", "b,c"]
Expand Down