Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add simple offset numbering

  • Loading branch information...
commit eea5d79cea2586690e04e4843556f527f5df0023 1 parent 246344b
@andrzejsliwa andrzejsliwa authored aerosol committed
Showing with 50 additions and 9 deletions.
  1. +8 −8 README.md
  2. +42 −1 src/binpp.erl
View
16 README.md
@@ -9,11 +9,11 @@ Example usage:
<<12,242,207,49,82,69,45,130,212,69,80,88,8,81,23,36,86,7,
68,19,133,97,51,216,56,145,88,8,81,...>>
2> binpp:pprint(Bin).
- 0C F2 CF 31 52 45 2D 82 D4 45 50 58 08 51 17 24 .òÏ1RE-‚ÔEPX.Q.$
- 56 07 44 13 85 61 33 D8 38 91 58 08 51 17 24 56 V.D.…a3Ø8‘X.Q.$V
- 0A 14 20 4E 24 16 09 60 F4 0A 15 11 01 30 13 89 .. N$..`ô....0.‰
- 05 81 0F 09 15 C5 61 33 D8 54 91 52 5D 81 17 24 ....Åa3ØT‘R].$
- 11 14 60 23 D1 3D 55 80 ..`#Ñ=U€
+ 00: 0C F2 CF 31 52 45 2D 82 D4 45 50 58 08 51 17 24 .òÏ1RE-‚ÔEPX.Q.$
+ 10: 56 07 44 13 85 61 33 D8 38 91 58 08 51 17 24 56 V.D.…a3Ø8‘X.Q.$V
+ 20: 0A 14 20 4E 24 16 09 60 F4 0A 15 11 01 30 13 89 .. N$..`ô....0.‰
+ 30: 05 81 0F 09 15 C5 61 33 D8 54 91 52 5D 81 17 24 ....Åa3ØT‘R].$
+ 40: 11 14 60 23 D1 3D 55 80 ..`#Ñ=U€
ok
Binpp will use io:format to output the formatted binary by default, however
@@ -23,15 +23,15 @@ 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,
+ [["00:",32,"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">>
+ <<"00: 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
+ 00: 66 6F 6F 20 62 61 72 20 62 61 7A foo bar baz
ok
View
43 src/binpp.erl
@@ -58,7 +58,8 @@ pprint(Bin) ->
pprint(Bin, Opts) when is_list(Opts) ->
{ok, Octets} = convert(Bin, hex),
Buckets = buckets(16, Octets),
- apply_opts(lists:map(fun print_bucket/1, Buckets), Opts).
+ Printed = print_buckets(Buckets),
+ apply_opts(Printed, Opts).
%% @doc Pretty print a slice of binary.
-spec pprint(binary() | bitstring(), {non_neg_integer(), non_neg_integer()},
@@ -132,6 +133,14 @@ convert(Bin, [], FormatFun) when is_bitstring(Bin), not is_binary(Bin) ->
convert(<<Bin:8/integer, Rest/binary>>, SoFar, FormatFun) ->
convert(Rest, [FormatFun(Bin)|SoFar], FormatFun).
+print_buckets(Buckets) ->
+ {Printed, _} = lists:mapfoldl(fun(Bucket, Offset) ->
+ B = print_bucket(Bucket),
+ Annotated = io_lib:format("~4.16.0B ~s", [Offset, B]),
+ {Annotated, Offset+1}
+ end, 0, Buckets),
+ Printed.
+
print_bucket(Bucket) ->
OctetLine = string:join(Bucket, [?SPACE]),
OctetRepr = lists:map(
@@ -278,6 +287,38 @@ print_bucket_test_() ->
[ { iolist_to_binary(["Print ", I]), fun() -> ?assertEqual(R, F(I)) end }
|| { I, R } <- Tests ].
+print_buckets_test_() ->
+ F = fun print_buckets/1,
+ Tests = [
+ {
+ [ ["00", "FF"] || _ <- lists:seq(0, 16) ],
+ lists:flatten([
+ "0000 00 FF .",255,"\n",
+ "0001 00 FF .",255,"\n",
+ "0002 00 FF .",255,"\n",
+ "0003 00 FF .",255,"\n",
+ "0004 00 FF .",255,"\n",
+ "0005 00 FF .",255,"\n",
+ "0006 00 FF .",255,"\n",
+ "0007 00 FF .",255,"\n",
+ "0008 00 FF .",255,"\n",
+ "0009 00 FF .",255,"\n",
+ "000A 00 FF .",255,"\n",
+ "000B 00 FF .",255,"\n",
+ "000C 00 FF .",255,"\n",
+ "000D 00 FF .",255,"\n",
+ "000E 00 FF .",255,"\n",
+ "000F 00 FF .",255,"\n",
+ "0010 00 FF .",255,"\n"])
+ }
+ ],
+ [ { iolist_to_binary(["Print buckets ", I]),
+ fun() ->
+ B = F(I),
+ ?assertEqual(R, lists:flatten(B))
+ end } || { I, R } <- Tests ].
+
+
convert_hex_test_() ->
F = fun convert/1,
Tests = [
Please sign in to comment.
Something went wrong with that request. Please try again.