Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add custom printers and plain returns support

  • Loading branch information...
commit e0ca459b154f461a1c3e7883a5ca555a6d3db8f3 1 parent 41be2a2
@aerosol aerosol authored
Showing with 48 additions and 25 deletions.
  1. +25 −3 README.md
  2. +23 −22 src/binpp.erl
View
28 README.md
@@ -24,10 +24,33 @@ Example usage:
11011000 01010100 10010001 01010010 01011101 10000001 00010111 00100100
00010001 00010100 01100000 00100011 11010001 00111101 01010101 10000000"
+Binpp will use io:format to output the formatted binary by default, however
+there are options making pprint functions return formatted data instead
+of performing direct IO write:
+
+ 1> Bin2 = <<"foo bar baz">>.
+ <<"foo bar baz">>
+ 2> binpp:pprint(Bin2, [{return, iolist}]).
+ [["66 6F 6F 20 62 61 72 20 62 61 7A ",32,
+ "foo bar baz","\n"]]
+ 3> binpp:pprint(Bin2, [{return, binary}]).
+ <<"66 6F 6F 20 62 61 72 20 62 61 7A foo bar baz\n">>
+
+You may use a custom printer function as well:
+
+ 4> binpp:pprint(Bin2, [{printer, fun(O) -> io:format("~s~n", [O]) end}]).
+ 66 6F 6F 20 62 61 72 20 62 61 7A foo bar baz
+
+ ok
+
+An additional API is provided for printing binary fragments:
+
+ 5> binpp:pprint(Bin2, {0, 3}, []).
+ 66 6F 6F foo
Also, binary byte-to-byte comparsion:
- 4> binpp:cmprint(<<1,2,1024:512,3,4>>, <<1,3,1024:512,5,6>>).
+ 6> binpp:cmprint(<<1,2,1024:512,3,4>>, <<1,3,1024:512,5,6>>).
-- 02 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 03 -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
@@ -37,7 +60,7 @@ Also, binary byte-to-byte comparsion:
Plus a handy little helper:
- 5> binpp:cmprint(<<1,2,255,3,1024:512>>, binpp:from_str("01 02 FF CC")).
+ 7> binpp:cmprint(<<1,2,255,3,1024:512>>, binpp:from_str("01 02 FF CC")).
-- -- -- 03 00 00 00 00 00 00 00 00 00 00 00 00 -- -- -- CC ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
@@ -45,5 +68,4 @@ Plus a handy little helper:
00 00 04 00 ?? ?? ?? ??
ok
-
That's all folks!
View
45 src/binpp.erl
@@ -14,13 +14,14 @@
-module(binpp).
-author('Adam Rutkowski adam@mtod.org').
--export([pprint/1, pprint/3]).
--export([pbin/1, pbin/3]).
+-export([pprint/1, pprint/2, pprint/3]).
-export([cmprint/2]).
-export([from_str/1, from_str/2]).
-export([format/1, format/2]).
-export([convert/1, convert/2]).
+-opaque opt() :: {return, iolist} | {return, binary} | {printer, function()}.
+-opaque opts() :: list(opt()).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% API %
@@ -53,28 +54,22 @@ format(Bin) ->
-spec pprint(binary()) -> ok.
pprint(Bin) ->
- {ok, Octets} = convert(Bin, hex),
- Buckets = buckets(16, Octets),
- io:format("~s~n", [lists:map(fun print_bucket/1, Buckets)]).
-
-pprint(Bin, Pos, Len) when Len =< size(Bin) ->
- pprint(binary:part(Bin, Pos, Len));
+ pprint(Bin, []).
-pprint(Bin, Pos, _) ->
- pprint(binary:part(Bin, Pos, size(Bin)-Pos)).
+-spec pprint(binary(), opts()) -> ok | any().
--spec pbin(binary()) -> iolist().
-
-pbin(Bin) ->
+pprint(Bin, Opts) when is_list(Opts) ->
{ok, Octets} = convert(Bin, hex),
Buckets = buckets(16, Octets),
- lists:map(fun print_bucket/1, Buckets).
+ apply_opts(lists:map(fun print_bucket/1, Buckets), Opts).
-pbin(Bin, Pos, Len) when Len =< size(Bin) ->
- pbin(binary:part(Bin, Pos, Len));
+-spec pprint(binary(), {non_neg_integer(), non_neg_integer()},
+ opts()) -> ok | any().
-pbin(Bin, Pos, _) ->
- pbin(binary:part(Bin, Pos, size(Bin)-Pos)).
+pprint(Bin, {Pos, Len}, Opts) when Len =< size(Bin) ->
+ pprint(binary:part(Bin, Pos, Len), Opts);
+pprint(Bin, {Pos, _}, Opts) ->
+ pprint(binary:part(Bin, Pos, size(Bin)-Pos), Opts).
-spec cmprint(binary(), binary()) -> ok.
@@ -105,6 +100,16 @@ from_str(Str) when is_list(Str) ->
% Core :) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+apply_opts(IoList, []) ->
+ io:format("~s~n", [IoList]);
+apply_opts(IoList, [{return, iolist}]) ->
+ IoList;
+apply_opts(IoList, [{return, binary}]) ->
+ iolist_to_binary(IoList);
+apply_opts(IoList, [{printer, Fun}]) when is_function(Fun) ->
+ Fun(IoList);
+apply_opts(_, _) -> erlang:error(badarg).
+
convert(<<>>, Acc, _) ->
{ok, lists:reverse(Acc)};
@@ -141,16 +146,12 @@ print_comparsion([L|LRest], [R|RRest]) ->
diff([], [], LD, RD) ->
{ok, {lists:reverse(LD), lists:reverse(RD)}};
-
diff([], [H2|R2], LD, RD) ->
diff([], R2, ["??"|LD], [H2|RD]);
-
diff([H1|R1], [], LD, RD) ->
diff(R1, [], [H1|LD], ["??"|RD]);
-
diff([H1|R1], [H2|R2], LD, RD) when H1 =:= H2 ->
diff(R1, R2, ["--"|LD], ["--"|RD]);
-
diff([H1|R1], [H2|R2], LD, RD) ->
diff(R1, R2, [H1|LD], [H2|RD]).
Please sign in to comment.
Something went wrong with that request. Please try again.