mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 04:30:25 +00:00
Rewrite list/tokens parsing with an added whitespace function
This commit is contained in:
parent
6223018212
commit
1a839954bb
1 changed files with 18 additions and 27 deletions
|
@ -44,53 +44,44 @@ list(Data, Fun) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec list(binary(), fun(), [binary()]) -> [any()] | {error, badarg}.
|
-spec list(binary(), fun(), [binary()]) -> [any()] | {error, badarg}.
|
||||||
list(<<>>, _Fun, Acc) ->
|
|
||||||
Acc;
|
|
||||||
%% From the RFC:
|
%% From the RFC:
|
||||||
%% <blockquote>Wherever this construct is used, null elements are allowed,
|
%% <blockquote>Wherever this construct is used, null elements are allowed,
|
||||||
%% but do not contribute to the count of elements present.
|
%% but do not contribute to the count of elements present.
|
||||||
%% That is, "(element), , (element) " is permitted, but counts
|
%% That is, "(element), , (element) " is permitted, but counts
|
||||||
%% as only two elements. Therefore, where at least one element is required,
|
%% as only two elements. Therefore, where at least one element is required,
|
||||||
%% at least one non-null element MUST be present.</blockquote>
|
%% at least one non-null element MUST be present.</blockquote>
|
||||||
list(<< $,, Rest/bits >>, Fun, Acc) ->
|
|
||||||
list(Rest, Fun, Acc);
|
|
||||||
list(Data, Fun, Acc) ->
|
list(Data, Fun, Acc) ->
|
||||||
Fun(Data,
|
whitespace(Data,
|
||||||
fun (R, <<>>) -> list_separator(R,
|
fun (<<>>) -> Acc;
|
||||||
fun (D) -> list(D, Fun, Acc) end);
|
(<< $,, Rest/bits >>) -> list(Rest, Fun, Acc);
|
||||||
(R, I) -> list_separator(R,
|
(Rest) -> Fun(Rest,
|
||||||
fun (D) -> list(D, Fun, [I|Acc]) end)
|
fun (D, I) -> whitespace(D,
|
||||||
|
fun (<<>>) -> [I|Acc];
|
||||||
|
(<< $,, R/bits >>) -> list(R, Fun, [I|Acc]);
|
||||||
|
(_Any) -> {error, badarg}
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
-spec list_separator(binary(), fun()) -> any().
|
%% @doc Skip whitespace.
|
||||||
list_separator(<<>>, Fun) ->
|
-spec whitespace(binary(), fun()) -> any().
|
||||||
Fun(<<>>);
|
whitespace(<< C, Rest/bits >>, Fun)
|
||||||
list_separator(<< $,, Rest/bits >>, Fun) ->
|
|
||||||
Fun(Rest);
|
|
||||||
list_separator(<< C, Rest/bits >>, Fun)
|
|
||||||
when C =:= $\s; C =:= $\t ->
|
when C =:= $\s; C =:= $\t ->
|
||||||
list_separator(Rest, Fun);
|
whitespace(Rest, Fun);
|
||||||
list_separator(_Data, _Fun) ->
|
whitespace(Data, Fun) ->
|
||||||
{error, badarg}.
|
Fun(Data).
|
||||||
|
|
||||||
%% @doc Parse a case-insensitive token.
|
%% @doc Parse a case-insensitive token.
|
||||||
%%
|
%%
|
||||||
%% Changes all characters to lowercase.
|
%% Changes all characters to lowercase.
|
||||||
-spec token_ci(binary(), fun()) -> any().
|
-spec token_ci(binary(), fun()) -> any().
|
||||||
token_ci(Data, Fun) ->
|
token_ci(Data, Fun) ->
|
||||||
token(Data, Fun, ci).
|
token(Data, Fun, ci, <<>>).
|
||||||
|
|
||||||
%% @doc Parse a token.
|
%% @doc Parse a token.
|
||||||
-spec token(binary(), fun()) -> any().
|
-spec token(binary(), fun()) -> any().
|
||||||
token(Data, Fun) ->
|
token(Data, Fun) ->
|
||||||
token(Data, Fun, cs).
|
token(Data, Fun, cs, <<>>).
|
||||||
|
|
||||||
-spec token(binary(), fun(), ci | cs) -> any().
|
|
||||||
token(<< C, Rest/bits >>, Fun, Case)
|
|
||||||
when C =:= $\s; C =:= $\t ->
|
|
||||||
token(Rest, Fun, Case);
|
|
||||||
token(Data, Fun, Case) ->
|
|
||||||
token(Data, Fun, Case, <<>>).
|
|
||||||
|
|
||||||
-spec token(binary(), fun(), ci | cs, binary()) -> any().
|
-spec token(binary(), fun(), ci | cs, binary()) -> any().
|
||||||
token(<<>>, Fun, _Case, Acc) ->
|
token(<<>>, Fun, _Case, Acc) ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue