Skip to content

Commit

Permalink
Move unescape map to Kernel for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 27, 2023
1 parent bc3c915 commit f23b283
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
13 changes: 11 additions & 2 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6188,16 +6188,25 @@ defmodule Kernel do
defmacro sigil_r(term, modifiers)

defmacro sigil_r({:<<>>, _meta, [string]}, options) when is_binary(string) do
binary = :elixir_interpolation.unescape_string(string, &Regex.unescape_map/1)
binary = :elixir_interpolation.unescape_string(string, &regex_unescape_map/1)
regex = Regex.compile!(binary, :binary.list_to_bin(options))
Macro.escape(regex)
end

defmacro sigil_r({:<<>>, meta, pieces}, options) do
binary = {:<<>>, meta, unescape_tokens(pieces, &Regex.unescape_map/1)}
binary = {:<<>>, meta, unescape_tokens(pieces, &regex_unescape_map/1)}
quote(do: Regex.compile!(unquote(binary), unquote(:binary.list_to_bin(options))))
end

defp regex_unescape_map(:newline), do: true
defp regex_unescape_map(?f), do: ?\f
defp regex_unescape_map(?n), do: ?\n
defp regex_unescape_map(?r), do: ?\r
defp regex_unescape_map(?t), do: ?\t
defp regex_unescape_map(?v), do: ?\v
defp regex_unescape_map(?a), do: ?\a
defp regex_unescape_map(_), do: false

@doc ~S"""
Handles the sigil `~R` for regular expressions.
Expand Down
13 changes: 0 additions & 13 deletions lib/elixir/lib/regex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -852,19 +852,6 @@ defmodule Regex do

# Helpers

@doc false
# Unescape map function used by Macro.unescape_string.
def unescape_map(:newline), do: true
def unescape_map(?f), do: ?\f
def unescape_map(?n), do: ?\n
def unescape_map(?r), do: ?\r
def unescape_map(?t), do: ?\t
def unescape_map(?v), do: ?\v
def unescape_map(?a), do: ?\a
def unescape_map(_), do: false

# Private Helpers

defp translate_options(<<?u, t::binary>>, acc), do: translate_options(t, [:unicode, :ucp | acc])
defp translate_options(<<?i, t::binary>>, acc), do: translate_options(t, [:caseless | acc])
defp translate_options(<<?x, t::binary>>, acc), do: translate_options(t, [:extended | acc])
Expand Down

0 comments on commit f23b283

Please sign in to comment.