tweak hexdump
parent
edb78de4a7
commit
ee8ba1d5d4
|
@ -1,14 +1,14 @@
|
||||||
! Copyright (C) 2008 Doug Coleman.
|
! Copyright (C) 2008 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: help.markup help.syntax kernel ;
|
USING: help.markup help.syntax kernel sequences strings ;
|
||||||
IN: hexdump
|
IN: hexdump
|
||||||
|
|
||||||
HELP: hexdump.
|
HELP: hexdump.
|
||||||
{ $values { "sequence" "a sequence" } }
|
{ $values { "seq" sequence } }
|
||||||
{ $description "Converts a sequence to its hexadecimal and ASCII representation sixteen characters at a time and writes it to standard out." } ;
|
{ $description "Converts a sequence to its hexadecimal and ASCII representation sixteen characters at a time and writes it to standard out." } ;
|
||||||
|
|
||||||
HELP: hexdump
|
HELP: hexdump
|
||||||
{ $values { "sequence" "a sequence" } { "string" "a string" } }
|
{ $values { "seq" sequence } { "str" string } }
|
||||||
{ $description "Converts a sequence to its hexadecimal and ASCII representation sixteen characters at a time. Lines are separated by a newline character." }
|
{ $description "Converts a sequence to its hexadecimal and ASCII representation sixteen characters at a time. Lines are separated by a newline character." }
|
||||||
{ $see-also hexdump. } ;
|
{ $see-also hexdump. } ;
|
||||||
|
|
||||||
|
|
|
@ -7,29 +7,30 @@ IN: hexdump
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: header. ( len -- )
|
: write-header ( len -- )
|
||||||
"Length: " write dup unparse write ", " write >hex write "h" write nl ;
|
"Length: " write
|
||||||
|
[ unparse write ", " write ]
|
||||||
|
[ >hex write "h" write nl ] bi ;
|
||||||
|
|
||||||
: offset. ( lineno -- )
|
: write-offset ( lineno -- )
|
||||||
16 * >hex 8 CHAR: 0 pad-left write "h: " write ;
|
16 * >hex 8 CHAR: 0 pad-left write "h: " write ;
|
||||||
|
|
||||||
: h-pad. ( digit -- )
|
: write-hex-digit ( digit -- )
|
||||||
>hex 2 CHAR: 0 pad-left write ;
|
>hex 2 CHAR: 0 pad-left write ;
|
||||||
|
|
||||||
: line. ( str n -- )
|
: write-hex-line ( str n -- )
|
||||||
offset.
|
write-offset
|
||||||
dup [ h-pad. " " write ] each
|
dup [ write-hex-digit bl ] each
|
||||||
16 over length - 3 * CHAR: \s <string> write
|
16 over length - 3 * CHAR: \s <string> write
|
||||||
[ dup printable? [ drop CHAR: . ] unless write1 ] each
|
[ dup printable? [ drop CHAR: . ] unless write1 ] each
|
||||||
nl ;
|
nl ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: hexdump ( sequence -- string )
|
: hexdump ( seq -- str )
|
||||||
[
|
[
|
||||||
dup length header.
|
[ length write-header ]
|
||||||
16 <sliced-groups> [ line. ] each-index
|
[ 16 <sliced-groups> [ write-hex-line ] each-index ] bi
|
||||||
] with-string-writer ;
|
] with-string-writer ;
|
||||||
|
|
||||||
: hexdump. ( sequence -- )
|
: hexdump. ( seq -- ) hexdump write ;
|
||||||
hexdump write ;
|
|
||||||
|
|
Loading…
Reference in New Issue