Skip to content

Commit dd4a0e9

Browse files
committed
No longer trim by default when splitting
Further discussion here: https://groups.google.com/forum/#!topic/elixir-lang-core/66gb3J0bnUA
1 parent bcceccd commit dd4a0e9

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

lib/elixir/lib/regex.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ defmodule Regex do
275275
opts = [return: return, parts: parts]
276276
splits = :re.split(string, compiled, opts)
277277

278-
if Keyword.get(options, :trim, true) do
278+
if Keyword.get(options, :trim, false) do
279279
lc split inlist splits, split != "", do: split
280280
else
281281
splits

lib/elixir/lib/string.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,17 @@ defmodule String do
163163
The string is split into as many parts as possible by
164164
default, unless the `global` option is set to `false`.
165165
166-
Empty strings are removed from the result, unless the
167-
`trim` option is set to `false`.
166+
Empty strings are only removed from the result if the
167+
`trim` option is set to `true`.
168168
169169
## Examples
170170
171171
iex> String.split("a,b,c", ",")
172172
["a", "b", "c"]
173173
iex> String.split("a,b,c", ",", global: false)
174174
["a", "b,c"]
175-
iex> String.split(" a b c ", " ", trim: false)
176-
["", "a", "b", "c", ""]
175+
iex> String.split(" a b c ", " ", trim: true)
176+
["a", "b", "c"]
177177
178178
iex> String.split("1,2 3,4", [" ", ","])
179179
["1", "2", "3", "4"]
@@ -200,7 +200,7 @@ defmodule String do
200200
opts = if options[:global] != false, do: [:global], else: []
201201
splits = :binary.split(binary, pattern, opts)
202202

203-
if Keyword.get(options, :trim, true) do
203+
if Keyword.get(options, :trim, false) do
204204
lc split inlist splits, split != "", do: split
205205
else
206206
splits

lib/elixir/test/elixir/regex_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +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"]
120-
assert Regex.split(%r" ", "foo bar baz") == ["foo", "bar", "baz"]
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"]
120+
assert Regex.split(%r" ", " foo bar baz ") == ["", "foo", "bar", "baz", ""]
121+
assert Regex.split(%r" ", " foo bar baz ", trim: true) == ["foo", "bar", "baz"]
122+
assert Regex.split(%r"=", "key=") == ["key", ""]
123+
assert Regex.split(%r"=", "=value") == ["", "value"]
124124
end
125125
126126
test :replace do

lib/elixir/test/elixir/string_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ defmodule StringTest do
2929
assert String.split("a,b,c", ",") == ["a", "b", "c"]
3030
assert String.split("a,b", ".") == ["a,b"]
3131
assert String.split("1,2 3,4", [" ", ","]) == ["1", "2", "3", "4"]
32-
assert String.split(" a b c ", " ") == ["a", "b", "c"]
32+
assert String.split(" a b c ", " ") == ["", "a", "b", "c", ""]
3333

3434
assert String.split("a,b,c", ",", global: false) == ["a", "b,c"]
3535
assert String.split("1,2 3,4", [" ", ","], global: false) == ["1", "2 3,4"]
3636

37-
assert String.split(" a b c ", " ", trim: false) == ["", "a", "b", "c", ""]
38-
assert String.split(" a b c ", " ", trim: false, global: false) == ["", "a b c "]
37+
assert String.split(" a b c ", " ", trim: true) == ["a", "b", "c"]
38+
assert String.split(" a b c ", " ", trim: true, global: false) == ["a b c "]
3939
end
4040

4141
test :split_with_regex do
4242
assert String.split("", %r{,}) == [""]
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"]
46+
assert String.split("a,b.c ", %r{\W}) == ["a", "b", "c", ""]
4747
assert String.split("a,b.c ", %r{\W}, trim: false) == ["a", "b", "c", ""]
4848
assert String.split("a,b", %r{\.}) == ["a,b"]
4949
end

0 commit comments

Comments
 (0)