Skip to content

Commit 99bd9bd

Browse files
authored
Replace downcase_ascii/1 implementation with String.downcase/2 (#424)
Also moved "downcase_ascii_char/1" to "Mint.HTTP1.Parse" as it's the only module that uses it now.
1 parent a0afdce commit 99bd9bd

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

lib/mint/core/util.ex

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,6 @@ defmodule Mint.Core.Util do
107107
end
108108
end
109109

110-
# Lowercases an ASCII string more efficiently than
111-
# String.downcase/1.
112-
@spec downcase_ascii(String.t()) :: String.t()
113-
def downcase_ascii(string) do
114-
for <<char <- string>>, do: <<downcase_ascii_char(char)>>, into: ""
115-
end
116-
117-
@spec downcase_ascii_char(byte()) :: byte()
118-
def downcase_ascii_char(char) when char in ?A..?Z, do: char + 32
119-
def downcase_ascii_char(char) when char in 0..127, do: char
120-
121110
# If the buffer is empty, reusing the incoming data saves
122111
# a potentially large allocation of memory.
123112
# This should be fixed in a subsequent OTP release.

lib/mint/http1.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ defmodule Mint.HTTP1 do
973973
end
974974

975975
defp lower_header_keys(headers) do
976-
for {name, value} <- headers, do: {Util.downcase_ascii(name), value}
976+
for {name, value} <- headers, do: {String.downcase(name, :ascii), value}
977977
end
978978

979979
defp add_default_headers(headers, conn) do

lib/mint/http1/parse.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ defmodule Mint.HTTP1.Parse do
5555
defp token_list_downcase(rest, acc), do: token_downcase(rest, _token_acc = <<>>, acc)
5656

5757
defp token_downcase(<<char, rest::binary>>, token_acc, acc) when is_tchar(char),
58-
do: token_downcase(rest, <<token_acc::binary, Util.downcase_ascii_char(char)>>, acc)
58+
do: token_downcase(rest, <<token_acc::binary, downcase_ascii_char(char)>>, acc)
5959

6060
defp token_downcase(rest, token_acc, acc), do: token_list_sep_downcase(rest, [token_acc | acc])
6161

@@ -68,4 +68,7 @@ defmodule Mint.HTTP1.Parse do
6868
do: token_list_downcase(rest, acc)
6969

7070
defp token_list_sep_downcase(_rest, _acc), do: :error
71+
72+
defp downcase_ascii_char(char) when char in ?A..?Z, do: char + 32
73+
defp downcase_ascii_char(char) when char in 0..127, do: char
7174
end

lib/mint/http1/response.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ defmodule Mint.HTTP1.Response do
3838
end
3939
end
4040

41-
defp header_name(atom) when is_atom(atom), do: atom |> Atom.to_string() |> Util.downcase_ascii()
42-
defp header_name(binary) when is_binary(binary), do: Util.downcase_ascii(binary)
41+
defp header_name(atom) when is_atom(atom) do
42+
atom
43+
|> Atom.to_string()
44+
|> String.downcase(:ascii)
45+
end
46+
47+
defp header_name(binary) when is_binary(binary), do: String.downcase(binary, :ascii)
4348
end

lib/mint/http2.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ defmodule Mint.HTTP2 do
13451345
end
13461346

13471347
defp downcase_header_names(headers) do
1348-
for {name, value} <- headers, do: {Util.downcase_ascii(name), value}
1348+
for {name, value} <- headers, do: {String.downcase(name, :ascii), value}
13491349
end
13501350

13511351
defp add_default_headers(headers, body) do
@@ -1746,7 +1746,7 @@ defmodule Mint.HTTP2 do
17461746

17471747
defp join_cookie_headers(headers) do
17481748
# If we have 0 or 1 Cookie headers, we just use the old list of headers.
1749-
case Enum.split_with(headers, fn {name, _value} -> Util.downcase_ascii(name) == "cookie" end) do
1749+
case Enum.split_with(headers, fn {name, _value} -> String.downcase(name, :ascii) == "cookie" end) do
17501750
{[], _headers} ->
17511751
headers
17521752

0 commit comments

Comments
 (0)