1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays io io.streams.string kernel math math.parser
4 namespaces sequences splitting grouping strings ascii byte-arrays ;
9 : write-header ( len -- )
11 [ number>string write ", " write ]
12 [ >hex write "h" write nl ] bi ;
14 : write-offset ( lineno -- )
15 16 * >hex 8 CHAR: 0 pad-head write "h: " write ;
17 : >hex-digit ( digit -- str )
18 >hex 2 CHAR: 0 pad-head " " append ;
20 : >hex-digits ( bytes -- str )
21 [ >hex-digit ] { } map-as concat 48 CHAR: \s pad-tail ;
23 : >ascii ( bytes -- str )
24 [ [ printable? ] keep CHAR: . ? ] "" map-as ;
26 : write-hex-line ( bytes lineno -- )
27 write-offset [ >hex-digits write ] [ >ascii write ] bi nl ;
31 GENERIC: hexdump. ( byte-array -- )
33 M: byte-array hexdump.
34 [ length write-header ]
35 [ 16 <sliced-groups> [ write-hex-line ] each-index ] bi ;
37 : hexdump ( byte-array -- str )
38 [ hexdump. ] with-string-writer ;