Erlang-Parser
view release on metacpan or search on metacpan
dehex(C) when C >= $A, C =< $F ->
C - $A + 10.
%% @spec hexdigit(integer()) -> char()
%% @doc Convert an integer less than 16 to a hex digit.
hexdigit(C) when C >= 0, C =< 9 ->
C + $0;
hexdigit(C) when C =< 15 ->
C + $a - 10.
%% Internal API
to_hex(<<>>, Acc) ->
lists:reverse(Acc);
to_hex(<<C1:4, C2:4, Rest/binary>>, Acc) ->
to_hex(Rest, [hexdigit(C2), hexdigit(C1) | Acc]).
to_hex_int(0, Acc) ->
Acc;
to_hex_int(I, Acc) ->
to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]).
to_bin([], Acc) ->
iolist_to_binary(lists:reverse(Acc));
to_bin([C1, C2 | Rest], Acc) ->
to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]).
%%
%% Tests
%%
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
to_hex_test() ->
"ff000ff1" = to_hex([255, 0, 15, 241]),
"ff000ff1" = to_hex(16#ff000ff1),
"0" = to_hex(16#0),
ok.
to_bin_test() ->
<<255, 0, 15, 241>> = to_bin("ff000ff1"),
<<255, 0, 10, 161>> = to_bin("Ff000aA1"),
ok.
to_int_test() ->
16#ff000ff1 = to_int("ff000ff1"),
16#ff000aa1 = to_int("FF000Aa1"),
16#0 = to_int("0"),
ok.
-endif.
%% @author Bob Ippolito <bob@mochimedia.com>
%% @copyright 2007 Mochi Media, Inc.
%% @doc Yet another JSON (RFC 4627) library for Erlang. mochijson2 works
%% with binaries as strings, arrays as lists (without an {array, _})
%% wrapper and it only knows how to decode UTF-8 (and ASCII).
%%
%% JSON terms are decoded as follows (javascript -> erlang):
%% <ul>
%% <li>{"key": "value"} ->
%% {struct, [{<<"key">>, <<"value">>}]}</li>
%% <li>["array", 123, 12.34, true, false, null] ->
%% [<<"array">>, 123, 12.34, true, false, null]
%% </li>
%% </ul>
%% <ul>
%% <li>Strings in JSON decode to UTF-8 binaries in Erlang</li>
%% <li>Objects decode to {struct, PropList}</li>
%% <li>Numbers decode to integer or float</li>
%% <li>true, false, null decode to their respective terms.</li>
%% </ul>
%% The encoder will accept the same format that the decoder will produce,
%% but will also allow additional cases for leniency:
%% <ul>
%% <li>atoms other than true, false, null will be considered UTF-8
%% strings (even as a proplist key)
%% </li>
%% <li>{json, IoList} will insert IoList directly into the output
%% with no validation
%% </li>
%% <li>{array, Array} will be encoded as Array
%% (legacy mochijson style)
%% </li>
%% <li>A non-empty raw proplist will be encoded as an object as long
%% as the first pair does not have an atom key of json, struct,
%% or array
%% </li>
%% </ul>
-module(mochijson2).
-author('bob@mochimedia.com').
-export([encoder/1, encode/1]).
-export([decoder/1, decode/1, decode/2]).
%% This is a macro to placate syntax highlighters..
-define(Q, $\").
-define(ADV_COL(S, N), S#decoder{offset=N+S#decoder.offset,
column=N+S#decoder.column}).
-define(INC_COL(S), S#decoder{offset=1+S#decoder.offset,
column=1+S#decoder.column}).
-define(INC_LINE(S), S#decoder{offset=1+S#decoder.offset,
column=1,
line=1+S#decoder.line}).
-define(INC_CHAR(S, C),
case C of
$\n ->
S#decoder{column=1,
line=1+S#decoder.line,
offset=1+S#decoder.offset};
_ ->
S#decoder{column=1+S#decoder.column,
offset=1+S#decoder.offset}
end).
-define(IS_WHITESPACE(C),
(C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)).
%% @type iolist() = [char() | binary() | iolist()]
%% @type iodata() = iolist() | binary()
( run in 0.528 second using v1.01-cache-2.11-cpan-39bf76dae61 )