Skip to content

Commit 3f0c583

Browse files
author
José Valim
committed
Deprecate Regex.captures and String.valid_codepoint?
1 parent 0ce2fbb commit 3f0c583

File tree

4 files changed

+26
-41
lines changed

4 files changed

+26
-41
lines changed

lib/elixir/lib/regex.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,27 @@ defmodule Regex do
156156
end
157157
end
158158

159+
@doc false
160+
def captures(regex, string, options // []) do
161+
IO.write "Regex.captures/3 is deprecated, please use Regex.named_captures/3\n#{Exception.format_stacktrace}"
162+
named_captures(regex, string, options)
163+
end
164+
159165
@doc """
160166
Returns the given captures as a keyword list or `nil` if no captures
161167
are found. Requires the regex to be compiled with the groups option.
162168
163169
## Examples
164170
165-
iex> Regex.captures(%r/c(?<foo>d)/g, "abcd")
171+
iex> Regex.named_captures(%r/c(?<foo>d)/g, "abcd")
166172
[foo: "d"]
167-
iex> Regex.captures(%r/a(?<foo>b)c(?<bar>d)/g, "abcd")
173+
iex> Regex.named_captures(%r/a(?<foo>b)c(?<bar>d)/g, "abcd")
168174
[foo: "b", bar: "d"]
169-
iex> Regex.captures(%r/a(?<foo>b)c(?<bar>d)/g, "efgh")
175+
iex> Regex.named_captures(%r/a(?<foo>b)c(?<bar>d)/g, "efgh")
170176
nil
171177
172178
"""
173-
def captures(regex(groups: groups) = regex, string, options // []) do
179+
def named_captures(regex(groups: groups) = regex, string, options // []) do
174180
options = Keyword.put_new(options, :capture, :groups)
175181
results = run(regex, string, options)
176182
if results, do: Enum.zip(groups, results)

lib/elixir/lib/string.ex

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -661,27 +661,14 @@ defmodule String do
661661
def valid_character?(<<_ :: utf8>> = codepoint), do: valid?(codepoint)
662662
def valid_character?(_), do: false
663663

664-
@doc %S"""
665-
Checks whether `str` is a valid codepoint.
666-
667-
Note that the empty string is considered invalid, as are
668-
strings containing multiple codepoints.
669-
670-
## Examples
671-
672-
iex> String.valid_codepoint?("a")
673-
true
674-
iex> String.valid_codepoint?("ø")
675-
true
676-
iex> String.valid_codepoint?(<<0xffff :: 16>>)
677-
false
678-
iex> String.valid_codepoint?("asdf")
679-
false
664+
@doc false
665+
def valid_codepoint?(binary) do
666+
IO.write "String.valid_codepoint?/1 is deprecated, please use match against << _ :: utf8 >> instead\n#{Exception.format_stacktrace}"
667+
do_valid_codepoint?(binary)
668+
end
680669

681-
"""
682-
@spec valid_codepoint?(codepoint) :: boolean
683-
def valid_codepoint?(<<_ :: utf8>>), do: true
684-
def valid_codepoint?(_), do: false
670+
defp do_valid_codepoint?(<<_ :: utf8>>), do: true
671+
defp do_valid_codepoint?(_), do: false
685672

686673
@doc """
687674
Returns unicode graphemes in the string.

lib/elixir/test/elixir/regex_test.exs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ defmodule Regex.BinaryTest do
6868
assert Regex.match?(%r/foo$/, "afoo")
6969
end
7070

71-
test :captures do
72-
assert Keyword.equal? Regex.captures(%r/(?<foo>c)(?<bar>d)/g, 'abcd'), [bar: 'd', foo: 'c']
73-
assert Regex.captures(%r/c(?<foo>d)/g, 'abcd') == [foo: 'd']
74-
assert Regex.captures(%r/c(?<foo>d)/g, 'no_match') == nil
75-
assert Regex.captures(%r/c(?<foo>d|e)/g, 'abcd abce') == [foo: 'd']
76-
assert Regex.captures(%r/c(?<foo>d)/g, 'abcd', return: :binary) == [foo: "d"]
77-
assert Regex.captures(%r/c(.)/g, 'cat') == []
71+
test :named_captures do
72+
assert Keyword.equal? Regex.named_captures(%r/(?<foo>c)(?<bar>d)/g, 'abcd'), [bar: 'd', foo: 'c']
73+
assert Regex.named_captures(%r/c(?<foo>d)/g, 'abcd') == [foo: 'd']
74+
assert Regex.named_captures(%r/c(?<foo>d)/g, 'no_match') == nil
75+
assert Regex.named_captures(%r/c(?<foo>d|e)/g, 'abcd abce') == [foo: 'd']
76+
assert Regex.named_captures(%r/c(?<foo>d)/g, 'abcd', return: :binary) == [foo: "d"]
77+
assert Regex.named_captures(%r/c(.)/g, 'cat') == []
7878
end
7979
8080
test :sigil_R do

lib/elixir/test/elixir/string_test.exs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ defmodule StringTest do
200200
end
201201

202202
test :mixed_codepoints do
203-
assert String.codepoints("ϖͲϥЫݎߟΈټϘለДШव׆ש؇؊صلټܗݎޥޘ߉ऌ૫ሏᶆ℆ℙℱ ⅚Ⅷ↠∈⌘①ffi") == ["ϖ", "Ͳ", "ϥ", "Ы", "ݎ", "ߟ", "Έ", "ټ", "Ϙ", "ለ", "Д", "Ш", "व", "׆", "ש", "؇", "؊", "ص", "ل", "ټ", "ܗ", "ݎ", "ޥ", "ޘ", "߉", "ऌ", "૫", "ሏ", "ᶆ", "℆", "ℙ", "ℱ", " ", "⅚", "Ⅷ", "↠", "∈", "⌘", "①", "ffi"]
203+
assert String.codepoints("ϖͲϥЫݎߟΈټϘለДШव׆ש؇؊صلټܗݎޥޘ߉ऌ૫ሏᶆ℆ℙℱ ⅚Ⅷ↠∈⌘①ffi") ==
204+
["ϖ", "Ͳ", "ϥ", "Ы", "ݎ", "ߟ", "Έ", "ټ", "Ϙ", "ለ", "Д", "Ш", "व", "׆", "ש", "؇", "؊", "ص", "ل", "ټ", "ܗ", "ݎ", "ޥ", "ޘ", "߉", "ऌ", "૫", "ሏ", "ᶆ", "℆", "ℙ", "ℱ", " ", "⅚", "Ⅷ", "↠", "∈", "⌘", "①", "ffi"]
204205
end
205206

206207
test :graphemes do
@@ -302,15 +303,6 @@ defmodule StringTest do
302303
refute String.valid_character?("ab")
303304
end
304305

305-
test :valid_codepoint? do
306-
assert String.valid_codepoint?("a")
307-
assert String.valid_codepoint?("ø")
308-
assert String.valid_codepoint?("あ")
309-
310-
refute String.valid_codepoint?(<<0xffff :: 16>>)
311-
refute String.valid_codepoint?("ab")
312-
end
313-
314306
test :to_integer do
315307
assert String.to_integer("12") === {12, ""}
316308
assert String.to_integer("-12") === {-12, ""}

0 commit comments

Comments
 (0)