factor/extra/printf/printf.factor

108 lines
3.5 KiB
Factor
Raw Normal View History

2008-09-11 21:16:35 -04:00
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: io io.encodings.ascii io.files io.streams.string
kernel sequences splitting strings vectors math math.parser macros
2008-09-16 20:26:28 -04:00
fry peg.ebnf unicode.case arrays prettyprint quotations ;
2008-09-11 21:16:35 -04:00
2008-09-16 20:26:28 -04:00
IN: printf
2008-09-11 21:16:35 -04:00
2008-09-16 20:26:28 -04:00
<PRIVATE
2008-09-11 21:16:35 -04:00
2008-09-16 20:26:28 -04:00
: compose-all ( seq -- quot )
[ ] [ compose ] reduce ;
: write-all ( seq -- quot )
[ [ write ] append ] map ;
: apply-format ( params quot -- params string )
[ dup pop ] dip call ; inline
2008-09-16 20:26:28 -04:00
: fix-neg ( string -- string )
dup CHAR: 0 swap index 0 =
[ dup CHAR: - swap index dup
[ swap remove-nth "-" prepend ]
[ drop ] if ] when ;
: >digits ( string -- digits )
[ 0 ] [ string>number ] if-empty ;
: max-digits ( string digits -- string )
[ "." split1 ] dip [ CHAR: 0 pad-right ] [ head-slice ] bi "." swap 3append ;
2008-09-16 20:26:28 -04:00
: max-width ( string length -- string )
[ dup length ] dip [ > ] keep swap [ head-slice >string ] [ drop ] if ;
2008-09-16 20:26:28 -04:00
: >exponential ( n -- base exp )
0
[ swap dup [ 10.0 > ] keep 1.0 < or ]
[ dup 10.0 >
[ 10.0 / [ 1+ ] dip swap ]
[ 10.0 * [ 1- ] dip swap ] if
] [ swap ] while
[ number>string ] dip
dup abs number>string 2 CHAR: 0 pad-left
[ 0 < [ "-" ] [ "+" ] if ] dip append
"e" prepend ;
2008-09-11 21:16:35 -04:00
2008-09-16 20:26:28 -04:00
EBNF: parse-format-string
2008-09-11 21:16:35 -04:00
2008-09-16 20:26:28 -04:00
plain-text = (!("%").)+ => [[ >string 1quotation ]]
2008-09-11 21:16:35 -04:00
2008-09-16 20:26:28 -04:00
percents = "%" => [[ '[ "%" ] ]]
2008-09-11 21:16:35 -04:00
2008-09-16 20:26:28 -04:00
pad-zero = "0" => [[ CHAR: 0 ]]
pad-char = "'" (.) => [[ second ]]
pad-char_ = (pad-zero|pad-char)? => [[ CHAR: \s or 1quotation ]]
pad-align = ("-")? => [[ [ [ pad-right ] ] [ [ pad-left ] ] if ]]
pad-width = ([0-9])* => [[ >digits 1quotation ]]
pad = pad-align pad-char_ pad-width => [[ reverse compose-all ]]
2008-09-16 20:26:28 -04:00
width = "." ([0-9])* => [[ second >digits '[ _ max-width ] ]]
2008-09-16 20:26:28 -04:00
width_ = (width)? => [[ [ ] or ]]
digits = "." ([0-9])* => [[ second >digits '[ _ max-digits ] ]]
2008-09-16 20:26:28 -04:00
digits_ = (digits)? => [[ [ ] or ]]
fmt-c = "c" => [[ [ 1string ] ]]
fmt-C = "C" => [[ [ 1string >upper ] ]]
chars = (fmt-c | fmt-C) => [[ '[ _ apply-format ] ]]
2008-09-16 20:26:28 -04:00
fmt-s = "s" => [[ [ ] ]]
fmt-S = "S" => [[ [ >upper ] ]]
strings = pad width_ (fmt-s | fmt-S) => [[ reverse compose-all '[ _ apply-format ] ]]
2008-09-16 20:26:28 -04:00
fmt-d = "d" => [[ [ >fixnum number>string ] ]]
decimals = fmt-d
fmt-e = "e" => [[ [ >exponential ] ]]
fmt-E = "E" => [[ [ >exponential >upper ] ]]
exps = digits_ (fmt-e | fmt-E) => [[ reverse [ swap ] join [ swap append ] append ]]
2008-09-16 20:26:28 -04:00
fmt-f = "f" => [[ [ >float number>string ] ]]
floats = digits_ fmt-f => [[ reverse compose-all ]]
2008-09-16 20:26:28 -04:00
fmt-x = "x" => [[ [ >hex ] ]]
fmt-X = "X" => [[ [ >hex >upper ] ]]
hex = fmt-x | fmt-X
numbers = (pad) (decimals|floats|hex|exps) => [[ reverse compose-all [ fix-neg ] append '[ _ apply-format ] ]]
formats = "%" (chars|strings|numbers|percents) => [[ second ]]
text = (formats|plain-text)* => [[ write-all compose-all ]]
2008-09-16 20:26:28 -04:00
;EBNF
PRIVATE>
2008-09-11 21:16:35 -04:00
MACRO: printf ( format-string -- )
parse-format-string '[ reverse >vector @ drop ] ;
2008-09-11 21:16:35 -04:00
: sprintf ( params format-string -- result )
[ printf ] with-string-writer ;
2008-09-11 21:16:35 -04:00
: fprintf ( filename params format-string -- )
rot ascii [ printf ] with-file-appender ;
2008-09-11 21:16:35 -04:00