From d2717a7d13d18d5ae4db817f37206c88f7b68f06 Mon Sep 17 00:00:00 2001 From: preciz Date: Sun, 14 Jun 2026 15:21:35 +0200 Subject: [PATCH] Optimize unpercent with guard and by avoiding with clause --- lib/elixir/lib/uri.ex | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/elixir/lib/uri.ex b/lib/elixir/lib/uri.ex index 25a9edc88ad..420d3a7cdf0 100644 --- a/lib/elixir/lib/uri.ex +++ b/lib/elixir/lib/uri.ex @@ -484,18 +484,19 @@ defmodule URI do unpercent(string, "", true) end + defguardp is_hex(char) when char in ?0..?9 or char in ?A..?F or char in ?a..?f + defp unpercent(<>, acc, spaces = true) do unpercent(tail, <>, spaces) end + defp unpercent(<>, acc, spaces) + when is_hex(h1) and is_hex(h2) do + unpercent(tail, <>, spaces) + end + defp unpercent(<>, acc, spaces) do - with <> <- tail, - dec1 when is_integer(dec1) <- hex_to_dec(hex1), - dec2 when is_integer(dec2) <- hex_to_dec(hex2) do - unpercent(tail, <>, spaces) - else - _ -> unpercent(tail, <>, spaces) - end + unpercent(tail, <>, spaces) end defp unpercent(<>, acc, spaces) do @@ -508,7 +509,6 @@ defmodule URI do defp hex_to_dec(n) when n in ?A..?F, do: n - ?A + 10 defp hex_to_dec(n) when n in ?a..?f, do: n - ?a + 10 defp hex_to_dec(n) when n in ?0..?9, do: n - ?0 - defp hex_to_dec(_n), do: nil @doc """ Creates a new URI struct by parsing and validating a string or from an existing URI.