1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3 USING: accessors arrays assocs calendar calendar.english 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 vectors
8 math.functions.integer-logs splitting ;
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-decimal-simple ( x digits -- string )
35 [ 10^ * round-to-even >integer number>string ]
36 [ 1 + CHAR: 0 pad-head ]
37 [ cut* ] tri [ "." glue ] unless-empty
38 ] curry keep neg? [ CHAR: - prefix ] when ;
40 : format-scientific-mantissa ( x log10x digits -- string rounded-up? )
41 [ swap - 10^ * round-to-even >integer number>string ] keep
43 [ but-last >string ] when ! 9.9 rounded to 1e+01
44 1 cut [ "." glue ] unless-empty
47 : format-scientific-exponent ( rounded-up? log10x -- string )
48 swap [ 1 + ] when number>string 2 CHAR: 0 pad-head
49 dup CHAR: - swap index "e" "e+" ? prepend ;
51 : format-scientific-simple ( x digits -- string )
53 [ abs dup integer-log10 ] dip
54 [ format-scientific-mantissa ]
55 [ drop nip format-scientific-exponent ] 3bi append
56 ] curry keep neg? [ CHAR: - prefix ] when ;
58 : format-float-fast ( x digits string -- string )
59 [ "" -1 ] 2dip "C" format-float ;
61 : format-fast-scientific? ( x digits -- x' digits ? )
64 [ abs integer-log10 abs 308 < ]
66 [ [ [ >float ] dip ] when ] keep
69 : ?fix-nonsignificant-zero ( string digits -- string )
70 [ ".0" "" replace ] [ drop ] if-zero ;
72 : format-scientific ( x digits -- string )
73 format-fast-scientific? [
74 [ "e" format-float-fast ]
75 [ ?fix-nonsignificant-zero ] bi
76 ] [ format-scientific-simple ] if ;
78 : format-fast-decimal? ( x digits -- x' digits ? )
82 [ drop dup integer? [ abs 53 2^ < ] [ drop f ] if ]
84 [ [ abs integer-log10 ] dip
85 [ drop abs 308 < ] [ + 15 <= ] 2bi and ]
88 [ [ [ >float ] dip ] when ] keep
91 : format-decimal ( x digits -- string )
92 format-fast-decimal? [
93 [ "f" format-float-fast ]
94 [ ?fix-nonsignificant-zero ] bi
95 ] [ format-decimal-simple ] if ;
97 ERROR: unknown-printf-directive ;
101 zero = "0" => [[ CHAR: 0 ]]
102 char = "'" (.) => [[ second ]]
104 pad-char = (zero|char)? => [[ CHAR: \s or ]]
105 pad-align = ("-")? => [[ \ pad-tail \ pad-head ? ]]
106 pad-width = ([0-9])* => [[ >digits ]]
107 pad = pad-align pad-char pad-width => [[ <reversed> >quotation dup first 0 = [ drop [ ] ] when ]]
109 sign_ = [+ ] => [[ '[ dup first CHAR: - = [ _ prefix ] unless ] ]]
110 sign = (sign_)? => [[ [ ] or ]]
112 width_ = "." ([0-9])* => [[ second >digits '[ _ short head ] ]]
113 width = (width_)? => [[ [ ] or ]]
115 digits_ = "." ([0-9])* => [[ second >digits ]]
116 digits = (digits_)? => [[ 6 or ]]
118 fmt-% = "%" => [[ "%" ]]
119 fmt-c = "c" => [[ [ 1string ] ]]
120 fmt-C = "C" => [[ [ 1string >upper ] ]]
121 fmt-s = "s" => [[ [ present ] ]]
122 fmt-S = "S" => [[ [ present >upper ] ]]
123 fmt-u = "u" => [[ [ unparse ] ]]
124 fmt-d = "d" => [[ [ >integer number>string ] ]]
125 fmt-o = "o" => [[ [ >integer >oct ] ]]
126 fmt-b = "b" => [[ [ >integer >bin ] ]]
127 fmt-e = digits "e" => [[ first '[ _ format-scientific ] ]]
128 fmt-E = digits "E" => [[ first '[ _ format-scientific >upper ] ]]
129 fmt-f = digits "f" => [[ first '[ _ format-decimal ] ]]
130 fmt-x = "x" => [[ [ >integer >hex ] ]]
131 fmt-X = "X" => [[ [ >integer >hex >upper ] ]]
132 unknown = (.)* => [[ unknown-printf-directive ]]
134 strings_ = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
135 strings = pad width strings_ => [[ <reversed> compose-all ]]
137 numbers_ = fmt-d|fmt-o|fmt-b|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
138 numbers = sign pad numbers_ => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
140 types = strings|numbers
142 lists = "[%" types ", %]" => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
144 assocs = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
146 formats = "%" (types|fmt-%|lists|assocs|unknown) => [[ second ]]
148 plain-text = (!("%").)+ => [[ >string ]]
150 text = (formats|plain-text)* => [[ ]]
154 : printf-quot ( format-string -- format-quot n )
155 parse-printf [ [ callable? ] count ] keep [
156 dup string? [ 1quotation ] [ [ 1 - ] dip ] if
158 ] map nip [ compose-all ] [ length ] bi ; inline
162 MACRO: printf ( format-string -- quot )
164 @ output-stream get [ stream-write ] curry _ napply
167 MACRO: sprintf ( format-string -- quot )
172 : vprintf ( seq format-string -- )
173 parse-printf output-stream get '[
175 [ unclip-slice ] dip call( x -- y )
176 ] unless _ stream-write
179 : vsprintf ( seq format-string -- result )
180 [ vprintf ] with-string-writer ; inline
184 : pad-00 ( n -- string )
185 number>string 2 CHAR: 0 pad-head ; inline
187 : pad-000 ( n -- string )
188 number>string 3 CHAR: 0 pad-head ; inline
190 : >time ( timestamp -- string )
191 [ hour>> ] [ minute>> ] [ second>> floor ] tri
192 [ pad-00 ] tri@ 3array ":" join ; inline
194 : >date ( timestamp -- string )
195 [ month>> ] [ day>> ] [ year>> ] tri
196 [ pad-00 ] tri@ 3array "/" join ; inline
198 : >datetime ( timestamp -- string )
201 [ day-of-week day-abbreviation3 ]
202 [ month>> month-abbreviation ]
205 [ year>> number>string ]
207 ] output>array " " join ; inline
209 : week-of-year ( timestamp day -- n )
210 [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
211 [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
213 : week-of-year-sunday ( timestamp -- n ) 0 week-of-year ; inline
215 : week-of-year-monday ( timestamp -- n ) 1 week-of-year ; inline
219 fmt-% = "%" => [[ "%" ]]
220 fmt-a = "a" => [[ [ day-of-week day-abbreviation3 ] ]]
221 fmt-A = "A" => [[ [ day-of-week day-name ] ]]
222 fmt-b = "b" => [[ [ month>> month-abbreviation ] ]]
223 fmt-B = "B" => [[ [ month>> month-name ] ]]
224 fmt-c = "c" => [[ [ >datetime ] ]]
225 fmt-d = "d" => [[ [ day>> pad-00 ] ]]
226 fmt-H = "H" => [[ [ hour>> pad-00 ] ]]
227 fmt-I = "I" => [[ [ hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
228 fmt-j = "j" => [[ [ day-of-year pad-000 ] ]]
229 fmt-m = "m" => [[ [ month>> pad-00 ] ]]
230 fmt-M = "M" => [[ [ minute>> pad-00 ] ]]
231 fmt-p = "p" => [[ [ hour>> 12 < "AM" "PM" ? ] ]]
232 fmt-S = "S" => [[ [ second>> floor pad-00 ] ]]
233 fmt-U = "U" => [[ [ week-of-year-sunday pad-00 ] ]]
234 fmt-w = "w" => [[ [ day-of-week number>string ] ]]
235 fmt-W = "W" => [[ [ week-of-year-monday pad-00 ] ]]
236 fmt-x = "x" => [[ [ >date ] ]]
237 fmt-X = "X" => [[ [ >time ] ]]
238 fmt-y = "y" => [[ [ year>> 100 mod pad-00 ] ]]
239 fmt-Y = "Y" => [[ [ year>> number>string ] ]]
240 fmt-Z = "Z" => [[ [ "Not yet implemented" throw ] ]]
241 unknown = (.)* => [[ "Unknown directive" throw ]]
243 formats_ = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
244 fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
245 fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
247 formats = "%" (formats_) => [[ second ]]
249 plain-text = (!("%").)+ => [[ >string ]]
251 text = (formats|plain-text)* => [[ ]]
257 MACRO: strftime ( format-string -- quot )
262 '[ over @ swap push-all ]
265 SBUF" " clone [ _ cleave drop ] keep "" like