Browse Source

move to nitro

221V 3 years ago
parent
commit
8b46f3a0ff
2 changed files with 84 additions and 121 deletions
  1. 82 1
      src/nitro.erl
  2. 2 120
      src/nitro_conv.erl

+ 82 - 1
src/nitro.erl

@@ -102,7 +102,34 @@ temp_id() ->
   "auto" ++ integer_to_list(unique_integer() rem 1000000).
 
 
-%% JavaScript encode/decode
+% Fast HEX
+
+%digit(0) -> $0;
+%digit(1) -> $1;
+%digit(2) -> $2;
+%digit(3) -> $3;
+%digit(4) -> $4;
+%digit(5) -> $5;
+%digit(6) -> $6;
+%digit(7) -> $7;
+%digit(8) -> $8;
+%digit(9) -> $9;
+%digit(10) -> $a;
+%digit(11) -> $b;
+%digit(12) -> $c;
+%digit(13) -> $d;
+%digit(14) -> $e;
+%digit(15) -> $f.
+digit(X) when X >= 0 andalso X =< 9 -> X + 48;
+digit(X) when X >= 10 andalso X =< 15 -> X + 87.
+
+hex(Bin) ->
+  << << (digit(A1)), (digit(A2)) >> || <<A1:4, A2:4>> <= Bin >>.
+unhex(Hex) ->
+  << << (erlang:list_to_integer([H1, H2], 16)) >> || <<H1, H2>> <= Hex >>.
+
+
+%% JavaScript escape
 
 js_escape(undefined) -> [];
 js_escape(Value) when erlang:is_list(Value) ->
@@ -192,6 +219,60 @@ html_encode_whites([H|T]) ->
     _ -> [H|html_encode_whites(T)]
   end.
 
+
+%% URL encode/decode
+
+-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 =:= $_))).
+
+url_encode(Atom) when erlang:is_atom(Atom) ->
+  url_encode(erlang:atom_to_list(Atom));
+url_encode(Int) when erlang:is_integer(Int) ->
+  url_encode(erlang:integer_to_list(Int));
+url_encode(Bin) when erlang:is_binary(Bin) ->
+  url_encode(erlang:binary_to_list(Bin));
+url_encode(String) ->
+  url_encode(String, []).
+
+url_encode([], Acc) ->
+  lists:reverse(Acc);
+url_encode([C | Rest], Acc) when ?QS_SAFE(C) ->
+  url_encode(Rest, [C | Acc]);
+url_encode([$\s | Rest], Acc) ->
+  url_encode(Rest, [$+ | Acc]);
+url_encode([C | Rest], Acc) ->
+  <<Hi:4, Lo:4>> = <<C>>,
+  url_encode(Rest, [digit(Lo), digit(Hi), ?PERCENT | Acc]).
+
+url_decode(Binary) when erlang:is_binary(Binary) ->
+  url_decode(erlang:binary_to_list(Binary));
+url_decode(String) -> url_decode_h(lists:reverse(String)).
+
+unhexdigit(C) when C >= $0, C =< $9 ->
+  C - $0;
+unhexdigit(C) when C >= $a, C =< $f ->
+  C - $a + 10;
+unhexdigit(C) when C >= $A, C =< $F ->
+  C - $A + 10.
+
+url_decode_h(S) -> url_decode_h(S, []).
+
+url_decode_h([], Acc) -> Acc;
+url_decode_h([$+ | Rest], Acc) ->
+  url_decode_h(Rest, [$\s | Acc]);
+url_decode_h([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) ->
+  url_decode_h(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]);
+url_decode_h([C | Rest], Acc) -> url_decode_h(Rest, [C | Acc]).
+
+
 script() -> erlang:get(script).
 script(Script) -> erlang:put(script, Script).
 

+ 2 - 120
src/nitro_conv.erl

@@ -6,129 +6,12 @@
 
 -compile([export_all, nowarn_export_all]). % todo export
 
-
-% to_atom to_list to_binary
-
--define(IS_STRING(Term), (erlang:is_list(Term) andalso Term /= [] andalso erlang:is_integer(erlang:hd(Term)))).
-
--ifndef(N2Z_JSON).
--define(N2Z_JSON, (application:get_env(n2z, json, jsone))).
--endif.
-
-
-to_list(L) when ?IS_STRING(L) -> L;
-to_list(L) when erlang:is_list(L) ->
-  SubLists = [inner_to_list(X) || X <- L],
-  lists:flatten(SubLists);
-to_list(A) -> inner_to_list(A).
-
-inner_to_list(A) when erlang:is_atom(A) -> erlang:atom_to_list(A);
-inner_to_list(B) when erlang:is_binary(B) -> erlang:binary_to_list(B);
-inner_to_list(I) when erlang:is_integer(I) -> erlang:integer_to_list(I);
-inner_to_list(L) when erlang:is_tuple(L) -> lists:flatten(io_lib:format("~p", [L]));
-inner_to_list(L) when erlang:is_list(L) -> L;
-inner_to_list(F) when erlang:is_float(F) -> erlang:float_to_list(F, [{decimals, 9}, compact]).
-
-to_atom(A) when erlang:is_atom(A) -> A;
-to_atom(B) when erlang:is_binary(B) -> to_atom(erlang:binary_to_list(B));
-to_atom(I) when erlang:is_integer(I) -> to_atom(erlang:integer_to_list(I));
-to_atom(F) when erlang:is_float(F) -> to_atom(erlang:float_to_list(F, [{decimals, 9}, compact]));
-to_atom(L) when erlang:is_list(L) -> erlang:list_to_atom(erlang:binary_to_list(erlang:list_to_binary(L))).
-
-to_binary(A) when erlang:is_atom(A) -> erlang:atom_to_binary(A,latin1);
-to_binary(B) when erlang:is_binary(B) -> B;
-to_binary(T) when erlang:is_tuple(T) -> erlang:term_to_binary(T);
-to_binary(I) when erlang:is_integer(I) -> to_binary(erlang:integer_to_list(I));
-to_binary(F) when erlang:is_float(F) -> float_to_binary(F, [{decimals, 9}, compact]);
-to_binary(L) when erlang:is_list(L) ->  erlang:iolist_to_binary(L).
-
-to_integer(A) when erlang:is_atom(A) -> to_integer(erlang:atom_to_list(A));
-to_integer(B) when erlang:is_binary(B) -> to_integer(erlang:binary_to_list(B));
-to_integer(I) when erlang:is_integer(I) -> I;
-to_integer([]) -> 0;
-to_integer(L) when erlang:is_list(L) -> erlang:list_to_integer(L);
-to_integer(F) when erlang:is_float(F) -> erlang:round(F).
-
-
-%% URL encode/decode
-
-url_encode(S) -> quote_plus(S).
-url_decode(S) -> unquote(S).
-
--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 =:= $_))).
-
-quote_plus(Atom) when erlang:is_atom(Atom) ->
-  quote_plus(erlang:atom_to_list(Atom));
-quote_plus(Int) when erlang:is_integer(Int) ->
-  quote_plus(erlang:integer_to_list(Int));
-quote_plus(Bin) when erlang:is_binary(Bin) ->
-  quote_plus(erlang:binary_to_list(Bin));
-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, [digit(Lo), digit(Hi), ?PERCENT | Acc]).
-
-unquote(Binary) when erlang:is_binary(Binary) ->
-  unquote(erlang:binary_to_list(Binary));
-unquote(String) -> qs_revdecode(lists:reverse(String)).
-
-unhexdigit(C) when C >= $0, C =< $9 ->
-  C - $0;
-unhexdigit(C) when C >= $a, C =< $f ->
-  C - $a + 10;
-unhexdigit(C) when C >= $A, C =< $F ->
-  C - $A + 10.
-
-qs_revdecode(S) -> qs_revdecode(S, []).
-qs_revdecode([], Acc) -> Acc;
-qs_revdecode([$+ | Rest], Acc) ->
-  qs_revdecode(Rest, [$\s | Acc]);
-qs_revdecode([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) ->
-  qs_revdecode(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]);
-qs_revdecode([C | Rest], Acc) -> qs_revdecode(Rest, [C | Acc]).
-
 % JOIN
 
 join([],_) -> [];
 join([Item], _Delim) -> [Item];
 join([Item|Items], Delim) -> [Item, Delim | join(Items,Delim)].
 
-% Fast HEX
-
-digit(0) -> $0;
-digit(1) -> $1;
-digit(2) -> $2;
-digit(3) -> $3;
-digit(4) -> $4;
-digit(5) -> $5;
-digit(6) -> $6;
-digit(7) -> $7;
-digit(8) -> $8;
-digit(9) -> $9;
-digit(10) -> $a;
-digit(11) -> $b;
-digit(12) -> $c;
-digit(13) -> $d;
-digit(14) -> $e;
-digit(15) -> $f.
-
-hex(Bin) ->
-  << << (digit(A1)), (digit(A2)) >> || <<A1:4, A2:4>> <= Bin >>.
-unhex(Hex) ->
-  << << (erlang:list_to_integer([H1, H2], 16)) >> || <<H1, H2>> <= Hex >>.
 
 f(S) -> f(S, []).
 f(S, Args) -> lists:flatten(io_lib:format(S, Args)).
@@ -173,9 +56,8 @@ pickle(Data) ->
 
 depickle(PickledData) ->
   try
-  {Data, _PickleTime} = erlang:binary_to_term(base64:decode(nitro:to_binary(PickledData))),
-  Data
-  
+    {Data, _PickleTime} = erlang:binary_to_term(base64:decode(nitro:to_binary(PickledData))),
+    Data
   catch _:_ ->
     undefined
   end.