%%% BEGIN openflax/string.erl %%% %%% %%% openflax - Open Source web server for Erlang/OTP %%% Copyright (c)2004 Cat's Eye Technologies. All rights reserved. %%% %%% Redistribution and use in source and binary forms, with or without %%% modification, are permitted provided that the following conditions %%% are met: %%% %%% Redistributions of source code must retain the above copyright %%% notice, this list of conditions and the following disclaimer. %%% %%% Redistributions in binary form must reproduce the above copyright %%% notice, this list of conditions and the following disclaimer in %%% the documentation and/or other materials provided with the %%% distribution. %%% %%% Neither the name of Cat's Eye Technologies nor the names of its %%% contributors may be used to endorse or promote products derived %%% from this software without specific prior written permission. %%% %%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND %%% CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, %%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF %%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE %%% DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE %%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, %%% OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, %%% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, %%% OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON %%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, %%% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY %%% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE %%% POSSIBILITY OF SUCH DAMAGE. %% @doc OpenFlax string functions. %% %%
These are functions for manipulating strings used by OpenFlax.
%% They should only be used by OpenFlax; for general-purpose versions
%% of these functions, see the ce_string, ce_lists
%% and ce_lib modules of the ce application.
content_type.)
from_header(String) ->
substitute($-, $_, to_lower(String)).
%% @spec to_header(string()) -> string()
%% @doc Given an string such as "content_type", returns a string suitable
%% for use as an HTTP-header field name ("Content-Type".)
to_header(String) ->
substitute($_, $-, to_caps(String)).
%% @spec from_term(term()) -> string()
%% @doc Tries to convert any term into a string.
from_term(F) when is_float(F) ->
float_to_list(F);
from_term(I) when is_integer(I) ->
integer_to_list(I);
from_term(A) when is_atom(A) ->
atom_to_list(A);
from_term(S) when is_list(S) ->
S;
from_term(X) ->
io_lib:format("~w", [X]).
%% @spec to_atom(string()) -> atom()
%% @doc Tries to convert a string into an atom.
to_atom(String) ->
list_to_atom(lists:flatten(String)).
%% @spec decode_hex(string()) -> integer()
%% @doc Parses the given string as an integer in hexadecimal notation.
decode_hex(String) ->
decode_hex(String, 0).
decode_hex([], Acc) ->
Acc;
decode_hex([Head | Tail], Acc) ->
decode_hex(Tail, Acc * 16 + decode_hex_char(Head)).
decode_hex_char(Digit) when Digit >= $0, Digit =< $9 ->
Digit - $0;
decode_hex_char(Digit) when Digit >= $a, Digit =< $z ->
(Digit - $a) + 10;
decode_hex_char(Digit) when Digit >= $A, Digit =< $Z ->
(Digit - $A) + 10.
%% @spec from_datetime_rfc_1123() -> string()
%% @equiv from_datetime_rfc_1123(calendar:universal_time())
from_datetime_rfc_1123() -> from_datetime_rfc_1123(calendar:universal_time()).
%% @spec from_datetime_rfc_1123({date(), time()}) -> string()
%% @doc Returns a date/time string formatted in accordance to RFC 1123.
%% This sort of date/time looks like "Sun, 06 Nov 1994 08:23:19 GMT".
from_datetime_rfc_1123({{Y, M, D}, {H, I, S}}) ->
Dow = case calendar:day_of_the_week(Y, M, D) of
1 -> "Mon";
2 -> "Tue";
3 -> "Wed";
4 -> "Thu";
5 -> "Fri";
6 -> "Sat";
7 -> "Sun"
end,
Month = case M of
1 -> "Jan";
2 -> "Feb";
3 -> "Mar";
4 -> "Apr";
5 -> "May";
6 -> "Jun";
7 -> "Jul";
8 -> "Aug";
9 -> "Sep";
10 -> "Oct";
11 -> "Nov";
12 -> "Dec"
end,
lists:flatten(io_lib:format(
"~s, ~2.2.0w ~s ~w ~2.2.0w:~2.2.0w:~2.2.0w GMT",
[Dow, D, Month, Y, H, I, S])).
%%% END of openflax/string.erl %%%