]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting.factor
formatting: fix missing '+' for %+e with negative exponent
[factor.git] / basis / formatting / formatting.factor
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 FROM: math.parser.private => format-float ;
9 IN: formatting
10
11 <PRIVATE
12
13 : compose-all ( seq -- quot )
14     [ ] [ compose ] reduce ; inline
15
16 : fix-sign ( string -- string )
17     dup first CHAR: 0 = [
18         dup [ [ CHAR: 0 = not ] [ digit? ] bi and ] find
19         [
20             1 - swap 2dup nth {
21                 { CHAR: - [ remove-nth "-" prepend ] }
22                 { CHAR: + [ remove-nth "+" prepend ] }
23                 [ drop nip ]
24             } case
25         ] [ drop ] if
26     ] when ;
27
28 : >digits ( string -- digits )
29     [ 0 ] [ string>number ] if-empty ;
30
31 : format-simple ( x digits string -- string )
32     [ >float "" -1 ] 2dip "C" format-float ;
33
34 : format-scientific ( x digits -- string ) "e" format-simple ;
35
36 : format-decimal ( x digits -- string ) "f" format-simple ;
37
38 ERROR: unknown-printf-directive ;
39
40 EBNF: parse-printf
41
42 zero      = "0"                  => [[ CHAR: 0 ]]
43 char      = "'" (.)              => [[ second ]]
44
45 pad-char  = (zero|char)?         => [[ CHAR: \s or ]]
46 pad-align = ("-")?               => [[ \ pad-tail \ pad-head ? ]]
47 pad-width = ([0-9])*             => [[ >digits ]]
48 pad       = pad-align pad-char pad-width => [[ <reversed> >quotation dup first 0 = [ drop [ ] ] when ]]
49
50 sign_     = [+ ]                 => [[ '[ dup first CHAR: - = [ _ prefix ] unless ] ]]
51 sign      = (sign_)?             => [[ [ ] or ]]
52
53 width_    = "." ([0-9])*         => [[ second >digits '[ _ short head ] ]]
54 width     = (width_)?            => [[ [ ] or ]]
55
56 digits_   = "." ([0-9])*         => [[ second >digits ]]
57 digits    = (digits_)?           => [[ 6 or ]]
58
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-u     = "u"                  => [[ [ unparse ] ]]
65 fmt-d     = "d"                  => [[ [ >integer number>string ] ]]
66 fmt-o     = "o"                  => [[ [ >integer >oct ] ]]
67 fmt-b     = "b"                  => [[ [ >integer >bin ] ]]
68 fmt-e     = digits "e"           => [[ first '[ _ format-scientific ] ]]
69 fmt-E     = digits "E"           => [[ first '[ _ format-scientific >upper ] ]]
70 fmt-f     = digits "f"           => [[ first '[ _ format-decimal ] ]]
71 fmt-x     = "x"                  => [[ [ >hex ] ]]
72 fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
73 unknown   = (.)*                 => [[ unknown-printf-directive ]]
74
75 strings_  = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
76 strings   = pad width strings_   => [[ <reversed> compose-all ]]
77
78 numbers_  = fmt-d|fmt-o|fmt-b|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
79 numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
80
81 types     = strings|numbers
82
83 lists     = "[%" types ", %]"    => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
84
85 assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
86
87 formats   = "%" (types|fmt-%|lists|assocs|unknown) => [[ second ]]
88
89 plain-text = (!("%").)+          => [[ >string ]]
90
91 text      = (formats|plain-text)* => [[ ]]
92
93 ;EBNF
94
95 : printf-quot ( format-string -- format-quot n )
96     parse-printf [ [ callable? ] count ] keep [
97         dup string? [ 1quotation ] [ [ 1 - ] dip ] if
98         over [ ndip ] 2curry
99     ] map nip [ compose-all ] [ length ] bi ; inline
100
101 PRIVATE>
102
103 MACRO: printf ( format-string -- quot )
104     printf-quot '[
105         @ output-stream get [ stream-write ] curry _ napply
106     ] ;
107
108 MACRO: sprintf ( format-string -- quot )
109     printf-quot '[
110         @ _ "" nappend-as
111     ] ;
112
113 : vprintf ( seq format-string -- )
114     parse-printf output-stream get '[
115         dup string? [
116             [ unclip-slice ] dip call( x -- y )
117         ] unless _ stream-write
118     ] each drop ;
119
120 : vsprintf ( seq format-string -- result )
121     [ vprintf ] with-string-writer ; inline
122
123 <PRIVATE
124
125 : pad-00 ( n -- string )
126     number>string 2 CHAR: 0 pad-head ; inline
127
128 : pad-000 ( n -- string )
129     number>string 3 CHAR: 0 pad-head ; inline
130
131 : >time ( timestamp -- string )
132     [ hour>> ] [ minute>> ] [ second>> floor ] tri
133     [ pad-00 ] tri@ 3array ":" join ; inline
134
135 : >date ( timestamp -- string )
136     [ month>> ] [ day>> ] [ year>> ] tri
137     [ pad-00 ] tri@ 3array "/" join ; inline
138
139 : >datetime ( timestamp -- string )
140     [
141        {
142           [ day-of-week day-abbreviation3 ]
143           [ month>> month-abbreviation ]
144           [ day>> pad-00 ]
145           [ >time ]
146           [ year>> number>string ]
147        } cleave
148     ] output>array " " join ; inline
149
150 : week-of-year ( timestamp day -- n )
151     [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
152     [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
153
154 : week-of-year-sunday ( timestamp -- n ) 0 week-of-year ; inline
155
156 : week-of-year-monday ( timestamp -- n ) 1 week-of-year ; inline
157
158 EBNF: parse-strftime
159
160 fmt-%     = "%"                  => [[ "%" ]]
161 fmt-a     = "a"                  => [[ [ day-of-week day-abbreviation3 ] ]]
162 fmt-A     = "A"                  => [[ [ day-of-week day-name ] ]]
163 fmt-b     = "b"                  => [[ [ month>> month-abbreviation ] ]]
164 fmt-B     = "B"                  => [[ [ month>> month-name ] ]]
165 fmt-c     = "c"                  => [[ [ >datetime ] ]]
166 fmt-d     = "d"                  => [[ [ day>> pad-00 ] ]]
167 fmt-H     = "H"                  => [[ [ hour>> pad-00 ] ]]
168 fmt-I     = "I"                  => [[ [ hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
169 fmt-j     = "j"                  => [[ [ day-of-year pad-000 ] ]]
170 fmt-m     = "m"                  => [[ [ month>> pad-00 ] ]]
171 fmt-M     = "M"                  => [[ [ minute>> pad-00 ] ]]
172 fmt-p     = "p"                  => [[ [ hour>> 12 < "AM" "PM" ? ] ]]
173 fmt-S     = "S"                  => [[ [ second>> floor pad-00 ] ]]
174 fmt-U     = "U"                  => [[ [ week-of-year-sunday pad-00 ] ]]
175 fmt-w     = "w"                  => [[ [ day-of-week number>string ] ]]
176 fmt-W     = "W"                  => [[ [ week-of-year-monday pad-00 ] ]]
177 fmt-x     = "x"                  => [[ [ >date ] ]]
178 fmt-X     = "X"                  => [[ [ >time ] ]]
179 fmt-y     = "y"                  => [[ [ year>> 100 mod pad-00 ] ]]
180 fmt-Y     = "Y"                  => [[ [ year>> number>string ] ]]
181 fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]]
182 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
183
184 formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
185             fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
186             fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
187
188 formats   = "%" (formats_)       => [[ second ]]
189
190 plain-text = (!("%").)+          => [[ >string ]]
191
192 text      = (formats|plain-text)* => [[ ]]
193
194 ;EBNF
195
196 PRIVATE>
197
198 MACRO: strftime ( format-string -- quot )
199     parse-strftime [
200         dup string? [
201             '[ _ swap push-all ]
202         ] [
203             '[ over @ swap push-all ]
204         ] if
205     ] map '[
206         SBUF" " clone [ _ cleave drop ] keep "" like
207     ] ;