1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays ascii calendar combinators fry kernel
5 io io.encodings.ascii io.files io.streams.string
6 macros math math.functions math.parser peg.ebnf quotations
7 sequences splitting strings unicode.case vectors ;
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 : pad-digits ( string digits -- string' )
32 [ "." split1 ] dip [ CHAR: 0 pad-right ] [ head-slice ] bi "." glue ;
34 : max-digits ( n digits -- n' )
35 10 swap ^ [ * round ] keep / ;
37 : max-width ( string length -- string' )
40 : >exp ( x -- exp base )
43 [ dup [ 10.0 >= ] [ 1.0 < ] bi or ]
46 [ 10.0 * [ 1- ] dip ] if
48 ] keep 0 < [ neg ] when ;
50 : exp>string ( exp base digits -- string )
51 [ max-digits ] keep -rot
54 [ abs number>string 2 CHAR: 0 pad-left ] bi
58 rot pad-digits prepend ;
62 zero = "0" => [[ CHAR: 0 ]]
63 char = "'" (.) => [[ second ]]
65 pad-char = (zero|char)? => [[ CHAR: \s or ]]
66 pad-align = ("-")? => [[ \ pad-right \ pad-left ? ]]
67 pad-width = ([0-9])* => [[ >digits ]]
68 pad = pad-align pad-char pad-width => [[ reverse >quotation dup first 0 = [ drop [ ] ] when ]]
70 sign = ("+")? => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]]
72 width_ = "." ([0-9])* => [[ second >digits '[ _ max-width ] ]]
73 width = (width_)? => [[ [ ] or ]]
75 digits_ = "." ([0-9])* => [[ second >digits ]]
76 digits = (digits_)? => [[ 6 or ]]
78 fmt-% = "%" => [[ [ "%" ] ]]
79 fmt-c = "c" => [[ [ 1string ] ]]
80 fmt-C = "C" => [[ [ 1string >upper ] ]]
81 fmt-s = "s" => [[ [ ] ]]
82 fmt-S = "S" => [[ [ >upper ] ]]
83 fmt-d = "d" => [[ [ >fixnum number>string ] ]]
84 fmt-e = digits "e" => [[ first '[ >exp _ exp>string ] ]]
85 fmt-E = digits "E" => [[ first '[ >exp _ exp>string >upper ] ]]
86 fmt-f = digits "f" => [[ first dup '[ >float _ max-digits number>string _ pad-digits ] ]]
87 fmt-x = "x" => [[ [ >hex ] ]]
88 fmt-X = "X" => [[ [ >hex >upper ] ]]
89 unknown = (.)* => [[ "Unknown directive" throw ]]
91 strings_ = fmt-c|fmt-C|fmt-s|fmt-S
92 strings = pad width strings_ => [[ reverse compose-all ]]
94 numbers_ = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
95 numbers = sign pad numbers_ => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
97 formats = "%" (strings|numbers|fmt-%|unknown) => [[ second '[ _ dip ] ]]
99 plain-text = (!("%").)+ => [[ >string '[ _ swap ] ]]
101 text = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
107 MACRO: printf ( format-string -- )
108 parse-printf [ length ] keep compose-all '[ _ <vector> @ reverse [ write ] each ] ;
110 : sprintf ( format-string -- result )
111 [ printf ] with-string-writer ; inline
116 : zero-pad ( str -- str' ) 2 CHAR: 0 pad-left ; inline
118 : >time ( timestamp -- string )
119 [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
120 [ number>string zero-pad ] map ":" join ; inline
122 : >date ( timestamp -- string )
123 [ month>> ] [ day>> ] [ year>> ] tri 3array
124 [ number>string zero-pad ] map "/" join ; inline
126 : >datetime ( timestamp -- string )
127 { [ day-of-week day-abbreviation3 ]
128 [ month>> month-abbreviation ]
129 [ day>> number>string zero-pad ]
131 [ year>> number>string ]
132 } cleave 3array [ 2array ] dip append " " join ; inline
134 : (week-of-year) ( timestamp day -- n )
135 [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
136 [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1+ >fixnum ] if ;
138 : week-of-year-sunday ( timestamp -- n ) 0 (week-of-year) ; inline
140 : week-of-year-monday ( timestamp -- n ) 1 (week-of-year) ; inline
144 fmt-% = "%" => [[ [ "%" ] ]]
145 fmt-a = "a" => [[ [ dup day-of-week day-abbreviation3 ] ]]
146 fmt-A = "A" => [[ [ dup day-of-week day-name ] ]]
147 fmt-b = "b" => [[ [ dup month>> month-abbreviation ] ]]
148 fmt-B = "B" => [[ [ dup month>> month-name ] ]]
149 fmt-c = "c" => [[ [ dup >datetime ] ]]
150 fmt-d = "d" => [[ [ dup day>> number>string zero-pad ] ]]
151 fmt-H = "H" => [[ [ dup hour>> number>string zero-pad ] ]]
152 fmt-I = "I" => [[ [ dup hour>> dup 12 > [ 12 - ] when number>string zero-pad ] ]]
153 fmt-j = "j" => [[ [ dup day-of-year number>string ] ]]
154 fmt-m = "m" => [[ [ dup month>> number>string zero-pad ] ]]
155 fmt-M = "M" => [[ [ dup minute>> number>string zero-pad ] ]]
156 fmt-p = "p" => [[ [ dup hour>> 12 < "AM" "PM" ? ] ]]
157 fmt-S = "S" => [[ [ dup second>> round number>string zero-pad ] ]]
158 fmt-U = "U" => [[ [ dup week-of-year-sunday ] ]]
159 fmt-w = "w" => [[ [ dup day-of-week number>string ] ]]
160 fmt-W = "W" => [[ [ dup week-of-year-monday ] ]]
161 fmt-x = "x" => [[ [ dup >date ] ]]
162 fmt-X = "X" => [[ [ dup >time ] ]]
163 fmt-y = "y" => [[ [ dup year>> 100 mod number>string ] ]]
164 fmt-Y = "Y" => [[ [ dup year>> number>string ] ]]
165 fmt-Z = "Z" => [[ [ "Not yet implemented" throw ] ]]
166 unknown = (.)* => [[ "Unknown directive" throw ]]
168 formats_ = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
169 fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
170 fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
172 formats = "%" (formats_) => [[ second '[ _ dip ] ]]
174 plain-text = (!("%").)+ => [[ >string '[ _ swap ] ]]
176 text = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
182 MACRO: strftime ( format-string -- )
183 parse-strftime [ length ] keep [ ] join
184 '[ _ <vector> @ reverse concat nip ] ;