From 866f798c0056bdd9f893309478a6c1808652d608 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 2 Nov 2008 02:12:12 -0600 Subject: [PATCH] Clean up hexdump --- extra/hexdump/hexdump.factor | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/extra/hexdump/hexdump.factor b/extra/hexdump/hexdump.factor index 5262755821..b965fb41bb 100644 --- a/extra/hexdump/hexdump.factor +++ b/extra/hexdump/hexdump.factor @@ -1,36 +1,36 @@ ! 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 ; +namespaces sequences splitting grouping strings ascii ; IN: hexdump string write ", " write ] [ >hex write "h" write nl ] bi ; : write-offset ( lineno -- ) 16 * >hex 8 CHAR: 0 pad-left write "h: " write ; -: write-hex-digit ( digit -- ) - >hex 2 CHAR: 0 pad-left write ; +: >hex-digit ( digit -- str ) + >hex 2 CHAR: 0 pad-left " " append ; -: 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 ; +: >hex-digits ( bytes -- str ) + [ >hex-digit ] { } map-as concat 48 CHAR: \s pad-right ; + +: >ascii ( bytes -- str ) + [ [ printable? ] keep CHAR: . ? ] map ; + +: write-hex-line ( str lineno -- ) + write-offset [ >hex-digits write ] [ >ascii write ] bi nl ; PRIVATE> -: hexdump ( seq -- str ) - [ - [ length write-header ] - [ 16 [ write-hex-line ] each-index ] bi - ] with-string-writer ; +: hexdump. ( seq -- ) + [ length write-header ] + [ 16 [ write-hex-line ] each-index ] bi ; -: hexdump. ( seq -- ) hexdump write ; +: hexdump ( seq -- str ) + [ hexdump. ] with-string-writer ;