Skip to content

Commit dd79a3e

Browse files
author
José Valim
committed
Remove more regexes from stdlib and logger
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent aca50d4 commit dd79a3e

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

lib/elixir/lib/regex.ex

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,6 @@ defmodule Regex do
701701
[get_index(string, h) | get_indexes(string, t, arity - 1)]
702702
end
703703

704-
{:ok, pattern} = :re.compile(~S"[.^$*+?()\[\]{}\\\|\s#-]", [:unicode])
705-
@escape_pattern pattern
706-
707704
@doc ~S"""
708705
Escapes a string to be literally matched in a regex.
709706
@@ -718,8 +715,14 @@ defmodule Regex do
718715
"""
719716
@spec escape(String.t) :: String.t
720717
def escape(string) when is_binary(string) do
721-
:re.replace(string, @escape_pattern, "\\\\&", [:global, {:return, :binary}])
718+
escape(string, [])
719+
end
720+
721+
for char <- '.^$*+?()[]{}|#-\\\t\n\v\f\r\s' do
722+
defp escape(<<unquote(char), rest::binary>>, acc), do: escape(rest, [acc, ?\\, unquote(char)])
722723
end
724+
defp escape(<<char, rest::binary>>, acc), do: escape(rest, [acc, char])
725+
defp escape(<<>>, acc), do: IO.iodata_to_binary(acc)
723726

724727
# Helpers
725728

lib/elixir/lib/uri.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ defmodule URI do
400400
def parse(%URI{} = uri), do: uri
401401

402402
def parse(string) when is_binary(string) do
403-
# From http://tools.ietf.org/html/rfc3986#appendix-B
404-
regex = ~r/^(([a-z][a-z0-9\+\-\.]*):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/i
403+
# From https://tools.ietf.org/html/rfc3986#appendix-B
404+
regex = Regex.recompile!(~r/^(([a-z][a-z0-9\+\-\.]*):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/i)
405405
parts = nillify(Regex.run(regex, string))
406406

407407
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
@@ -419,7 +419,8 @@ defmodule URI do
419419

420420
# Split an authority into its userinfo, host and port parts.
421421
defp split_authority(string) do
422-
components = Regex.run(~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/, string || "")
422+
regex = Regex.recompile!(~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/)
423+
components = Regex.run(regex, string || "")
423424

424425
destructure [_, _, userinfo, host, _, port], nillify(components)
425426
host = if host, do: host |> String.trim_leading("[") |> String.trim_trailing("]")

lib/logger/lib/logger/formatter.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ defmodule Logger.Formatter do
8484
def compile({mod, fun}) when is_atom(mod) and is_atom(fun), do: {mod, fun}
8585

8686
def compile(str) do
87-
for part <- Regex.split(~r/(?<head>)\$[a-z]+(?<tail>)/, str, on: [:head, :tail], trim: true) do
87+
regex = Regex.recompile!(~r/(?<head>)\$[a-z]+(?<tail>)/)
88+
89+
for part <- Regex.split(regex, str, on: [:head, :tail], trim: true) do
8890
case part do
8991
"$" <> code -> compile_code(String.to_atom(code))
9092
_ -> part

0 commit comments

Comments
 (0)