Created
June 11, 2009 14:24
-
-
Save daniello/127917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% copied from Mochiweb's mochiweb_uitl.erl | |
-module(url_encoder). | |
-export([encode/1]). | |
-define(PERCENT, 37). % $\% | |
-define(FULLSTOP, 46). % $\. | |
-define(IS_HEX(C), ((C >= $0 andalso C =< $9) orelse | |
(C >= $a andalso C =< $f) orelse | |
(C >= $A andalso C =< $F))). | |
-define(QS_SAFE(C), ((C >= $a andalso C =< $z) orelse | |
(C >= $A andalso C =< $Z) orelse | |
(C >= $0 andalso C =< $9) orelse | |
(C =:= ?FULLSTOP orelse C =:= $- orelse C =:= $~ orelse | |
C =:= $_))). | |
encode(S) -> quote_plus(S). | |
%% @spec quote_plus(atom() | integer() | float() | string() | binary()) -> string() | |
%% @doc URL safe encoding of the given term. | |
quote_plus(Atom) when is_atom(Atom) -> | |
quote_plus(atom_to_list(Atom)); | |
quote_plus(Int) when is_integer(Int) -> | |
quote_plus(integer_to_list(Int)); | |
quote_plus(Binary) when is_binary(Binary) -> | |
quote_plus(binary_to_list(Binary)); | |
quote_plus(Float) when is_float(Float) -> | |
quote_plus(mochinum:digits(Float)); | |
quote_plus(String) -> | |
quote_plus(String, []). | |
quote_plus([], Acc) -> | |
lists:reverse(Acc); | |
quote_plus([C | Rest], Acc) when ?QS_SAFE(C) -> | |
quote_plus(Rest, [C | Acc]); | |
quote_plus([$\s | Rest], Acc) -> | |
quote_plus(Rest, [$+ | Acc]); | |
quote_plus([C | Rest], Acc) -> | |
<<Hi:4, Lo:4>> = <<C>>, | |
quote_plus(Rest, [hexdigit(Lo), hexdigit(Hi), ?PERCENT | Acc]). | |
hexdigit(C) when C < 10 -> $0 + C; | |
hexdigit(C) when C < 16 -> $A + (C - 10). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment