Skip to content

Commit e304ea9

Browse files
committed
Accept the trim option for Regex.split/3
By default `trim` will be set to true. However, since the `global` option was implemented with the `parts` option (`{ :parts, :infinity }`), we have to apply a filter to the final result to support `trim`.
1 parent 64cdc76 commit e304ea9

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

lib/elixir/lib/regex.ex

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,17 @@ defmodule Regex do
264264
def split(regex, string, options // [])
265265

266266
def split(regex(re_pattern: compiled), string, options) do
267-
parts =
268-
cond do
269-
Keyword.get(options, :global) == false -> 2
270-
p = Keyword.get(options, :parts) -> p
271-
true -> :infinity
272-
end
267+
defaults = [global: true, trim: true, parts: :infinity, return: return_for(string)]
268+
options = Keyword.merge(defaults, options)
273269

274-
return = Keyword.get(options, :return, return_for(string))
275-
opts = [return: return, parts: parts]
276-
:re.split(string, compiled, opts)
270+
unless options[:global], do: options = Keyword.put(options, :parts, 2)
271+
272+
valid_options = Dict.take(options, [:parts, :return])
273+
splits = :re.split(string, compiled, valid_options)
274+
275+
if options[:trim], do: splits = Enum.filter(splits, &(&1 != ""))
276+
277+
splits
277278
end
278279

279280
@doc %B"""

lib/elixir/test/elixir/regex_test.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ defmodule Regex.BinaryTest do
113113
end
114114
115115
test :split do
116-
assert Regex.split(%r",", "") == [""]
116+
assert Regex.split(%r",", "") == []
117117
assert Regex.split(%r" ", "foo bar baz") == ["foo", "bar", "baz"]
118118
assert Regex.split(%r" ", "foo bar baz", parts: 2) == ["foo", "bar baz"]
119119
assert Regex.split(%r"\s", "foobar") == ["foobar"]
120120
assert Regex.split(%r" ", "foo bar baz") == ["foo", "bar", "baz"]
121-
assert Regex.split(%r"=", "key=") == ["key", ""]
122-
assert Regex.split(%r"=", "=value") == ["", "value"]
121+
assert Regex.split(%r" ", " foo bar baz ", trim: false) == ["", "foo", "bar", "baz", ""]
122+
assert Regex.split(%r"=", "key=") == ["key"]
123+
assert Regex.split(%r"=", "=value") == ["value"]
123124
end
124125
125126
test :replace do

lib/elixir/test/elixir/string_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ defmodule StringTest do
4343
assert String.split("a,b", %r{,}) == ["a", "b"]
4444
assert String.split("a,b,c", %r{,}) == ["a", "b", "c"]
4545
assert String.split("a,b,c", %r{,}, global: false) == ["a", "b,c"]
46+
assert String.split("a,b.c ", %r{\W}) == ["a", "b", "c"]
47+
assert String.split("a,b.c ", %r{\W}, trim: false) == ["a", "b", "c", ""]
4648
assert String.split("a,b", %r{\.}) == ["a,b"]
4749
end
4850

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ defmodule ExUnit.DocTest do
331331
end
332332

333333
defp extract_tests(line, doc) do
334-
lines = String.split(doc, %r/\n/) |> adjust_indent
334+
lines = String.split(doc, %r/\n/, trim: false) |> adjust_indent
335335
extract_tests(lines, line, "", "", [], true)
336336
end
337337

0 commit comments

Comments
 (0)