factor/extra/hexdump/hexdump.factor

32 lines
769 B
Factor
Raw Normal View History

USING: arrays io io.streams.string kernel math math.parser namespaces
2008-06-09 06:22:21 -04:00
prettyprint sequences sequences.lib splitting grouping strings ascii ;
2007-09-20 18:09:08 -04:00
IN: hexdump
<PRIVATE
: header. ( len -- )
"Length: " write dup unparse write ", " write >hex write "h" write nl ;
2008-01-20 11:52:50 -05:00
: offset. ( lineno -- )
16 * >hex 8 CHAR: 0 pad-left write "h: " write ;
: h-pad. ( digit -- )
>hex 2 CHAR: 0 pad-left write ;
2007-09-20 18:09:08 -04:00
: line. ( str n -- )
offset.
dup [ h-pad. " " write ] each
2008-01-20 11:52:50 -05:00
16 over length - 3 * CHAR: \s <string> write
2007-09-20 18:09:08 -04:00
[ dup printable? [ drop CHAR: . ] unless write1 ] each
nl ;
PRIVATE>
: hexdump ( seq -- str )
[
dup length header.
2008-01-20 11:52:50 -05:00
16 <sliced-groups> [ line. ] each-index
] with-string-writer ;
2007-09-20 18:09:08 -04:00
: hexdump. ( seq -- )
hexdump write ;