factor/extra/hexdump/hexdump.factor

36 lines
861 B
Factor
Raw Normal View History

2008-10-02 19:08:21 -04:00
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays io io.streams.string kernel math math.parser
namespaces prettyprint sequences 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>
2008-10-02 19:08:21 -04:00
: hexdump ( sequence -- string )
2007-09-20 18:09:08 -04:00
[
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
2008-10-02 19:08:21 -04:00
: hexdump. ( sequence -- )
2007-09-20 18:09:08 -04:00
hexdump write ;