nitro_conv.erl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. -module(nitro_conv).
  2. %%-author('Maxim Sokhatsky').
  3. %% N2O Formatter: JSON, BERT
  4. -compile(export_all).
  5. -include_lib("nitro/include/nitro.hrl").
  6. % WF to_list to_atom to_binary to_integer
  7. -define(IS_STRING(Term),
  8. (erlang:is_list(Term) andalso Term /= [] andalso erlang:is_integer(hd(Term)))).
  9. -ifndef(N2O_JSON).
  10. -define(N2O_JSON, (application:get_env(n2o,json,jsone))).
  11. -endif.
  12. to_list(L) when ?IS_STRING(L) -> L;
  13. to_list(L) when erlang:is_list(L) ->
  14. SubLists = [inner_to_list(X) || X <- L],
  15. lists:flatten(SubLists);
  16. to_list(A) -> inner_to_list(A).
  17. inner_to_list(A) when erlang:is_atom(A) -> erlang:atom_to_list(A);
  18. inner_to_list(B) when erlang:is_binary(B) -> erlang:binary_to_list(B);
  19. inner_to_list(I) when erlang:is_integer(I) -> erlang:integer_to_list(I);
  20. inner_to_list(L) when erlang:is_tuple(L) -> lists:flatten(io_lib:format("~p", [L]));
  21. inner_to_list(L) when erlang:is_list(L) -> L;
  22. inner_to_list(F) when erlang:is_float(F) -> erlang:float_to_list(F, [{decimals, 9}, compact]).
  23. to_atom(A) when erlang:is_atom(A) -> A;
  24. to_atom(B) when erlang:is_binary(B) -> erlang:binary_to_atom(B);
  25. to_atom(I) when erlang:is_integer(I) -> to_atom(erlang:integer_to_list(I));
  26. to_atom(F) when erlang:is_float(F) -> to_atom(erlang:float_to_list(F, [{decimals, 9}, compact]));
  27. to_atom(L) when erlang:is_list(L) -> erlang:list_to_atom(L).
  28. to_binary(A) when erlang:is_atom(A) -> erlang:atom_to_binary(A, latin1);
  29. to_binary(B) when erlang:is_binary(B) -> B;
  30. to_binary(I) when erlang:is_integer(I) -> erlang:integer_to_binary(I);
  31. to_binary(F) when erlang:is_float(F) -> erlang:float_to_binary(F, [{decimals, 9}, compact]);
  32. to_binary(L) when erlang:is_list(L) -> erlang:iolist_to_binary(L);
  33. to_binary(X) when erlang:is_tuple(X) -> erlang:term_to_binary(X).
  34. to_integer(A) when erlang:is_atom(A) -> to_integer(erlang:atom_to_list(A));
  35. to_integer(B) when erlang:is_binary(B) -> to_integer(erlang:binary_to_list(B));
  36. to_integer(I) when erlang:is_integer(I) -> I;
  37. to_integer([]) -> 0;
  38. to_integer(L) when erlang:is_list(L) -> erlang:list_to_integer(L);
  39. to_integer(F) when erlang:is_float(F) -> erlang:round(F).
  40. %% HTML encode/decode
  41. %% html_encode(B, normal)
  42. html_encode(L, Fun) when erlang:is_function(Fun) ->
  43. Fun(L);
  44. html_encode(L, EncType) when erlang:is_atom(L) ->
  45. html_encode(nitro:to_list(L), EncType);
  46. html_encode(L, EncType) when erlang:is_integer(L) ->
  47. html_encode(erlang:integer_to_list(L), EncType);
  48. html_encode(L, EncType) when erlang:is_float(L) ->
  49. html_encode(erlang:float_to_list(L, [{decimals, 9}, compact]), EncType);
  50. html_encode(L, false) ->
  51. L;
  52. html_encode(L, true) ->
  53. L;
  54. html_encode(L, whites) ->
  55. html_encode_whites(nitro:to_list(lists:flatten([L]))).
  56. html_encode(<<>>) ->
  57. [];
  58. html_encode([]) ->
  59. [];
  60. html_encode(B) when is_binary(B) ->
  61. nitro:to_binary(
  62. html_encode(erlang:binary_to_list(B))
  63. );
  64. html_encode([$\n |T]) ->
  65. "<br>" ++ html_encode(T);
  66. html_encode([H|T]) ->
  67. case H of
  68. $< -> "&lt;" ++ html_encode(T);
  69. $> -> "&gt;" ++ html_encode(T);
  70. $" -> "&quot;" ++ html_encode(T);
  71. $' -> "&#39;" ++ html_encode(T);
  72. $& -> "&amp;" ++ html_encode(T);
  73. $\\ -> "&#92;" ++ html_encode(T);
  74. BigNum when erlang:is_integer(BigNum) andalso BigNum > 255 ->
  75. %% Any integers above 255 are converted to their HTML encode equivalent
  76. %% Example: 7534 gets turned into &#7534;
  77. [$&, $# | nitro:to_list(BigNum)] ++ ";" ++ html_encode(T);
  78. Tup when erlang:is_tuple(Tup) ->
  79. erlang:throw({html_encode, encountered_tuple, Tup});
  80. _ ->
  81. [H|html_encode(T)]
  82. end.
  83. html_encode_whites([]) -> [];
  84. html_encode_whites([H|T]) ->
  85. case H of
  86. $\s -> "&nbsp;" ++ html_encode_whites(T);
  87. $\t -> "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" ++ html_encode_whites(T);
  88. $< -> "&lt;" ++ html_encode_whites(T);
  89. $> -> "&gt;" ++ html_encode_whites(T);
  90. $" -> "&quot;" ++ html_encode_whites(T);
  91. $' -> "&#39;" ++ html_encode_whites(T);
  92. $& -> "&amp;" ++ html_encode_whites(T);
  93. $\n -> "<br>" ++ html_encode_whites(T);
  94. $\\ -> "&#92;" ++ html_encode_whites(T);
  95. _ -> [H|html_encode_whites(T)]
  96. end.
  97. %% URL encode/decode
  98. -define(PERCENT, 37). % $\%
  99. -define(FULLSTOP, 46). % $\.
  100. -define(IS_HEX(C), ((C >= $0 andalso C =< $9) orelse
  101. (C >= $a andalso C =< $f) orelse
  102. (C >= $A andalso C =< $F))).
  103. -define(QS_SAFE(C), ((C >= $a andalso C =< $z) orelse
  104. (C >= $A andalso C =< $Z) orelse
  105. (C >= $0 andalso C =< $9) orelse
  106. (C =:= ?FULLSTOP orelse C =:= $- orelse
  107. C =:= $~ orelse C =:= $_))).
  108. url_encode(Atom) when erlang:is_atom(Atom) ->
  109. url_encode(erlang:atom_to_list(Atom));
  110. url_encode(Int) when erlang:is_integer(Int) ->
  111. url_encode(erlang:integer_to_list(Int));
  112. url_encode(Bin) when erlang:is_binary(Bin) ->
  113. url_encode(erlang:binary_to_list(Bin));
  114. url_encode(String) ->
  115. url_encode(String, []).
  116. url_encode([], Acc) ->
  117. lists:reverse(Acc);
  118. url_encode([C | Rest], Acc) when ?QS_SAFE(C) ->
  119. url_encode(Rest, [C | Acc]);
  120. url_encode([$\s | Rest], Acc) ->
  121. url_encode(Rest, [$+ | Acc]);
  122. url_encode([C | Rest], Acc) ->
  123. <<Hi:4, Lo:4>> = <<C>>,
  124. url_encode(Rest, [digit(Lo), digit(Hi), ?PERCENT | Acc]).
  125. url_decode(Binary) when erlang:is_binary(Binary) ->
  126. url_decode(erlang:binary_to_list(Binary));
  127. url_decode(String) -> url_decode_h(lists:reverse(String)).
  128. unhexdigit(C) when C >= $0, C =< $9 ->
  129. C - $0;
  130. unhexdigit(C) when C >= $a, C =< $f ->
  131. C - $a + 10;
  132. unhexdigit(C) when C >= $A, C =< $F ->
  133. C - $A + 10.
  134. url_decode_h(S) -> url_decode_h(S, []).
  135. url_decode_h([], Acc) -> Acc;
  136. url_decode_h([$+ | Rest], Acc) ->
  137. url_decode_h(Rest, [$\s | Acc]);
  138. url_decode_h([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) ->
  139. url_decode_h(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]);
  140. url_decode_h([C | Rest], Acc) -> url_decode_h(Rest, [C | Acc]).
  141. %% JavaScript escape
  142. js_escape(undefined) -> [];
  143. js_escape(Value) when erlang:is_list(Value) ->
  144. erlang:binary_to_list( js_escape( erlang:iolist_to_binary(Value) ) );
  145. js_escape(Value) -> js_escape(Value, <<>>).
  146. js_escape(<<"\\", Rest/binary>>, Acc) -> %"
  147. js_escape(Rest, <<Acc/binary, "\\\\">>); %"
  148. js_escape(<<"\r", Rest/binary>>, Acc) ->
  149. js_escape(Rest, <<Acc/binary, "\\r">>);
  150. js_escape(<<"\n", Rest/binary>>, Acc) ->
  151. js_escape(Rest, <<Acc/binary, "\\n">>);
  152. js_escape(<<"\"", Rest/binary>>, Acc) ->
  153. js_escape(Rest, <<Acc/binary, "\\\"">>);
  154. js_escape(<<"'", Rest/binary>>, Acc) ->
  155. js_escape(Rest, <<Acc/binary, "\\'">>);
  156. js_escape(<<"`", Rest/binary>>, Acc) ->
  157. js_escape(Rest, <<Acc/binary, "\\`">>);
  158. js_escape(<<"<script", Rest/binary>>, Acc) ->
  159. js_escape(Rest, <<Acc/binary, "<script">>);
  160. js_escape(<<"script>", Rest/binary>>, Acc) ->
  161. js_escape(Rest, <<Acc/binary, "script>">>);
  162. js_escape(<<C, Rest/binary>>, Acc) ->
  163. js_escape(Rest, <<Acc/binary, C>>);
  164. js_escape(<<>>, Acc) -> Acc.
  165. %% JOIN
  166. %% join(List, Delimiter)
  167. join([], _) -> [];
  168. join([Item], _Delim) -> [Item];
  169. join([Item|Items], Delim) ->
  170. [Item, Delim | join(Items, Delim)].
  171. %% Fast HEX
  172. %digit(0) -> $0;
  173. %digit(1) -> $1;
  174. %digit(2) -> $2;
  175. %digit(3) -> $3;
  176. %digit(4) -> $4;
  177. %digit(5) -> $5;
  178. %digit(6) -> $6;
  179. %digit(7) -> $7;
  180. %digit(8) -> $8;
  181. %digit(9) -> $9;
  182. %digit(10) -> $a;
  183. %digit(11) -> $b;
  184. %digit(12) -> $c;
  185. %digit(13) -> $d;
  186. %digit(14) -> $e;
  187. %digit(15) -> $f.
  188. digit(X) when X >= 0 andalso X =< 9 -> X + 48;
  189. digit(X) when X >= 10 andalso X =< 15 -> X + 87.
  190. hex(Bin) ->
  191. << << (digit(A1)), (digit(A2)) >> || <<A1:4, A2:4>> <= Bin >>.
  192. unhex(Hex) ->
  193. << << (erlang:list_to_integer([H1, H2], 16)) >> || <<H1, H2>> <= Hex >>.
  194. f(S) -> f(S, []).
  195. f(S, Args) -> lists:flatten(io_lib:format(S, Args)).
  196. replace([], _, _) -> [];
  197. replace(String, S1, S2) when erlang:is_list(String), erlang:is_list(S1), erlang:is_list(S2) ->
  198. Length = erlang:length(S1),
  199. case string:substr(String, 1, Length) of
  200. S1 -> S2 ++ replace(string:substr(String, Length + 1), S1, S2);
  201. _ -> [hd(String)|replace(tl(String), S1, S2)]
  202. end.
  203. coalesce({_, Y}) -> Y;
  204. coalesce(false) -> undefined;
  205. coalesce([]) -> undefined;
  206. coalesce([H]) -> H;
  207. coalesce([undefined|T]) -> coalesce(T);
  208. coalesce([[]|T]) -> coalesce(T);
  209. coalesce([H|_]) -> H.
  210. indexof(Key, Fields) -> indexof(Key, Fields, 2).
  211. indexof(_, [], _) -> undefined;
  212. indexof(Key, [Key|_], N) -> N;
  213. indexof(Key, [_|T], N) -> indexof(Key, T, N + 1).
  214. config(App, Key, Default) -> application:get_env(App, Key, Default).
  215. os_env(Key) -> os_env(Key, "").
  216. os_env(Key, Default) ->
  217. case os:getenv(Key) of
  218. false -> Default;
  219. V -> V
  220. end.
  221. %% base64 encode/decode
  222. pickle(Data) -> base64:encode(erlang:term_to_binary({Data, os:timestamp()}, [compressed])).
  223. depickle(PickledData) ->
  224. try {Data, _PickleTime} = erlang:binary_to_term(base64:decode(nitro:to_binary(PickledData))),
  225. Data
  226. catch _:_ -> undefined
  227. end.