]> gitweb.factorcode.org Git - factor.git/blob - extra/hexdump/hexdump.factor
6a91cd65c593a6ad522a3cd0d9c095c8a548b0e5
[factor.git] / extra / hexdump / hexdump.factor
1 USING: arrays combinators.lib io io.streams.string
2 kernel math math.parser namespaces prettyprint
3 sequences splitting strings ;
4 IN: hexdump
5
6 <PRIVATE
7
8 : header. ( len -- )
9     "Length: " write dup unparse write ", " write >hex write "h" write nl ;
10
11 : offset. ( lineno -- )
12     16 * >hex 8 CHAR: 0 pad-left write "h: " write ;
13
14 : h-pad. ( digit -- )
15     >hex 2 CHAR: 0 pad-left write ;
16
17 : line. ( str n -- )
18     offset.
19     dup [ h-pad. " " write ] each
20     16 over length - 3 * CHAR: \s <string> write
21     [ dup printable? [ drop CHAR: . ] unless write1 ] each
22     nl ;
23
24 PRIVATE>
25 : hexdump ( seq -- str )
26     [
27         dup length header.
28         16 <sliced-groups> [ line. ] each-index
29     ] string-out ;
30
31 : hexdump. ( seq -- )
32     hexdump write ;