Skip to content

Commit 30711a7

Browse files
authored
Optimize Base.decode* (#14430)
1 parent dfb7dbe commit 30711a7

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

Diff for: lib/elixir/lib/base.ex

+18-21
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,12 @@ defmodule Base do
390390
end
391391

392392
defp unquote(decode_name)(char) do
393-
try do
394-
elem({unquote_splicing(decoded)}, char - unquote(min))
395-
rescue
396-
_ -> bad_character!(char)
397-
else
398-
nil -> bad_character!(char)
399-
char -> char
393+
index = char - unquote(min)
394+
395+
cond do
396+
index not in 0..unquote(length(decoded) - 1) -> bad_character!(char)
397+
new_char = elem({unquote_splicing(decoded)}, index) -> new_char
398+
true -> bad_character!(char)
400399
end
401400
end
402401

@@ -859,13 +858,12 @@ defmodule Base do
859858
end
860859

861860
defp unquote(decode_name)(char) do
862-
try do
863-
elem({unquote_splicing(decoded)}, char - unquote(min))
864-
rescue
865-
_ -> bad_character!(char)
866-
else
867-
nil -> bad_character!(char)
868-
char -> char
861+
index = char - unquote(min)
862+
863+
cond do
864+
index not in 0..unquote(length(decoded) - 1) -> bad_character!(char)
865+
new_char = elem({unquote_splicing(decoded)}, index) -> new_char
866+
true -> bad_character!(char)
869867
end
870868
end
871869

@@ -1508,13 +1506,12 @@ defmodule Base do
15081506
end
15091507

15101508
defp unquote(decode_name)(char) do
1511-
try do
1512-
elem({unquote_splicing(decoded)}, char - unquote(min))
1513-
rescue
1514-
_ -> bad_character!(char)
1515-
else
1516-
nil -> bad_character!(char)
1517-
char -> char
1509+
index = char - unquote(min)
1510+
1511+
cond do
1512+
index not in 0..unquote(length(decoded) - 1) -> bad_character!(char)
1513+
new_char = elem({unquote_splicing(decoded)}, index) -> new_char
1514+
true -> bad_character!(char)
15181515
end
15191516
end
15201517

0 commit comments

Comments
 (0)