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