Skip to content

Instantly share code, notes, and snippets.

@krestenkrab
Created November 17, 2011 14:37
Show Gist options
  • Save krestenkrab/1373280 to your computer and use it in GitHub Desktop.
Save krestenkrab/1373280 to your computer and use it in GitHub Desktop.
Parse a HTTP token-list
%%
%% Parse a comma-separated tokenlist
%%
%% the string: a, b , "c\" ", , d
%% gives : {ok, [<<"a">>, <<"b">>, <<"c\" >>, <<>>, <<"d">>]}
%%
-spec parse_tokenlist(string()) -> malformed | {ok, [binary()]}.
parse_tokenlist(String) ->
parse_tokenlist(String, []).
parse_tokenlist([$ |Rest], Out) ->
parse_tokenlist(Rest, Out);
parse_tokenlist([$,|Rest], Out) ->
parse_tokenlist(Rest, [<<"">>|Out]);
parse_tokenlist([$"|Rest], Out) ->
parse_quoted_string(Rest, [], Out);
parse_tokenlist([], Out) ->
{ok, Out};
parse_tokenlist(Str, Out) ->
parse_token(Str,[],Out).
parse_token([$,|Rest], Str, Out) ->
parse_tokenlist(Rest, [list_to_binary(list:reverse(Str)) | Out]);
parse_token([CH|Rest], Str, Out) ->
parse_token(Rest, [CH|Str], Out);
parse_token([], Str, Out) ->
{ok, [list_to_binary(lists:reverse(Str)) | Out ]}.
parse_quoted_string([$"|Rest], Str, Out) ->
parse_after_quoted_string(Rest, [list_to_binary(lists:reverse(Str)) | Out]);
parse_quoted_string([$\, CH|Rest], Str, Out) ->
parse_quoted_string(Rest, [CH|Str], Out);
parse_quoted_string([CH|Rest], Str, Out) ->
parse_quoted_string(Rest, [CH|Str], Out);
parse_quoted_string([], _, _) ->
malformed.
parse_after_quoted_string([$ |Rest], Out) ->
parse_after_quoted_string(Rest, Out);
parse_after_quoted_string([$,|Rest], Out) ->
parse_tokenlist(Rest, Out);
parse_after_quoted_string([], Out) ->
{ok, Out}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment