1 ! Copyright (C) 2008 John Benediktsson
2 ! See https://factorcode.org/license.txt for BSD license
3 USING: accessors arrays assocs calendar calendar.english
4 calendar.private combinators combinators.smart generalizations
5 io io.streams.string kernel math math.functions math.parser
6 multiline namespaces peg.ebnf present prettyprint quotations
7 sequences sequences.generalizations splitting strings unicode ;
8 FROM: math.parser.private => format-float ;
11 ERROR: unknown-format-directive value ;
15 : compose-all ( seq -- quot )
16 [ ] [ compose ] reduce ; inline
18 : fix-sign ( string -- string )
20 dup [ [ CHAR: 0 = not ] [ digit? ] bi and ] find
23 { CHAR: - [ remove-nth "-" prepend ] }
24 { CHAR: + [ remove-nth "+" prepend ] }
30 : >digits ( string -- digits )
31 [ 0 ] [ string>number ] if-empty ;
33 : format-decimal-simple ( x digits -- string )
36 [ 10^ * round-to-even >integer number>string ]
37 [ 1 + CHAR: 0 pad-head ]
38 [ cut* ] tri [ "." glue ] unless-empty
39 ] keepd neg? [ CHAR: - prefix ] when ;
41 : format-scientific-mantissa ( x log10x digits -- string rounded-up? )
42 [ swap - 10^ * round-to-even >integer number>string ] keep
44 [ but-last >string ] when ! 9.9 rounded to 1e+01
45 1 cut [ "." glue ] unless-empty
48 : format-scientific-exponent ( rounded-up? log10x -- string )
49 swap [ 1 + ] when number>string 2 CHAR: 0 pad-head
50 dup CHAR: - swap index "e" "e+" ? prepend ;
52 : format-scientific-simple ( x digits -- string )
54 [ abs dup integer-log10 ] dip
55 [ format-scientific-mantissa ]
56 [ drop nip format-scientific-exponent ] 3bi append
57 ] keepd neg? [ CHAR: - prefix ] when ;
59 : format-float-fast ( x digits string -- string )
60 [ "" -1 ] 2dip "C" format-float ;
62 : format-fast-scientific? ( x digits -- x' digits ? )
65 [ [ t ] [ abs integer-log10 abs 308 < ] if-zero ]
67 [ [ [ >float ] dip ] when ] keep
70 : format-scientific ( x digits -- string )
71 format-fast-scientific? [
72 [ "e" format-float-fast ]
73 [ [ ".0e" "e" replace ] [ drop ] if-zero ] bi
74 ] [ format-scientific-simple ] if ;
76 : format-fast-decimal? ( x digits -- x' digits ? )
80 [ drop dup integer? [ abs 53 2^ < ] [ drop f ] if ]
82 [ [ abs integer-log10 ] dip
83 [ drop abs 308 < ] [ + 15 <= ] 2bi and ]
86 [ [ [ >float ] dip ] when ] keep
89 : format-decimal ( x digits -- string )
90 format-fast-decimal? [
91 [ "f" format-float-fast ]
92 [ [ ".0" ?tail drop ] [ drop ] if-zero ] bi
93 ] [ format-decimal-simple ] if ;
95 EBNF: parse-printf [=[
97 zero = "0" => [[ CHAR: 0 ]]
98 char = "'" (.) => [[ second ]]
100 pad-char = (zero|char)? => [[ CHAR: \s or ]]
101 pad-align = ("-")? => [[ \ pad-tail \ pad-head ? ]]
102 pad-width = ([0-9])* => [[ >digits ]]
103 pad = pad-align pad-char pad-width => [[ <reversed> >quotation dup first 0 = [ drop [ ] ] when ]]
105 sign_ = [+ ] => [[ '[ dup first CHAR: - = [ _ prefix ] unless ] ]]
106 sign = (sign_)? => [[ [ ] or ]]
108 width_ = "." ([0-9])* => [[ second >digits '[ _ index-or-length head ] ]]
109 width = (width_)? => [[ [ ] or ]]
111 digits_ = "." ([0-9])* => [[ second >digits ]]
112 digits = (digits_)? => [[ 6 or ]]
114 fmt-% = "%" => [[ "%" ]]
115 fmt-c = "c" => [[ [ 1string ] ]]
116 fmt-C = "C" => [[ [ 1string >upper ] ]]
117 fmt-s = "s" => [[ [ present ] ]]
118 fmt-S = "S" => [[ [ present >upper ] ]]
119 fmt-u = "u" => [[ [ unparse ] ]]
120 fmt-d = "d" => [[ [ >integer number>string ] ]]
121 fmt-o = "o" => [[ [ >integer >oct ] ]]
122 fmt-b = "b" => [[ [ >integer >bin ] ]]
123 fmt-e = digits "e" => [[ first '[ _ format-scientific ] ]]
124 fmt-E = digits "E" => [[ first '[ _ format-scientific >upper ] ]]
125 fmt-f = digits "f" => [[ first '[ _ format-decimal ] ]]
126 fmt-x = "x" => [[ [ >integer >hex ] ]]
127 fmt-X = "X" => [[ [ >integer >hex >upper ] ]]
128 unknown = (.)* => [[ "" like unknown-format-directive ]]
130 strings_ = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
131 strings = pad width strings_ => [[ <reversed> compose-all ]]
133 numbers_ = fmt-d|fmt-o|fmt-b|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
134 numbers = sign pad numbers_ => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
136 types = strings|numbers
138 lists = "[%" types ", %]" => [[ second '[ _ { } map-as ", " join "{ " " }" surround ] ]]
140 assocs = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ [ _ _ bi* ":" glue ] { } assoc>map ", " join "{ " " }" surround ] ]]
142 formats = "%" (types|fmt-%|lists|assocs|unknown) => [[ second ]]
144 plain-text = [^%]+ => [[ >string ]]
146 text = (formats|plain-text)*
150 : printf-quot ( format-string -- format-quot n )
151 parse-printf [ [ callable? ] count ] keep [
152 dup string? [ 1quotation ] [ [ 1 - ] dip ] if
154 ] map nip [ compose-all ] [ length ] bi ; inline
158 MACRO: printf ( format-string -- quot )
160 @ output-stream get [ stream-write ] curry _ napply
163 MACRO: sprintf ( format-string -- quot )
168 : vprintf ( seq format-string -- )
169 parse-printf output-stream get '[
171 [ unclip-slice ] dip call( x -- y )
172 ] unless _ stream-write
175 : vsprintf ( seq format-string -- result )
176 [ vprintf ] with-string-writer ; inline
180 : pad-00 ( n -- string )
181 number>string 2 CHAR: 0 pad-head ; inline
183 : pad-000 ( n -- string )
184 number>string 3 CHAR: 0 pad-head ; inline
186 : >time ( timestamp -- string )
187 [ hour>> ] [ minute>> ] [ second>> floor ] tri
188 [ pad-00 ] tri@ 3array ":" join ; inline
190 : >date ( timestamp -- string )
191 [ month>> ] [ day>> ] [ year>> ] tri
192 [ pad-00 ] tri@ 3array "/" join ; inline
194 : >datetime ( timestamp -- string )
197 [ day-of-week day-abbreviation3 ]
198 [ month>> month-abbreviation ]
201 [ year>> number>string ]
203 ] output>array join-words ; inline
205 : week-of-year ( timestamp day -- n )
206 [ dup clone first-day-of-year dup clone ]
207 [ day-this-week ] bi* swap '[ _ time- duration>days ] bi@
208 dup 0 < [ 7 + - ] [ drop ] if 7 + 7 /i ;
210 : week-of-year-sunday ( timestamp -- n ) 0 week-of-year ; inline
212 : week-of-year-monday ( timestamp -- n ) 1 week-of-year ; inline
214 EBNF: parse-strftime [=[
216 fmt-% = "%" => [[ "%" ]]
217 fmt-a = "a" => [[ [ day-of-week day-abbreviation3 ] ]]
218 fmt-A = "A" => [[ [ day-of-week day-name ] ]]
219 fmt-b = "b" => [[ [ month>> month-abbreviation ] ]]
220 fmt-B = "B" => [[ [ month>> month-name ] ]]
221 fmt-c = "c" => [[ [ >datetime ] ]]
222 fmt-d = "d" => [[ [ day>> pad-00 ] ]]
223 fmt-H = "H" => [[ [ hour>> pad-00 ] ]]
224 fmt-I = "I" => [[ [ hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
225 fmt-j = "j" => [[ [ day-of-year pad-000 ] ]]
226 fmt-m = "m" => [[ [ month>> pad-00 ] ]]
227 fmt-M = "M" => [[ [ minute>> pad-00 ] ]]
228 fmt-p = "p" => [[ [ hour>> 12 < "AM" "PM" ? ] ]]
229 fmt-S = "S" => [[ [ second>> floor pad-00 ] ]]
230 fmt-U = "U" => [[ [ week-of-year-sunday pad-00 ] ]]
231 fmt-w = "w" => [[ [ day-of-week number>string ] ]]
232 fmt-W = "W" => [[ [ week-of-year-monday pad-00 ] ]]
233 fmt-x = "x" => [[ [ >date ] ]]
234 fmt-X = "X" => [[ [ >time ] ]]
235 fmt-y = "y" => [[ [ year>> 100 mod pad-00 ] ]]
236 fmt-Y = "Y" => [[ [ year>> number>string ] ]]
237 fmt-Z = "Z" => [[ [ "Not yet implemented" throw ] ]]
238 unknown = (.)* => [[ "" like unknown-format-directive ]]
240 formats_ = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
241 fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
242 fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
244 formats = "%" (formats_) => [[ second ]]
246 plain-text = [^%]+ => [[ >string ]]
248 text = (formats|plain-text)*
254 MACRO: strftime ( format-string -- quot )
261 ] map concat '[ SBUF" " clone @ nip "" like ] ;