Skip to content

Commit e33f300

Browse files
author
José Valim
committed
Remove regexes from ExUnit, IEx and Mix
1 parent 9074a99 commit e33f300

File tree

6 files changed

+48
-27
lines changed

6 files changed

+48
-27
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ defmodule ExUnit.DocTest do
426426
end
427427

428428
defp extract_tests(line_no, doc, module) do
429-
all_lines = String.split(doc, ~r/\n/, trim: false)
429+
all_lines = String.split(doc, "\n", trim: false)
430430
lines = adjust_indent(all_lines, line_no + 1, module)
431431
extract_tests(lines, "", "", [], true, module)
432432
end
@@ -626,11 +626,29 @@ defmodule ExUnit.DocTest do
626626
[mod, message] = :binary.split(error, ")")
627627
{:error, Module.concat([mod]), String.trim_leading(message)}
628628
_ ->
629-
if string =~ ~r/\A#[A-Z][\w\.]*</mu do
629+
if is_inspected?(string) do
630630
{:inspect, inspect(string)}
631631
else
632632
{:test, string}
633633
end
634634
end
635635
end
636+
637+
defp is_inspected?(<<?#, char, rest::binary>>) when char in ?A..?Z,
638+
do: is_inspected_end?(rest)
639+
defp is_inspected?(_),
640+
do: false
641+
642+
defp is_inspected_end?(<<?., char, rest::binary>>) when char in ?A..?Z,
643+
do: is_inspected_end?(rest)
644+
defp is_inspected_end?(<<char, rest::binary>>)
645+
when char in ?A..?Z
646+
when char in ?a..?z
647+
when char in ?0..?9
648+
when char == ?_,
649+
do: is_inspected_end?(rest)
650+
defp is_inspected_end?(<<?<, _::binary>>),
651+
do: true
652+
defp is_inspected_end?(_),
653+
do: false
636654
end

lib/ex_unit/lib/ex_unit/filters.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ defmodule ExUnit.Filters do
1414
"""
1515
@spec parse_path(String.t) :: {String.t, any}
1616
def parse_path(file) do
17-
case Regex.run(~r/^(.+):(\d+)$/, file, capture: :all_but_first) do
18-
[file, line_number] ->
19-
{file, exclude: [:test], include: [line: line_number]}
20-
nil ->
17+
{paths, [line]} = file |> String.split(":") |> Enum.split(-1)
18+
case Integer.parse(line) do
19+
{_, ""} ->
20+
{Enum.join(paths, ":"), exclude: [:test], include: [line: line]}
21+
_ ->
2122
{file, []}
2223
end
2324
end

lib/iex/lib/iex/server.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ defmodule IEx.Server do
269269

270270
defp allow_take?(identifier) do
271271
message = IEx.color(:eval_interrupt, "#{identifier}\nAllow? [Yn] ")
272-
IO.gets(:stdio, message) =~ ~r/^(Y(es)?)?$/i
272+
yes?(IO.gets(:stdio, message))
273+
end
274+
275+
defp yes?(string) do
276+
is_binary(string) and String.trim(string) in ["", "y", "Y", "yes", "YES", "Yes"]
273277
end
274278

275279
## State

lib/mix/lib/mix/shell/io.ex

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,27 @@ defmodule Mix.Shell.IO do
4646
end
4747

4848
@doc """
49-
Prints a message and prompts the user for
50-
input. Input will be consumed until Enter is pressed.
49+
Prints a message and prompts the user for input.
50+
51+
Input will be consumed until Enter is pressed.
5152
"""
5253
def prompt(message) do
5354
print_app()
54-
IO.gets message <> " "
55+
IO.gets(message <> " ")
5556
end
5657

5758
@doc """
5859
Prints a message and asks the user if they want to proceed.
59-
The user must press Enter or type anything that matches the "yes"
60-
regex `~r/^Y(es)?$/i`.
60+
61+
The user must press Enter or type one of "y", "yes", "Y", "YES" or
62+
"Yes".
6163
"""
6264
def yes?(message) do
6365
print_app()
64-
got_yes? IO.gets(message <> " [Yn] ")
66+
answer = IO.gets(message <> " [Yn] ")
67+
is_binary(answer) and String.trim(answer) in ["", "y", "Y", "yes", "YES", "Yes"]
6568
end
6669

67-
defp got_yes?(answer) when is_binary(answer) do
68-
answer =~ ~r/^(Y(es)?)?$/i
69-
end
70-
71-
# The IO server may return :eof or :error
72-
defp got_yes?(_), do: false
73-
7470
defp red(message) do
7571
[:red, :bright, message]
7672
end

lib/mix/lib/mix/shell/quiet.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ defmodule Mix.Shell.Quiet do
3232
defdelegate error(message), to: Mix.Shell.IO
3333

3434
@doc """
35-
Prints a message and prompts the user for
36-
input. Input will be consumed until Enter is pressed.
35+
Prints a message and prompts the user for input.
36+
37+
Input will be consumed until Enter is pressed.
3738
"""
3839
defdelegate prompt(message), to: Mix.Shell.IO
3940

4041
@doc """
41-
Receives a message and asks the user if they want to proceed.
42-
The user must press enter or type anything that matches the "yes"
43-
regex `~r/^Y(es)?$/i`.
42+
Prints a message and asks the user if they want to proceed.
43+
44+
The user must press Enter or type one of "y", "yes", "Y", "YES" or
45+
"Yes".
4446
"""
4547
defdelegate yes?(message), to: Mix.Shell.IO
4648
end

lib/mix/lib/mix/tasks/new.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ defmodule Mix.Tasks.New do
158158
end
159159

160160
defp check_application_name!(name, inferred?) do
161-
unless name =~ ~r/^[a-z][\w_]*$/ do
161+
unless name =~ Regex.recompile!(~r/^[a-z][a-z0-9_]*$/) do
162162
Mix.raise "Application name must start with a letter and have only lowercase " <>
163163
"letters, numbers and underscore, got: #{inspect name}" <>
164164
(if inferred? do
@@ -171,7 +171,7 @@ defmodule Mix.Tasks.New do
171171
end
172172

173173
defp check_mod_name_validity!(name) do
174-
unless name =~ ~r/^[A-Z]\w*(\.[A-Z]\w*)*$/ do
174+
unless name =~ Regex.recompile!(~r/^[A-Z]\w*(\.[A-Z]\w*)*$/) do
175175
Mix.raise "Module name must be a valid Elixir alias (for example: Foo.Bar), got: #{inspect name}"
176176
end
177177
end

0 commit comments

Comments
 (0)