Skip to content

Commit

Permalink
Make whitespace (more) optional in list strings (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwinchester authored Jun 12, 2023
1 parent 71f37c9 commit c30c454
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/plug/conn/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ defmodule Plug.Conn.Utils do
iex> list("empties, , are,, filtered")
["empties", "are", "filtered"]
iex> list("whitespace , , ,, is ,definitely,optional")
["whitespace", "is", "definitely", "optional"]
"""
@spec list(binary) :: [binary]
def list(binary) do
Expand Down Expand Up @@ -300,7 +303,17 @@ defmodule Plug.Conn.Utils do

defp strip_spaces("\r\n" <> t), do: strip_spaces(t)
defp strip_spaces(<<h, t::binary>>) when h in [?\s, ?\t], do: strip_spaces(t)
defp strip_spaces(t), do: t
defp strip_spaces(t), do: trim_trailing(t)

defp trim_trailing(binary), do: trim_trailing(binary, byte_size(binary))

defp trim_trailing(binary, pos) do
if pos > 0 and :binary.at(binary, pos - 1) in [?\s, ?\t] do
trim_trailing(binary, pos - 1)
else
binary_part(binary, 0, pos)
end
end

defp downcase_char(char) when char in @upper, do: char + 32
defp downcase_char(char), do: char
Expand Down

0 comments on commit c30c454

Please sign in to comment.