Skip to content

Commit 6d02cd9

Browse files
author
José Valim
committed
Merge pull request #1672 from meh/regex-compile
Return error on unsupported options in Regex.compile
2 parents 42692c2 + 6e60fa1 commit 6d02cd9

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/elixir/lib/regex.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ defmodule Regex do
6262
def compile(source, options // "")
6363

6464
def compile(source, options) when is_binary(options) do
65-
compile(source, translate_options(options), options)
65+
case translate_options(options) do
66+
{ :error, rest } ->
67+
{ :error, { :invalid_option, rest } }
68+
69+
translated_options ->
70+
compile(source, translated_options, options)
71+
end
6672
end
6773

6874
def compile(source, options) when is_list(options) do
@@ -357,6 +363,7 @@ defmodule Regex do
357363
defp translate_options(<<?m, t :: binary>>), do: [:multiline|translate_options(t)]
358364
defp translate_options(<<?g, t :: binary>>), do: [:groups|translate_options(t)]
359365
defp translate_options(<<>>), do: []
366+
defp translate_options(rest), do: { :error, rest }
360367

361368
{ :ok, pattern } = :re.compile(%B"\(\?<(?<G>[^>]*)>")
362369
@groups_pattern pattern

lib/elixir/test/elixir/regex_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ defmodule Regex.BinaryTest do
2929
{ :ok, regex } = Regex.compile("foo")
3030
assert is_regex(regex)
3131
assert { :error, _ } = Regex.compile("*foo")
32+
assert { :error, _ } = Regex.compile("foo", "y")
3233
end
3334

3435
test :compile_with_erl_opts do

0 commit comments

Comments
 (0)