Skip to content

Commit c1deb99

Browse files
author
José Valim
committed
Deprecate char lists in Regex
1 parent 1625bdb commit c1deb99

File tree

2 files changed

+18
-78
lines changed

2 files changed

+18
-78
lines changed

lib/elixir/lib/regex.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ defmodule Regex do
3737
* `newline` - not available, use `(*CR)` or `(*LF)` or `(*CRLF)` or `(*ANYCRLF)`
3838
or `(*ANY)` at the beginning of the regexp according to the re documentation
3939
40-
Most of the functions in this module accept either a binary or a char list
41-
as subject. The result is based on the argument (a binary will return
42-
a binary, a char list will return a char list).
4340
"""
4441

4542
defrecordp :regex, Regex, [:re_pattern, :source, :options, :groups]
@@ -346,7 +343,7 @@ defmodule Regex do
346343
"\\\\what\\ if"
347344
348345
"""
349-
@spec escape(String.t | char_list) :: String.t | char_list
346+
@spec escape(String.t) :: String.t
350347
def escape(string) do
351348
:re.replace(string, @escape_pattern, "\\\\&", [:global, { :return, return_for(string) }])
352349
end
@@ -366,7 +363,10 @@ defmodule Regex do
366363
# Private Helpers
367364

368365
defp return_for(element) when is_binary(element), do: :binary
369-
defp return_for(element) when is_list(element), do: :list
366+
defp return_for(element) when is_list(element) do
367+
IO.write :stderr, "Passing char lists to Regex is deprecated, please use binaries instead\n#{Exception.format_stacktrace}"
368+
:list
369+
end
370370

371371
defp translate_options(<<?u, t :: binary>>) do
372372
IO.write "The /u flag for regular expressions is no longer needed\n#{Exception.format_stacktrace}"

lib/elixir/test/elixir/regex_test.exs

Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Code.require_file "test_helper.exs", __DIR__
22

3-
defmodule Regex.BinaryTest do
3+
defmodule RegexTest do
44
use ExUnit.Case, async: true
55

66
test :multiline do
@@ -70,12 +70,11 @@ defmodule Regex.BinaryTest do
7070
end
7171

7272
test :named_captures do
73-
assert Keyword.equal? Regex.named_captures(~r/(?<foo>c)(?<bar>d)/g, 'abcd'), [bar: 'd', foo: 'c']
74-
assert Regex.named_captures(~r/c(?<foo>d)/g, 'abcd') == [foo: 'd']
75-
assert Regex.named_captures(~r/c(?<foo>d)/g, 'no_match') == nil
76-
assert Regex.named_captures(~r/c(?<foo>d|e)/g, 'abcd abce') == [foo: 'd']
77-
assert Regex.named_captures(~r/c(?<foo>d)/g, 'abcd', return: :binary) == [foo: "d"]
78-
assert Regex.named_captures(~r/c(.)/g, 'cat') == []
73+
assert Keyword.equal? Regex.named_captures(~r/(?<foo>c)(?<bar>d)/g, "abcd"), [bar: "d", foo: "c"]
74+
assert Regex.named_captures(~r/c(?<foo>d)/g, "abcd") == [foo: "d"]
75+
assert Regex.named_captures(~r/c(?<foo>d)/g, "no_match") == nil
76+
assert Regex.named_captures(~r/c(?<foo>d|e)/g, "abcd abce") == [foo: "d"]
77+
assert Regex.named_captures(~r/c(.)/g, "cat") == []
7978
end
8079

8180
test :sigil_R do
@@ -85,14 +84,12 @@ defmodule Regex.BinaryTest do
8584
test :run do
8685
assert Regex.run(~r"c(d)", "abcd") == ["cd", "d"]
8786
assert Regex.run(~r"e", "abcd") == nil
88-
assert Regex.run(~r"c(d)", "abcd", return: :list) == ['cd', 'd']
8987
end
9088

9189
test :run_with_groups do
92-
assert Regex.run(~r/c(?<foo>d)/g, 'abcd', capture: :groups) == ['d']
93-
assert Regex.run(~r/c(?<foo>d)/g, 'no_match', capture: :groups) == nil
94-
assert Regex.run(~r/c(?<foo>d|e)/g, 'abcd abce', capture: :groups) == ['d']
95-
assert Regex.run(~r/c(?<foo>d)/g, 'abcd', return: :binary, capture: :groups) == ["d"]
90+
assert Regex.run(~r/c(?<foo>d)/g, "abcd", capture: :groups) == ["d"]
91+
assert Regex.run(~r/c(?<foo>d)/g, "no_match", capture: :groups) == nil
92+
assert Regex.run(~r/c(?<foo>d|e)/g, "abcd abce", capture: :groups) == ["d"]
9693
end
9794

9895
test :run_with_indexes do
@@ -104,15 +101,13 @@ defmodule Regex.BinaryTest do
104101
assert Regex.scan(~r"c(d|e)", "abcd abce") == [["cd", "d"], ["ce", "e"]]
105102
assert Regex.scan(~r"c(?:d|e)", "abcd abce") == [["cd"], ["ce"]]
106103
assert Regex.scan(~r"e", "abcd") == []
107-
assert Regex.scan(~r"c(d|e)", "abcd abce", return: :list) == [['cd', 'd'], ['ce', 'e']]
108104
end
109105

110106
test :scan_with_groups do
111-
assert Regex.scan(~r/cd/g, 'abcd', capture: :groups) == []
112-
assert Regex.scan(~r/c(?<foo>d)/g, 'abcd', capture: :groups) == [['d']]
113-
assert Regex.scan(~r/c(?<foo>d)/g, 'no_match', capture: :groups) == []
114-
assert Regex.scan(~r/c(?<foo>d|e)/g, 'abcd abce', capture: :groups) == [['d'], ['e']]
115-
assert Regex.scan(~r/c(?<foo>d)/g, 'abcd', return: :binary, capture: :groups) == [["d"]]
107+
assert Regex.scan(~r/cd/g, "abcd", capture: :groups) == []
108+
assert Regex.scan(~r/c(?<foo>d)/g, "abcd", capture: :groups) == [["d"]]
109+
assert Regex.scan(~r/c(?<foo>d)/g, "no_match", capture: :groups) == []
110+
assert Regex.scan(~r/c(?<foo>d|e)/g, "abcd abce", capture: :groups) == [["d"], ["e"]]
116111
end
117112

118113
test :split do
@@ -172,58 +167,3 @@ defmodule Regex.BinaryTest do
172167
Regex.match? ~r/#{Regex.escape(string)}/simx, match
173168
end
174169
end
175-
176-
defmodule Regex.ListTest do
177-
use ExUnit.Case, async: true
178-
179-
test :match? do
180-
assert Regex.match?(~r(foo), 'foo')
181-
refute Regex.match?(~r(foo), 'FOO')
182-
assert Regex.match?(~r(foo)i, 'FOO')
183-
assert Regex.match?(~r/\d{1,3}/i, '123')
184-
185-
assert Regex.match?(~r(foo), 'afooa')
186-
refute Regex.match?(~r(^foo), 'afooa')
187-
assert Regex.match?(~r(^foo), 'fooa')
188-
refute Regex.match?(~r(foo$), 'afooa')
189-
assert Regex.match?(~r(foo$), 'afoo')
190-
end
191-
192-
test :run do
193-
assert Regex.run(~r'c(d)', 'abcd') == ['cd', 'd']
194-
assert Regex.run(~r'e', 'abcd') == nil
195-
assert Regex.run(~r"c(d)", "abcd", return: :binary) == ["cd", "d"]
196-
end
197-
198-
test :indexes do
199-
assert Regex.run(~r'c(d)', 'abcd', return: :index) == [{2, 2}, {3, 1}]
200-
assert Regex.run(~r'e', 'abcd', return: :index) == nil
201-
end
202-
203-
test :scan do
204-
assert Regex.scan(~r'c(d|e)', 'abcd abce') == [['cd', 'd'], ['ce', 'e']]
205-
assert Regex.scan(~r'c(?:d|e)', 'abcd abce') == [['cd'], ['ce']]
206-
assert Regex.scan(~r'e', 'abcd') == []
207-
assert Regex.scan(~r'c(d|e)', 'abcd abce', return: :binary) == [["cd", "d"], ["ce", "e"]]
208-
end
209-
210-
test :split do
211-
assert Regex.split(~r' ', 'foo bar baz') == ['foo', 'bar', 'baz']
212-
assert Regex.split(~r' ', 'foo bar baz', parts: 2) == ['foo', 'bar baz']
213-
assert Regex.split(~r'\s', 'foobar') == ['foobar']
214-
end
215-
216-
test :replace do
217-
assert Regex.replace(~r(d), 'abc', 'd') == 'abc'
218-
assert Regex.replace(~r(b), 'abc', 'd') == 'adc'
219-
assert Regex.replace(~r(b), 'abc', '[&]') == 'a[b]c'
220-
assert Regex.replace(~r(b), 'abc', '[\\&]') == 'a[&]c'
221-
assert Regex.replace(~r[(b)], 'abc', '[\\1]') == 'a[b]c'
222-
223-
assert Regex.replace(~r(d), 'abcbe', 'd') == 'abcbe'
224-
assert Regex.replace(~r(b), 'abcbe', 'd') == 'adcde'
225-
assert Regex.replace(~r(b), 'abcbe', '[&]') == 'a[b]c[b]e'
226-
assert Regex.replace(~r(b), 'abcbe', '[\\&]') == 'a[&]c[&]e'
227-
assert Regex.replace(~r[(b)], 'abcbe', '[\\1]') == 'a[b]c[b]e'
228-
end
229-
end

0 commit comments

Comments
 (0)