Skip to content

Commit e42e3e5

Browse files
author
José Valim
committed
Deprecate =~
Use String.contains?/2 when the right side is a string or String.match?/2 when the right side is a regex. The goal of this deprecation is to make space for the upcoming `~` and `~=` operators.
1 parent 2a6039d commit e42e3e5

30 files changed

+211
-263
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Deprecations
1717
* [Kernel] `binary_to_term/1`, `binary_to_term/2`, `term_to_binary/1` and `term_to_binary/2` are deprecated in favor of their counterparts in the `:erlang` module
1818
* [Kernel] `//` for default arguments is deprecated in favor of `\\`. This is a soft deprecation, no warnings will be emitted for it in this release
19+
* [Kernel] Deprecate `=~` in favor of `String.contains?/2` (for strings) and `String.match?/2` (for regexes)
1920
* [Kernel] Deprecate `@behavior` in favor of `@behaviour`
2021
* [Record] Deprecate `to_keywords`, `getter` and `list getter` functionalities in `defrecordp`
2122

lib/elixir/lib/kernel.ex

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,32 +1789,15 @@ defmodule Kernel do
17891789
end
17901790
end
17911791

1792-
@doc """
1793-
Matches the term on the left against the regular expression or string on the
1794-
right. Returns true if `left` matches `right` (if it's a regular expression)
1795-
or contains `right` (if it's a string).
1796-
1797-
## Examples
1798-
1799-
iex> "abcd" =~ %r/c(d)/
1800-
true
1801-
1802-
iex> "abcd" =~ %r/e/
1803-
false
1804-
1805-
iex> "abcd" =~ "bc"
1806-
true
1807-
1808-
iex> "abcd" =~ "ad"
1809-
false
1810-
1811-
"""
1792+
@doc false
18121793
def left =~ right when is_binary(left) and is_binary(right) do
1794+
IO.write :stderr, "left =~ right is deprecated, please use String.match?/2 (for regexes) or String.contains?/2 (for strings) instead\n#{Exception.format_stacktrace}"
18131795
:binary.match(left, right) != :nomatch
18141796
end
18151797

18161798
def left =~ right when is_binary(left) and is_tuple(right) and
18171799
tuple_size(right) > 0 and elem(right, 0) == Regex do
1800+
IO.write :stderr, "left =~ right is deprecated, please use String.match?/2 (for regexes) or String.contains?/2 (for strings) instead\n#{Exception.format_stacktrace}"
18181801
Regex.match?(right, left)
18191802
end
18201803

lib/elixir/lib/string.ex

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ defmodule String do
974974
end
975975

976976
defp do_starts_with(string, prefix) when is_binary(prefix) do
977-
match?({0, _}, :binary.match(string, prefix))
977+
Kernel.match?({0, _}, :binary.match(string, prefix))
978978
end
979979

980980
defp do_starts_with(_, _) do
@@ -1021,7 +1021,24 @@ defmodule String do
10211021
end
10221022

10231023
@doc """
1024-
Returns `true` if `string` contains match, otherwise `false`.
1024+
Check if `string` matches the given regular expression.
1025+
1026+
## Examples
1027+
1028+
iex> String.match?("foo", %r/foo/)
1029+
true
1030+
iex> String.match?("bar", %r/foo/)
1031+
false
1032+
1033+
"""
1034+
@spec match?(t, Regex.t) :: boolean
1035+
def match?(string, regex) do
1036+
Regex.match?(regex, string)
1037+
end
1038+
1039+
@doc """
1040+
Check if `string` contains any of the given `contents`.
1041+
10251042
`matches` can be either a single string or a list of strings.
10261043
10271044
## Examples
@@ -1036,12 +1053,12 @@ defmodule String do
10361053
"""
10371054
@spec contains?(t, t | [t]) :: boolean
10381055

1039-
def contains?(string, matches) when is_list(matches) do
1040-
Enum.any?(matches, &do_contains(string, &1))
1056+
def contains?(string, contents) when is_list(contents) do
1057+
Enum.any?(contents, &do_contains(string, &1))
10411058
end
10421059

1043-
def contains?(string, match) do
1044-
do_contains(string, match)
1060+
def contains?(string, content) do
1061+
do_contains(string, content)
10451062
end
10461063

10471064
defp do_contains(string, "") when is_binary(string) do

lib/elixir/lib/version.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ defmodule Version do
353353
@doc false
354354
def parse_pre(pre) do
355355
String.split(pre, ".") |> Enum.map fn piece ->
356-
if piece =~ %r/^(0|[1-9][0-9]*)$/ do
356+
if Regex.match?(%r/^(0|[1-9][0-9]*)$/, piece) do
357357
binary_to_integer(piece)
358358
else
359359
piece

lib/elixir/test/elixir/exception_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ defmodule Kernel.ExceptionTest do
4747
assert Exception.format_stacktrace_entry({:lists, :bar, [1, 2, 3], []}) == "(stdlib) :lists.bar(1, 2, 3)" end
4848

4949
test "format_stacktrace_entry with fun" do
50-
assert Exception.format_stacktrace_entry({fn(x) -> x end, [1], []}) =~ %r/#Function<.+>\(1\)/
51-
assert Exception.format_stacktrace_entry({fn(x, y) -> { x, y } end, 2, []}) =~ %r"#Function<.+>/2"
50+
assert String.match? Exception.format_stacktrace_entry({fn(x) -> x end, [1], []}), %r/#Function<.+>\(1\)/
51+
assert String.match? Exception.format_stacktrace_entry({fn(x, y) -> { x, y } end, 2, []}), %r"#Function<.+>/2"
5252
end
5353

5454
test "format_mfa" do
@@ -61,7 +61,7 @@ defmodule Kernel.ExceptionTest do
6161
end
6262

6363
test "format_fa" do
64-
assert Exception.format_fa(fn -> end, 1) =~
64+
assert String.match? Exception.format_fa(fn -> end, 1),
6565
%r"#Function<\d\.\d+/0 in Kernel\.ExceptionTest\.test format_fa/1>/1"
6666
end
6767

lib/elixir/test/elixir/hash_dict_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ defmodule HashDictTest do
4949
end
5050

5151
test "inspect" do
52-
assert inspect(filled_dict(8)) =~ "#HashDict<"
52+
assert String.starts_with? inspect(filled_dict(8)), "#HashDict<"
5353
end
5454

5555
defp smoke_test(range) do

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ defmodule Kernel.CLI.ParallelCompilerTest do
8787

8888
test "compiles files solving dependencies" do
8989
fixtures = [fixture_path("parallel_compiler/bar.ex"), fixture_path("parallel_compiler/foo.ex")]
90-
assert capture_io(fn ->
90+
assert String.contains? capture_io(fn ->
9191
assert [Bar, Foo] = Kernel.ParallelCompiler.files fixtures
92-
end) =~ "message_from_foo"
92+
end), "message_from_foo"
9393
end
9494

9595
test "does not hang on missing dependencies" do
9696
fixtures = [fixture_path("parallel_compiler/bat.ex")]
97-
assert capture_io(fn ->
97+
assert String.contains? capture_io(fn ->
9898
assert_raise CompileError, fn ->
9999
Kernel.ParallelCompiler.files fixtures
100100
end
101-
end) =~ "Compilation error"
101+
end), "Compilation error"
102102
end
103103

104104
test "handles possible deadlocks" do
@@ -110,8 +110,8 @@ defmodule Kernel.CLI.ParallelCompilerTest do
110110
end
111111
end)
112112

113-
assert msg =~ "* #{fixture_path "parallel_deadlock/foo.ex"} is missing module Bar"
114-
assert msg =~ "* #{fixture_path "parallel_deadlock/bar.ex"} is missing module Foo"
113+
assert String.contains? msg, "* #{fixture_path "parallel_deadlock/foo.ex"} is missing module Bar"
114+
assert String.contains? msg, "* #{fixture_path "parallel_deadlock/bar.ex"} is missing module Foo"
115115
end
116116

117117
test "warnings_as_errors" do

lib/elixir/test/elixir/kernel/expansion_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ defmodule Kernel.ExpansionTest do
153153
## Locals
154154

155155
test "locals: expands to remote calls" do
156-
assert { {:., _, [Kernel, :=~] }, _, [{:a, _, []}, {:b, _, []}] } =
157-
expand(quote do: a =~ b)
156+
assert { {:., _, [Kernel, :function_exported?] }, _, [{:a, _, []}, {:b, _, []}, {:c, _, []}] } =
157+
expand(quote do: function_exported?(a, b, c))
158158
end
159159

160160
test "locals: expands to configured local" do

0 commit comments

Comments
 (0)