Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Util.downcase_ascii with String.downcase(&1, :ascii) #424

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions lib/mint/core/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,6 @@ defmodule Mint.Core.Util do
end
end

# Lowercases an ASCII string more efficiently than
# String.downcase/1.
@spec downcase_ascii(String.t()) :: String.t()
def downcase_ascii(string) do
for <<char <- string>>, do: <<downcase_ascii_char(char)>>, into: ""
end

@spec downcase_ascii_char(byte()) :: byte()
def downcase_ascii_char(char) when char in ?A..?Z, do: char + 32
def downcase_ascii_char(char) when char in 0..127, do: char

# If the buffer is empty, reusing the incoming data saves
# a potentially large allocation of memory.
# This should be fixed in a subsequent OTP release.
Expand Down
2 changes: 1 addition & 1 deletion lib/mint/http1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ defmodule Mint.HTTP1 do
end

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

defp add_default_headers(headers, conn) do
Expand Down
5 changes: 4 additions & 1 deletion lib/mint/http1/parse.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mint.HTTP1.Parse do
@moduledoc false

alias Mint.Core.Util

Check warning on line 4 in lib/mint/http1/parse.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.15, OTP 26.0.2)

unused alias Util

Check warning on line 4 in lib/mint/http1/parse.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.12, OTP 24.3)

unused alias Util

Check warning on line 4 in lib/mint/http1/parse.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.11, OTP 23.3.1)

unused alias Util

Check warning on line 4 in lib/mint/http1/parse.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.10, OTP 21.3)

unused alias Util

defmacro is_digit(char), do: quote(do: unquote(char) in ?0..?9)
defmacro is_alpha(char), do: quote(do: unquote(char) in ?a..?z or unquote(char) in ?A..?Z)
Expand Down Expand Up @@ -55,7 +55,7 @@
defp token_list_downcase(rest, acc), do: token_downcase(rest, _token_acc = <<>>, acc)

defp token_downcase(<<char, rest::binary>>, token_acc, acc) when is_tchar(char),
do: token_downcase(rest, <<token_acc::binary, Util.downcase_ascii_char(char)>>, acc)
do: token_downcase(rest, <<token_acc::binary, downcase_ascii_char(char)>>, acc)

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

Expand All @@ -68,4 +68,7 @@
do: token_list_downcase(rest, acc)

defp token_list_sep_downcase(_rest, _acc), do: :error

defp downcase_ascii_char(char) when char in ?A..?Z, do: char + 32
defp downcase_ascii_char(char) when char in 0..127, do: char
end
9 changes: 7 additions & 2 deletions lib/mint/http1/response.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mint.HTTP1.Response do
@moduledoc false

alias Mint.Core.Util

Check warning on line 4 in lib/mint/http1/response.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.15, OTP 26.0.2)

unused alias Util

Check warning on line 4 in lib/mint/http1/response.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.12, OTP 24.3)

unused alias Util

Check warning on line 4 in lib/mint/http1/response.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.11, OTP 23.3.1)

unused alias Util

Check warning on line 4 in lib/mint/http1/response.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.10, OTP 21.3)

unused alias Util

def decode_status_line(binary) do
case :erlang.decode_packet(:http_bin, binary, []) do
Expand Down Expand Up @@ -38,6 +38,11 @@
end
end

defp header_name(atom) when is_atom(atom), do: atom |> Atom.to_string() |> Util.downcase_ascii()
defp header_name(binary) when is_binary(binary), do: Util.downcase_ascii(binary)
defp header_name(atom) when is_atom(atom) do
atom
|> Atom.to_string()
|> String.downcase(:ascii)
end

defp header_name(binary) when is_binary(binary), do: String.downcase(binary, :ascii)
end
4 changes: 2 additions & 2 deletions lib/mint/http2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ defmodule Mint.HTTP2 do
end

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

defp add_default_headers(headers, body) do
Expand Down Expand Up @@ -1730,7 +1730,7 @@ defmodule Mint.HTTP2 do

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

Expand Down
Loading