1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: io io.encodings.ascii io.files io.streams.string combinators
5 kernel sequences splitting strings math math.functions math.parser
6 macros fry peg.ebnf ascii unicode.case arrays quotations vectors ;
12 : compose-all ( seq -- quot )
13 [ ] [ compose ] reduce ;
15 : fix-sign ( string -- string )
16 dup CHAR: 0 swap index 0 =
17 [ dup 0 swap [ [ CHAR: 0 = not ] keep digit? and ] find-from
18 [ dup 1- rot dup [ nth ] dip swap
20 { CHAR: - [ [ 1- ] dip remove-nth "-" prepend ] }
21 { CHAR: + [ [ 1- ] dip remove-nth "+" prepend ] }
27 : >digits ( string -- digits )
28 [ 0 ] [ string>number ] if-empty ;
30 : pad-digits ( string digits -- string' )
31 [ "." split1 ] dip [ CHAR: 0 pad-right ] [ head-slice ] bi "." glue ;
33 : max-digits ( n digits -- n' )
34 10 swap ^ [ * round ] keep / ;
36 : max-width ( string length -- string' )
39 : >exp ( x -- exp base )
42 [ dup [ 10.0 >= ] [ 1.0 < ] bi or ]
45 [ 10.0 * [ 1- ] dip ] if
47 ] keep 0 < [ neg ] when ;
49 : exp>string ( exp base digits -- string )
50 [ max-digits ] keep -rot
53 [ abs number>string 2 CHAR: 0 pad-left ] bi
57 rot pad-digits prepend ;
59 EBNF: parse-format-string
61 zero = "0" => [[ CHAR: 0 ]]
62 char = "'" (.) => [[ second ]]
64 pad-char = (zero|char)? => [[ CHAR: \s or ]]
65 pad-align = ("-")? => [[ \ pad-right \ pad-left ? ]]
66 pad-width = ([0-9])* => [[ >digits ]]
67 pad = pad-align pad-char pad-width => [[ reverse >quotation dup first 0 = [ drop [ ] ] when ]]
69 sign = ("+")? => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]]
71 width_ = "." ([0-9])* => [[ second >digits '[ _ max-width ] ]]
72 width = (width_)? => [[ [ ] or ]]
74 digits_ = "." ([0-9])* => [[ second >digits ]]
75 digits = (digits_)? => [[ 6 or ]]
77 fmt-% = "%" => [[ [ "%" ] ]]
78 fmt-c = "c" => [[ [ 1string ] ]]
79 fmt-C = "C" => [[ [ 1string >upper ] ]]
80 fmt-s = "s" => [[ [ ] ]]
81 fmt-S = "S" => [[ [ >upper ] ]]
82 fmt-d = "d" => [[ [ >fixnum number>string ] ]]
83 fmt-e = digits "e" => [[ first '[ >exp _ exp>string ] ]]
84 fmt-E = digits "E" => [[ first '[ >exp _ exp>string >upper ] ]]
85 fmt-f = digits "f" => [[ first dup '[ >float _ max-digits number>string _ pad-digits ] ]]
86 fmt-x = "x" => [[ [ >hex ] ]]
87 fmt-X = "X" => [[ [ >hex >upper ] ]]
88 unknown = (.)* => [[ "Unknown directive" throw ]]
90 strings_ = fmt-c|fmt-C|fmt-s|fmt-S
91 strings = pad width strings_ => [[ reverse compose-all ]]
93 numbers_ = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
94 numbers = sign pad numbers_ => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
96 formats = "%" (strings|numbers|fmt-%|unknown) => [[ second '[ _ dip ] ]]
98 plain-text = (!("%").)+ => [[ >string '[ _ swap ] ]]
100 text = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
106 MACRO: printf ( format-string -- )
107 parse-format-string [ length ] keep compose-all '[ _ <vector> @ reverse [ write ] each ] ;
109 : sprintf ( format-string -- result )
110 [ printf ] with-string-writer ; inline