1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3 USING: accessors arrays assocs calendar combinators
4 combinators.smart fry generalizations io io.streams.string
5 kernel macros math math.functions math.parser namespaces
6 peg.ebnf present prettyprint quotations sequences
7 sequences.generalizations strings unicode.case
8 unicode.categories vectors ;
9 FROM: math.parser.private => format-float ;
14 : compose-all ( seq -- quot )
15 [ ] [ compose ] reduce ; inline
17 : fix-sign ( string -- string )
19 dup [ [ CHAR: 0 = not ] [ digit? ] bi and ] find
22 { CHAR: - [ remove-nth "-" prepend ] }
23 { CHAR: + [ remove-nth "+" prepend ] }
29 : >digits ( string -- digits )
30 [ 0 ] [ string>number ] if-empty ;
32 : format-simple ( x digits string -- string )
33 [ >float "" -1 ] 2dip "C" format-float ;
35 : format-scientific ( x digits -- string ) "e" format-simple ;
37 : format-decimal ( x digits -- string ) "f" format-simple ;
39 ERROR: unknown-printf-directive ;
43 zero = "0" => [[ CHAR: 0 ]]
44 char = "'" (.) => [[ second ]]
46 pad-char = (zero|char)? => [[ CHAR: \s or ]]
47 pad-align = ("-")? => [[ \ pad-tail \ pad-head ? ]]
48 pad-width = ([0-9])* => [[ >digits ]]
49 pad = pad-align pad-char pad-width => [[ <reversed> >quotation dup first 0 = [ drop [ ] ] when ]]
51 sign_ = [+ ] => [[ '[ dup CHAR: - swap index [ _ prefix ] unless ] ]]
52 sign = (sign_)? => [[ [ ] or ]]
54 width_ = "." ([0-9])* => [[ second >digits '[ _ short head ] ]]
55 width = (width_)? => [[ [ ] or ]]
57 digits_ = "." ([0-9])* => [[ second >digits ]]
58 digits = (digits_)? => [[ 6 or ]]
60 fmt-% = "%" => [[ "%" ]]
61 fmt-c = "c" => [[ [ 1string ] ]]
62 fmt-C = "C" => [[ [ 1string >upper ] ]]
63 fmt-s = "s" => [[ [ present ] ]]
64 fmt-S = "S" => [[ [ present >upper ] ]]
65 fmt-u = "u" => [[ [ unparse ] ]]
66 fmt-d = "d" => [[ [ >integer number>string ] ]]
67 fmt-o = "o" => [[ [ >integer >oct ] ]]
68 fmt-b = "b" => [[ [ >integer >bin ] ]]
69 fmt-e = digits "e" => [[ first '[ _ format-scientific ] ]]
70 fmt-E = digits "E" => [[ first '[ _ format-scientific >upper ] ]]
71 fmt-f = digits "f" => [[ first '[ _ format-decimal ] ]]
72 fmt-x = "x" => [[ [ >hex ] ]]
73 fmt-X = "X" => [[ [ >hex >upper ] ]]
74 unknown = (.)* => [[ unknown-printf-directive ]]
76 strings_ = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
77 strings = pad width strings_ => [[ <reversed> compose-all ]]
79 numbers_ = fmt-d|fmt-o|fmt-b|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
80 numbers = sign pad numbers_ => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
82 types = strings|numbers
84 lists = "[%" types ", %]" => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
86 assocs = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
88 formats = "%" (types|fmt-%|lists|assocs|unknown) => [[ second ]]
90 plain-text = (!("%").)+ => [[ >string ]]
92 text = (formats|plain-text)* => [[ ]]
96 : printf-quot ( format-string -- format-quot n )
97 parse-printf [ [ callable? ] count ] keep [
98 dup string? [ 1quotation ] [ [ 1 - ] dip ] if
100 ] map nip [ compose-all ] [ length ] bi ; inline
104 MACRO: printf ( format-string -- quot )
106 @ output-stream get [ stream-write ] curry _ napply
109 MACRO: sprintf ( format-string -- quot )
114 : vprintf ( seq format-string -- )
115 parse-printf output-stream get '[
117 [ unclip-slice ] dip call( x -- y )
118 ] unless _ stream-write
121 : vsprintf ( seq format-string -- result )
122 [ vprintf ] with-string-writer ; inline
126 : pad-00 ( n -- string )
127 number>string 2 CHAR: 0 pad-head ; inline
129 : pad-000 ( n -- string )
130 number>string 3 CHAR: 0 pad-head ; inline
132 : >time ( timestamp -- string )
133 [ hour>> ] [ minute>> ] [ second>> floor ] tri
134 [ pad-00 ] tri@ 3array ":" join ; inline
136 : >date ( timestamp -- string )
137 [ month>> ] [ day>> ] [ year>> ] tri
138 [ pad-00 ] tri@ 3array "/" join ; inline
140 : >datetime ( timestamp -- string )
143 [ day-of-week day-abbreviation3 ]
144 [ month>> month-abbreviation ]
147 [ year>> number>string ]
149 ] output>array " " join ; inline
151 : week-of-year ( timestamp day -- n )
152 [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
153 [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
155 : week-of-year-sunday ( timestamp -- n ) 0 week-of-year ; inline
157 : week-of-year-monday ( timestamp -- n ) 1 week-of-year ; inline
161 fmt-% = "%" => [[ "%" ]]
162 fmt-a = "a" => [[ [ day-of-week day-abbreviation3 ] ]]
163 fmt-A = "A" => [[ [ day-of-week day-name ] ]]
164 fmt-b = "b" => [[ [ month>> month-abbreviation ] ]]
165 fmt-B = "B" => [[ [ month>> month-name ] ]]
166 fmt-c = "c" => [[ [ >datetime ] ]]
167 fmt-d = "d" => [[ [ day>> pad-00 ] ]]
168 fmt-H = "H" => [[ [ hour>> pad-00 ] ]]
169 fmt-I = "I" => [[ [ hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
170 fmt-j = "j" => [[ [ day-of-year pad-000 ] ]]
171 fmt-m = "m" => [[ [ month>> pad-00 ] ]]
172 fmt-M = "M" => [[ [ minute>> pad-00 ] ]]
173 fmt-p = "p" => [[ [ hour>> 12 < "AM" "PM" ? ] ]]
174 fmt-S = "S" => [[ [ second>> floor pad-00 ] ]]
175 fmt-U = "U" => [[ [ week-of-year-sunday pad-00 ] ]]
176 fmt-w = "w" => [[ [ day-of-week number>string ] ]]
177 fmt-W = "W" => [[ [ week-of-year-monday pad-00 ] ]]
178 fmt-x = "x" => [[ [ >date ] ]]
179 fmt-X = "X" => [[ [ >time ] ]]
180 fmt-y = "y" => [[ [ year>> 100 mod pad-00 ] ]]
181 fmt-Y = "Y" => [[ [ year>> number>string ] ]]
182 fmt-Z = "Z" => [[ [ "Not yet implemented" throw ] ]]
183 unknown = (.)* => [[ "Unknown directive" throw ]]
185 formats_ = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
186 fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
187 fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
189 formats = "%" (formats_) => [[ second ]]
191 plain-text = (!("%").)+ => [[ >string ]]
193 text = (formats|plain-text)* => [[ ]]
199 MACRO: strftime ( format-string -- quot )
204 '[ over @ swap push-all ]
207 SBUF" " clone [ _ cleave drop ] keep "" like