Paste: hexdump cleanup

Author: slava
Mode: factor
Date: Sun, 2 Nov 2008 08:07:07
Plain Text |
! 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 sequences splitting grouping strings ascii ;
IN: hexdump

<PRIVATE

: write-header ( len -- )
    "Length: " write
    [ number>string write ", " write ]
    [ >hex write "h" write nl ] bi ;

: write-offset ( lineno -- )
    16 * >hex 8 CHAR: 0 pad-left write "h: " write ;

: >hex-digit ( digit -- str )
    >hex 2 CHAR: 0 pad-left " " append ;

: >hex-digits ( bytes -- str )
    [ >hex-digit ] { } map-as concat 48 CHAR: \s pad-right ;

: >ascii ( bytes -- str )
    [ dup printable? [ drop CHAR: . ] unless ] map ;

: write-hex-line ( str n -- )
    write-offset [ >hex-digits write ] [ >ascii write ] bi nl ;

PRIVATE>

: hexdump. ( seq -- )
    [ length write-header ]
    [ 16 <sliced-groups> [ write-hex-line ] each-index ] bi ;

: hexdump ( seq -- str )
    [ hexdump. ] with-string-writer ;

New Annotation

Summary:
Author:
Mode:
Body: