From ee8ba1d5d4b1a4b025a8086287540dc5f2bac854 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 21 Oct 2008 20:43:29 -0500 Subject: [PATCH] tweak hexdump --- extra/hexdump/hexdump-docs.factor | 6 +++--- extra/hexdump/hexdump.factor | 25 +++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/extra/hexdump/hexdump-docs.factor b/extra/hexdump/hexdump-docs.factor index a83f64e8db..4278e92f0e 100644 --- a/extra/hexdump/hexdump-docs.factor +++ b/extra/hexdump/hexdump-docs.factor @@ -1,14 +1,14 @@ ! Copyright (C) 2008 Doug Coleman. ! 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 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." } ; 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." } { $see-also hexdump. } ; diff --git a/extra/hexdump/hexdump.factor b/extra/hexdump/hexdump.factor index 618ed00802..5262755821 100644 --- a/extra/hexdump/hexdump.factor +++ b/extra/hexdump/hexdump.factor @@ -7,29 +7,30 @@ IN: hexdump hex write "h" write nl ; +: write-header ( len -- ) + "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 ; -: h-pad. ( digit -- ) +: write-hex-digit ( digit -- ) >hex 2 CHAR: 0 pad-left write ; -: line. ( str n -- ) - offset. - dup [ h-pad. " " write ] each +: write-hex-line ( str n -- ) + write-offset + dup [ write-hex-digit bl ] each 16 over length - 3 * CHAR: \s write [ dup printable? [ drop CHAR: . ] unless write1 ] each nl ; PRIVATE> -: hexdump ( sequence -- string ) +: hexdump ( seq -- str ) [ - dup length header. - 16 [ line. ] each-index + [ length write-header ] + [ 16 [ write-hex-line ] each-index ] bi ] with-string-writer ; -: hexdump. ( sequence -- ) - hexdump write ; +: hexdump. ( seq -- ) hexdump write ;