update ChangeLog
[lwes-erlang/github-mirror.git] / src / lwes_util.erl
blobcb13df1ac6589fbdb7eb0abc0c13ee49efabde2b
1 -module (lwes_util).
3 -include_lib ("lwes.hrl").
4 -include_lib ("lwes_internal.hrl").
6 % API
7 -export ([normalize_ip/1,
8 ip2bin/1,
9 ceiling/1,
10 count_ones/1,
11 any_to_list/1,
12 any_to_binary/1,
13 arr_to_binary/1,
14 arr_to_binary/2,
15 binary_to_any/2,
16 binary_to_arr/2]).
18 %%====================================================================
19 %% API functions
20 %%====================================================================
21 normalize_ip (Ip) when ?is_ip_addr (Ip) ->
22 Ip;
23 normalize_ip (Ip) when is_binary (Ip) ->
24 normalize_ip (binary_to_list (Ip));
25 normalize_ip (Ip) when is_list (Ip) ->
26 case inet_parse:address (Ip) of
27 {ok, {N1, N2, N3, N4}} -> {N1, N2, N3, N4};
28 _ -> erlang:error(badarg)
29 end;
30 normalize_ip (_) ->
31 % essentially turns function_clause error into badarg
32 erlang:error (badarg).
34 ip2bin (Ip) when is_binary (Ip) ->
35 Ip;
36 ip2bin (Ip) ->
37 list_to_binary (inet_parse:ntoa (Ip)).
40 ceiling(X) when X < 0 ->
41 trunc(X);
42 ceiling(X) ->
43 T = trunc(X),
44 case X - T == 0 of
45 true -> T;
46 false -> T + 1
47 end.
49 count_ones(Bin) -> count_ones(Bin, 0).
50 count_ones(<<>>, Counter) -> Counter;
51 count_ones(<<X:1, Rest/bitstring>>, Counter) ->
52 count_ones(Rest, Counter + X).
54 any_to_list (B) when is_binary (B) ->
55 binary_to_list (B);
56 any_to_list (L) when is_list (L) ->
58 any_to_list (I) when is_integer (I) ->
59 integer_to_list (I);
60 any_to_list (F) when is_float (F) ->
61 float_to_list (F);
62 any_to_list (A) when is_atom (A) ->
63 atom_to_list (A).
65 arr_to_binary (L, ipaddr) ->
66 [ip2bin(E) || E <- L ].
67 arr_to_binary (L) ->
68 [any_to_binary (E) || E <- L ].
70 any_to_binary (Ip) when ?is_ip_addr (Ip) ->
71 ip2bin (Ip);
72 any_to_binary (T) when is_tuple (T) ->
74 any_to_binary (I) when is_integer (I) ->
75 list_to_binary (integer_to_list (I));
76 any_to_binary (F) when is_float (F) ->
77 list_to_binary (float_to_list (F));
78 any_to_binary (L = [H|_]) when is_tuple (H) ->
80 any_to_binary (L = [[_|_]|_]) when is_list (L) ->
81 % support list of lists, being turned into list of binaries
82 [ any_to_binary (E) || E <- L ];
83 any_to_binary (L) when is_list (L) ->
84 list_to_binary (L);
85 any_to_binary (A) when is_atom (A) ->
86 list_to_binary (atom_to_list (A));
87 any_to_binary (B) when is_binary (B) ->
90 binary_to_arr (List, Type) ->
91 [ case E of
92 null -> undefined;
93 _ -> binary_to_any(E, Type)
94 end
95 || E
96 <- List
99 binary_to_any (Bin, binary) when is_binary (Bin) ->
100 Bin;
101 binary_to_any (L = [B|_], binary) when is_binary (B) ->
103 binary_to_any (Bin, Type) when is_binary (Bin) ->
104 binary_to_any (binary_to_list (Bin), Type);
105 binary_to_any (List, integer) ->
106 {I, _} = string:to_integer (List),
108 binary_to_any (List, float) ->
109 {F, _} = string:to_float (List),
111 binary_to_any (List, ipaddr) -> normalize_ip (List);
112 binary_to_any (L = [H|_], list) when is_binary (H) ->
113 [ binary_to_list (E) || E <- L ];
114 binary_to_any (List, list) ->
115 List;
116 binary_to_any (List, atom) ->
117 list_to_atom (List).
120 %%====================================================================
121 %% Test functions
122 %%====================================================================
123 -ifdef (TEST).
124 -include_lib ("eunit/include/eunit.hrl").
126 normalize_ip_test_ () ->
128 ?_assertEqual ({127,0,0,1}, normalize_ip (<<"127.0.0.1">>)),
129 ?_assertEqual ({127,0,0,1}, normalize_ip ("127.0.0.1")),
130 ?_assertEqual ({127,0,0,1}, normalize_ip ({127,0,0,1})),
131 ?_assertError (badarg, normalize_ip (<<"655.0.0.1">>)),
132 ?_assertError (badarg, normalize_ip ("655.0.0.1")),
133 ?_assertError (badarg, normalize_ip ({655,0,0,1}))
137 ceil_test_ () ->
139 ?_assertEqual(8, ceiling(7.5)),
140 ?_assertEqual(-10, ceiling(-10.9)),
141 ?_assertEqual(0, ceiling(0))
144 count_ones_test_ () ->
146 ?_assertEqual(0, count_ones (<<2#00000000>>)),
147 ?_assertEqual(1, count_ones (<<2#00000001>>)),
148 ?_assertEqual(1, count_ones (<<2#00000010>>)),
149 ?_assertEqual(2, count_ones (<<2#00000011>>)),
150 ?_assertEqual(2, count_ones (<<2#11000000>>)),
151 ?_assertEqual(7, count_ones (<<2#01111111>>)),
152 ?_assertEqual(14, count_ones (<<2#01111111,2#01111111>>))
155 binary_test_ () ->
157 ?_assertEqual (U, binary_to_any (any_to_binary (U), T))
158 || { U, T }
161 { 1.35, float },
162 { 1, integer },
163 { 1234567890123, integer },
164 { <<"b">>, binary },
165 { "b", list },
166 { true, atom },
167 { ["1"], list },
168 { ["1","2"], list }
172 -endif.