Skip to content

Instantly share code, notes, and snippets.

@m-2k
Created January 15, 2015 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-2k/d3bdcd768386598f1f1f to your computer and use it in GitHub Desktop.
Save m-2k/d3bdcd768386598f1f1f to your computer and use it in GitHub Desktop.
% Tail recursion
html_encode_utf8(<<>>, Acc) -> Acc;
html_encode_utf8(<<U,T/binary>>, Acc) when U =< 127 andalso U >= 0 ->
R = case U of
$& -> <<"&amp;">>;
$< -> <<"&lt;">>;
$> -> <<"&gt;">>;
$" -> <<"&quot;">>;
$' -> <<"&#x27;">>;
$/ -> <<"&#x2F;">>;
$\s -> <<"&nbsp;">>;
$\t -> <<"&nbsp;&nbsp;&nbsp;&nbsp;">>;
$\n -> <<"<br/>">>;
$\r -> <<>>;
_ -> <<U>>
end,
html_encode_utf8(T, <<Acc/binary,R/binary>>);
html_encode_utf8(<<U,T/binary>>, Acc) -> html_encode_utf8(T, <<Acc/binary,U>>).
html_encode_utf8(B) when is_binary(B) -> html_encode_utf8(B, <<>>);
html_encode_utf8(B) when is_list(B) -> html_encode_utf8(list_to_binary(B), <<>>).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment