Skip to content

Commit 7aebe55

Browse files
cogleJosé Valim
authored andcommitted
Fix String.split when parts and trim is used (#5077)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent 8aab3ce commit 7aebe55

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

lib/elixir/lib/string.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ defmodule String do
350350
defp parts_to_index(:infinity), do: 0
351351
defp parts_to_index(n) when is_integer(n) and n > 0, do: n
352352

353+
defp split_each("", _pattern, true, 1), do: []
353354
defp split_each(string, _pattern, _trim, 1) when is_binary(string), do: [string]
354355
defp split_each(string, pattern, trim, count) do
355356
case do_splitter(string, pattern, trim) do

lib/elixir/test/elixir/string_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ defmodule StringTest do
6666
assert String.split("abé", "", trim: true, parts: 2) == ["a", "bé"]
6767

6868
assert String.split("noël", "") == ["n", "o", "ë", "l", ""]
69+
70+
assert String.split("x-", "-", parts: 2, trim: true) == ["x"]
71+
assert String.split("x-x-", "-", parts: 3, trim: true) == ["x", "x"]
6972
end
7073

7174
test "split with regex" do

0 commit comments

Comments
 (0)