1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3 USING: accessors arrays assocs calendar combinators fry kernel
4 generalizations io io.streams.string macros math math.functions
5 math.parser peg.ebnf quotations sequences splitting strings
6 unicode.categories unicode.case vectors combinators.smart
8 FROM: math.parser.private => format-float ;
13 : compose-all ( seq -- quot )
14 [ ] [ compose ] reduce ;
16 : fix-sign ( string -- string )
17 dup CHAR: 0 swap index 0 =
18 [ dup 0 swap [ [ CHAR: 0 = not ] keep digit? and ] find-from
19 [ dup 1 - rot dup [ nth ] dip swap
21 { CHAR: - [ [ 1 - ] dip remove-nth "-" prepend ] }
22 { CHAR: + [ [ 1 - ] dip remove-nth "+" prepend ] }
28 : >digits ( string -- digits )
29 [ 0 ] [ string>number ] if-empty ;
31 : format-simple ( x digits string -- string )
32 [ [ >float ] [ number>string ] bi* "%." ] dip
33 surround 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 => [[ reverse >quotation dup first 0 = [ drop [ ] ] when ]]
51 sign = ("+")? => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]]
53 width_ = "." ([0-9])* => [[ second >digits '[ _ short head ] ]]
54 width = (width_)? => [[ [ ] or ]]
56 digits_ = "." ([0-9])* => [[ second >digits ]]
57 digits = (digits_)? => [[ 6 or ]]
59 fmt-% = "%" => [[ [ "%" ] ]]
60 fmt-c = "c" => [[ [ 1string ] ]]
61 fmt-C = "C" => [[ [ 1string >upper ] ]]
62 fmt-s = "s" => [[ [ present ] ]]
63 fmt-S = "S" => [[ [ present >upper ] ]]
64 fmt-d = "d" => [[ [ >integer number>string ] ]]
65 fmt-e = digits "e" => [[ first '[ _ format-scientific ] ]]
66 fmt-E = digits "E" => [[ first '[ _ format-scientific >upper ] ]]
67 fmt-f = digits "f" => [[ first '[ _ format-decimal ] ]]
68 fmt-x = "x" => [[ [ >hex ] ]]
69 fmt-X = "X" => [[ [ >hex >upper ] ]]
70 unknown = (.)* => [[ unknown-printf-directive ]]
72 strings_ = fmt-c|fmt-C|fmt-s|fmt-S
73 strings = pad width strings_ => [[ reverse compose-all ]]
75 numbers_ = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
76 numbers = sign pad numbers_ => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
78 types = strings|numbers
80 lists = "[%" types ", %]" => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
82 assocs = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
84 formats = "%" (types|fmt-%|lists|assocs|unknown) => [[ second '[ _ dip ] ]]
86 plain-text = (!("%").)+ => [[ >string '[ _ swap ] ]]
88 text = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
94 MACRO: printf ( format-string -- )
95 parse-printf [ length ] keep compose-all '[ _ <vector> @ reverse [ write ] each ] ;
97 : sprintf ( format-string -- result )
98 [ printf ] with-string-writer ; inline
102 : pad-00 ( n -- string ) number>string 2 CHAR: 0 pad-head ; inline
104 : pad-000 ( n -- string ) number>string 3 CHAR: 0 pad-head ; inline
106 : >time ( timestamp -- string )
107 [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
108 [ pad-00 ] map ":" join ; inline
110 : >date ( timestamp -- string )
111 [ month>> ] [ day>> ] [ year>> ] tri 3array
112 [ pad-00 ] map "/" join ; inline
114 : >datetime ( timestamp -- string )
117 [ day-of-week day-abbreviation3 ]
118 [ month>> month-abbreviation ]
121 [ year>> number>string ]
123 ] output>array " " join ; inline
125 : (week-of-year) ( timestamp day -- n )
126 [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
127 [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
129 : week-of-year-sunday ( timestamp -- n ) 0 (week-of-year) ; inline
131 : week-of-year-monday ( timestamp -- n ) 1 (week-of-year) ; inline
135 fmt-% = "%" => [[ [ "%" ] ]]
136 fmt-a = "a" => [[ [ dup day-of-week day-abbreviation3 ] ]]
137 fmt-A = "A" => [[ [ dup day-of-week day-name ] ]]
138 fmt-b = "b" => [[ [ dup month>> month-abbreviation ] ]]
139 fmt-B = "B" => [[ [ dup month>> month-name ] ]]
140 fmt-c = "c" => [[ [ dup >datetime ] ]]
141 fmt-d = "d" => [[ [ dup day>> pad-00 ] ]]
142 fmt-H = "H" => [[ [ dup hour>> pad-00 ] ]]
143 fmt-I = "I" => [[ [ dup hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
144 fmt-j = "j" => [[ [ dup day-of-year pad-000 ] ]]
145 fmt-m = "m" => [[ [ dup month>> pad-00 ] ]]
146 fmt-M = "M" => [[ [ dup minute>> pad-00 ] ]]
147 fmt-p = "p" => [[ [ dup hour>> 12 < "AM" "PM" ? ] ]]
148 fmt-S = "S" => [[ [ dup second>> floor pad-00 ] ]]
149 fmt-U = "U" => [[ [ dup week-of-year-sunday pad-00 ] ]]
150 fmt-w = "w" => [[ [ dup day-of-week number>string ] ]]
151 fmt-W = "W" => [[ [ dup week-of-year-monday pad-00 ] ]]
152 fmt-x = "x" => [[ [ dup >date ] ]]
153 fmt-X = "X" => [[ [ dup >time ] ]]
154 fmt-y = "y" => [[ [ dup year>> 100 mod pad-00 ] ]]
155 fmt-Y = "Y" => [[ [ dup year>> number>string ] ]]
156 fmt-Z = "Z" => [[ [ "Not yet implemented" throw ] ]]
157 unknown = (.)* => [[ "Unknown directive" throw ]]
159 formats_ = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
160 fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
161 fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
163 formats = "%" (formats_) => [[ second '[ _ dip ] ]]
165 plain-text = (!("%").)+ => [[ >string '[ _ swap ] ]]
167 text = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
173 MACRO: strftime ( format-string -- )
174 parse-strftime [ length ] keep [ ] join
175 '[ _ <vector> @ reverse concat nip ] ;