factor/contrib/hexdump/hexdump.factor

27 lines
742 B
Factor
Raw Normal View History

2006-07-24 03:42:54 -04:00
USING: io kernel math namespaces prettyprint sequences strings ;
IN: hexdump-internals
2006-08-10 00:27:54 -04:00
: header. ( len -- )
2006-07-24 03:42:54 -04:00
"Length: " write dup unparse write ", " write >hex write "h" write terpri ;
2006-08-10 00:27:54 -04:00
: offset. ( lineno -- ) 16 * >hex 8 CHAR: 0 pad-left write "h: " write ;
: h-pad. ( digit -- ) >hex 2 CHAR: 0 pad-left write ;
: line. ( str n -- )
offset. [ [ h-pad. " " write ] each ] keep
2006-07-24 03:42:54 -04:00
16 over length - [ " " write ] times
[ dup printable? [ drop CHAR: . ] unless ch>string write ] each
terpri ;
IN: hexdump
: hexdump ( str -- str )
#! Write hexdump to a string
[
2006-08-10 00:27:54 -04:00
dup length header.
16 group dup length [ line. ] 2each
2006-07-24 03:42:54 -04:00
] string-out ;
2006-08-10 00:27:54 -04:00
: hexdump. ( str -- )
2006-07-24 03:42:54 -04:00
#! Print hexdump
hexdump write ;