3 -include_lib ("lwes.hrl").
4 -include_lib ("lwes_internal.hrl").
7 -export ([normalize_ip
/1,
18 %%====================================================================
20 %%====================================================================
21 normalize_ip (Ip
) when ?
is_ip_addr (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
)
31 % essentially turns function_clause error into badarg
32 erlang:error (badarg
).
34 ip2bin (Ip
) when is_binary (Ip
) ->
37 list_to_binary (inet_parse:ntoa (Ip
)).
40 ceiling(X
) when X
< 0 ->
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
) ->
56 any_to_list (L
) when is_list (L
) ->
58 any_to_list (I
) when is_integer (I
) ->
60 any_to_list (F
) when is_float (F
) ->
62 any_to_list (A
) when is_atom (A
) ->
65 arr_to_binary (L
, ipaddr
) ->
66 [ip2bin(E
) || E
<- L
].
68 [any_to_binary (E
) || E
<- L
].
70 any_to_binary (Ip
) when ?
is_ip_addr (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
) ->
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
) ->
93 _
-> binary_to_any(E
, Type
)
99 binary_to_any (Bin
, binary) when is_binary (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) ->
116 binary_to_any (List
, atom) ->
120 %%====================================================================
122 %%====================================================================
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}))
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>>))
157 ?
_assertEqual (U
, binary_to_any (any_to_binary (U
), T
))
163 { 1234567890123, integer },