123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- -module(nitro_conv).
- %%-author('Maxim Sokhatsky').
- %% N2O Formatter: JSON, BERT
- -compile(export_all).
- -include_lib("nitro/include/nitro.hrl").
- % WF to_list to_atom to_binary to_integer
- -define(IS_STRING(Term),
- (erlang:is_list(Term) andalso Term /= [] andalso erlang:is_integer(hd(Term)))).
- -ifndef(N2O_JSON).
- -define(N2O_JSON, (application:get_env(n2o,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) -> erlang:binary_to_atom(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(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(I) when erlang:is_integer(I) -> erlang:integer_to_binary(I);
- to_binary(F) when erlang:is_float(F) -> erlang:float_to_binary(F, [{decimals, 9}, compact]);
- to_binary(L) when erlang:is_list(L) -> erlang:iolist_to_binary(L);
- to_binary(X) when erlang:is_tuple(X) -> erlang:term_to_binary(X).
- 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).
- %% HTML encode/decode
- %% html_encode(B, normal)
- html_encode(L, Fun) when erlang:is_function(Fun) ->
- Fun(L);
- html_encode(L, EncType) when erlang:is_atom(L) ->
- html_encode(nitro:to_list(L), EncType);
- html_encode(L, EncType) when erlang:is_integer(L) ->
- html_encode(erlang:integer_to_list(L), EncType);
- html_encode(L, EncType) when erlang:is_float(L) ->
- html_encode(erlang:float_to_list(L, [{decimals, 9}, compact]), EncType);
- html_encode(L, false) ->
- L;
- html_encode(L, true) ->
- L;
- html_encode(L, whites) ->
- html_encode_whites(nitro:to_list(lists:flatten([L]))).
- html_encode(<<>>) ->
- [];
- html_encode([]) ->
- [];
- html_encode(B) when is_binary(B) ->
- nitro:to_binary(
- html_encode(erlang:binary_to_list(B))
- );
- html_encode([$\n |T]) ->
- "<br>" ++ html_encode(T);
- html_encode([H|T]) ->
- case H of
- $< -> "<" ++ html_encode(T);
- $> -> ">" ++ html_encode(T);
- $" -> """ ++ html_encode(T);
- $' -> "'" ++ html_encode(T);
- $& -> "&" ++ html_encode(T);
- $\\ -> "\" ++ html_encode(T);
- BigNum when erlang:is_integer(BigNum) andalso BigNum > 255 ->
- %% Any integers above 255 are converted to their HTML encode equivalent
- %% Example: 7534 gets turned into ᵮ
- [$&, $# | nitro:to_list(BigNum)] ++ ";" ++ html_encode(T);
- Tup when erlang:is_tuple(Tup) ->
- erlang:throw({html_encode, encountered_tuple, Tup});
- _ ->
- [H|html_encode(T)]
- end.
- html_encode_whites([]) -> [];
- html_encode_whites([H|T]) ->
- case H of
- $\s -> " " ++ html_encode_whites(T);
- $\t -> " " ++ html_encode_whites(T);
- $< -> "<" ++ html_encode_whites(T);
- $> -> ">" ++ html_encode_whites(T);
- $" -> """ ++ html_encode_whites(T);
- $' -> "'" ++ html_encode_whites(T);
- $& -> "&" ++ html_encode_whites(T);
- $\n -> "<br>" ++ html_encode_whites(T);
- $\\ -> "\" ++ html_encode_whites(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]).
- %% JavaScript escape
- js_escape(undefined) -> [];
- js_escape(Value) when erlang:is_list(Value) ->
- erlang:binary_to_list( js_escape( erlang:iolist_to_binary(Value) ) );
- js_escape(Value) -> js_escape(Value, <<>>).
- js_escape(<<"\\", Rest/binary>>, Acc) -> %"
- js_escape(Rest, <<Acc/binary, "\\\\">>); %"
- js_escape(<<"\r", Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, "\\r">>);
- js_escape(<<"\n", Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, "\\n">>);
- js_escape(<<"\"", Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, "\\\"">>);
- js_escape(<<"'", Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, "\\'">>);
- js_escape(<<"`", Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, "\\`">>);
- js_escape(<<"<script", Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, "<script">>);
- js_escape(<<"script>", Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, "script>">>);
- js_escape(<<C, Rest/binary>>, Acc) ->
- js_escape(Rest, <<Acc/binary, C>>);
- js_escape(<<>>, Acc) -> Acc.
- %% JOIN
- %% join(List, Delimiter)
- 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.
- 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 >>.
- f(S) -> f(S, []).
- f(S, Args) -> lists:flatten(io_lib:format(S, Args)).
- replace([], _, _) -> [];
- replace(String, S1, S2) when erlang:is_list(String), erlang:is_list(S1), erlang:is_list(S2) ->
- Length = erlang:length(S1),
- case string:substr(String, 1, Length) of
- S1 -> S2 ++ replace(string:substr(String, Length + 1), S1, S2);
- _ -> [hd(String)|replace(tl(String), S1, S2)]
- end.
- coalesce({_, Y}) -> Y;
- coalesce(false) -> undefined;
- coalesce([]) -> undefined;
- coalesce([H]) -> H;
- coalesce([undefined|T]) -> coalesce(T);
- coalesce([[]|T]) -> coalesce(T);
- coalesce([H|_]) -> H.
- indexof(Key, Fields) -> indexof(Key, Fields, 2).
- indexof(_, [], _) -> undefined;
- indexof(Key, [Key|_], N) -> N;
- indexof(Key, [_|T], N) -> indexof(Key, T, N + 1).
- config(App, Key, Default) -> application:get_env(App, Key, Default).
- os_env(Key) -> os_env(Key, "").
- os_env(Key, Default) ->
- case os:getenv(Key) of
- false -> Default;
- V -> V
- end.
- %% base64 encode/decode
- pickle(Data) -> base64:encode(erlang:term_to_binary({Data, os:timestamp()}, [compressed])).
- depickle(PickledData) ->
- try {Data, _PickleTime} = erlang:binary_to_term(base64:decode(nitro:to_binary(PickledData))),
- Data
- catch _:_ -> undefined
- end.
|