Skip to content

Commit

Permalink
Add binary functions
Browse files Browse the repository at this point in the history
  • Loading branch information
glejeune committed May 26, 2017
1 parent cdf75aa commit f6de827
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
38 changes: 37 additions & 1 deletion doc/bucbinary.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,50 @@
## Function Index ##


<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#join-2">join/2</a></td><td>
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#are_floats-1">are_floats/1</a></td><td></td></tr><tr><td valign="top"><a href="#are_integers-1">are_integers/1</a></td><td></td></tr><tr><td valign="top"><a href="#is_float-1">is_float/1</a></td><td></td></tr><tr><td valign="top"><a href="#is_integer-1">is_integer/1</a></td><td></td></tr><tr><td valign="top"><a href="#join-2">join/2</a></td><td>
join a list of binaries with the given separator.</td></tr><tr><td valign="top"><a href="#trim-2">trim/2</a></td><td></td></tr></table>


<a name="functions"></a>

## Function Details ##

<a name="are_floats-1"></a>

### are_floats/1 ###

<pre><code>
are_floats(List::[binary()]) -&gt; true | false
</code></pre>
<br />

<a name="are_integers-1"></a>

### are_integers/1 ###

<pre><code>
are_integers(List::[binary()]) -&gt; true | false
</code></pre>
<br />

<a name="is_float-1"></a>

### is_float/1 ###

<pre><code>
is_float(Data::binary()) -&gt; true | false
</code></pre>
<br />

<a name="is_integer-1"></a>

### is_integer/1 ###

<pre><code>
is_integer(Data::binary()) -&gt; true | false
</code></pre>
<br />

<a name="join-2"></a>

### join/2 ###
Expand Down
70 changes: 68 additions & 2 deletions src/bucbinary.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
-module(bucbinary).

-export([
join/2,
trim/2
join/2
, trim/2
, is_integer/1
, is_float/1
, are_integers/1
, are_floats/1
]).

%% @doc
Expand Down Expand Up @@ -57,3 +61,65 @@ trim_right(Binary, Size) ->
Other
end.

-spec are_floats([binary()]) -> true | false.
are_floats([]) ->
false;
are_floats(List) ->
are_floats(List, true).
are_floats([], Acc) ->
Acc;
are_floats([E|Rest], Acc) ->
are_floats(Rest, bucbinary:is_float(E) andalso Acc).

-spec is_float(binary()) -> true | false.
is_float(<<>>) ->
false;
is_float(Data) ->
case binary:split(Data, [<<".">>]) of
[_, _] = V ->
are_integers(V);
_ ->
false
end.

-spec are_integers([binary()]) -> true | false.
are_integers([]) ->
false;
are_integers(List) ->
are_integers(List, true).
are_integers([], Acc) ->
Acc;
are_integers([E|Rest], Acc) ->
are_integers(Rest, bucbinary:is_integer(E) andalso Acc).

-spec is_integer(binary()) -> true | false.
is_integer(<<>>) ->
false;
is_integer(Data) ->
do_is_integer(Data).


do_is_integer(<<>>) ->
true;
do_is_integer(<<"0", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"1", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"2", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"3", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"4", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"5", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"6", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"7", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"8", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(<<"9", Rest/binary>>) ->
do_is_integer(Rest);
do_is_integer(_) ->
false.
2 changes: 1 addition & 1 deletion src/bucinet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ country(ipapi, IP) ->
<<"country">>,
<<"timezone">>);
country(ipinfo, IP) ->
country("http://ipinfo.io/" ++ ip_to_string(IP),
country("http://ipinfo.io/" ++ ip_to_string(IP) ++ "/json",
<<"country">>,
<<"xxx">>,
<<"xxx">>);
Expand Down
20 changes: 20 additions & 0 deletions test/bucbinary_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ bucbinary_test_() ->
[
?_test(t_join())
, ?_test(t_trim())
, ?_test(t_integers())
, ?_test(t_floats())
]}.

setup() ->
Expand Down Expand Up @@ -41,3 +43,21 @@ t_trim() ->
?assertEqual(<<"hello \n\r world">>,
bucbinary:trim(<<" \r hello \n\r world \n ">>, both)).

t_integers() ->
?assert(bucbinary:is_integer(<<"123">>)),
?assert(bucbinary:is_integer(<<"1">>)),
?assertNot(bucbinary:is_integer(<<"A">>)),
?assertNot(bucbinary:is_integer(<<"1.2">>)),
?assert(bucbinary:are_integers([<<"1">>, <<"123">>])),
?assertNot(bucbinary:are_integers([<<"A">>, <<"123">>])),
?assertNot(bucbinary:are_integers([<<"1.2">>, <<"123">>])).

t_floats() ->
?assert(bucbinary:is_float(<<"12.3">>)),
?assertNot(bucbinary:is_float(<<"1.">>)),
?assertNot(bucbinary:is_float(<<".1">>)),
?assertNot(bucbinary:is_float(<<"A">>)),
?assertNot(bucbinary:is_float(<<"123">>)),
?assert(bucbinary:are_floats([<<"1.1">>, <<"12.3">>])),
?assertNot(bucbinary:are_floats([<<"A">>, <<"12.3">>])),
?assertNot(bucbinary:are_floats([<<"1.2">>, <<"123">>])).
2 changes: 1 addition & 1 deletion test/bucinet_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ t_country() ->
bucinet:country("127.0.0.1")),
?assertEqual({ok, <<"US">>, <<"United States">>, <<"America/Los_Angeles">>},
bucinet:country(freegeoip, "208.80.152.201")),
?assertEqual({ok, <<"US">>, <<"United States">>, <<"America/Los_Angeles">>},
?assertEqual({ok, <<"US">>, <<"United States">>, <<"America/New_York">>},
bucinet:country(ipapi, "208.80.152.201")),
?assertEqual({ok, <<"US">>, undefined, undefined},
bucinet:country(ipinfo, "208.80.152.201")),
Expand Down

0 comments on commit f6de827

Please sign in to comment.