]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting.factor
formatting: fix bug when using %e and the number rounds up in magnitude
[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 math.functions.integer-logs splitting ;
9 FROM: math.parser.private => format-float ;
10 IN: formatting
11
12 <PRIVATE
13
14 : compose-all ( seq -- quot )
15     [ ] [ compose ] reduce ; inline
16
17 : fix-sign ( string -- string )
18     dup first CHAR: 0 = [
19         dup [ [ CHAR: 0 = not ] [ digit? ] bi and ] find
20         [
21             1 - swap 2dup nth {
22                 { CHAR: - [ remove-nth "-" prepend ] }
23                 { CHAR: + [ remove-nth "+" prepend ] }
24                 [ drop nip ]
25             } case
26         ] [ drop ] if
27     ] when ;
28
29 : >digits ( string -- digits )
30     [ 0 ] [ string>number ] if-empty ;
31
32 : format-decimal-simple ( x digits -- string )
33     [
34         [ abs ] dip
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 ;
39
40 : format-scientific-mantissa ( x log10x digits -- string rounded-up? )
41     [ swap - 10^ * round-to-even >integer number>string ] keep
42     over length 1 - < [
43         [ but-last >string ] when ! 9.9 rounded to 1e+01
44         1 cut [ "." glue ] unless-empty
45     ] keep ;
46
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 ;
50
51 : format-scientific-simple ( x digits -- string )
52     [
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 ;
57
58 : format-float-fast ( x digits string -- string )
59     [ "" -1 ] 2dip "C" format-float ;
60
61 : format-fast-scientific? ( x digits -- x' digits ? )
62     over float? [ t ]
63     [ 2dup
64         [ abs integer-log10 abs 308 < ]
65         [ 15 < ] bi* and
66         [ [ [ >float ] dip ] when ] keep
67     ] if ;
68
69 : ?fix-nonsignificant-zero ( string digits -- string )
70     [ ".0" "" replace ] [ drop ] if-zero ;
71
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 ;
77
78 : format-fast-decimal? ( x digits -- x' digits ? )
79     over float? [ t ]
80     [
81         2dup
82         [ drop dup integer?  [ abs 53 2^ < ] [ drop f ] if ]
83         [ over ratio?
84             [ [ abs integer-log10 ] dip
85               [ drop abs 308 < ] [ + 15 <= ] 2bi and ]
86             [ 2drop f ] if
87         ] 2bi or
88         [ [ [ >float ] dip ] when ] keep
89     ] if ; inline
90
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 ;
96
97 ERROR: unknown-printf-directive ;
98
99 EBNF: parse-printf
100
101 zero      = "0"                  => [[ CHAR: 0 ]]
102 char      = "'" (.)              => [[ second ]]
103
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 ]]
108
109 sign_     = [+ ]                 => [[ '[ dup first CHAR: - = [ _ prefix ] unless ] ]]
110 sign      = (sign_)?             => [[ [ ] or ]]
111
112 width_    = "." ([0-9])*         => [[ second >digits '[ _ short head ] ]]
113 width     = (width_)?            => [[ [ ] or ]]
114
115 digits_   = "." ([0-9])*         => [[ second >digits ]]
116 digits    = (digits_)?           => [[ 6 or ]]
117
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"                  => [[ [ number>string ] ]]
125 fmt-o     = "o"                  => [[ [ >oct ] ]]
126 fmt-O     = "O"                  => [[ [ >oct >upper ] ]]
127 fmt-b     = "b"                  => [[ [ >bin ] ]]
128 fmt-B     = "B"                  => [[ [ >bin >upper ] ]]
129 fmt-e     = digits "e"           => [[ first '[ _ format-scientific ] ]]
130 fmt-E     = digits "E"           => [[ first '[ _ format-scientific >upper ] ]]
131 fmt-f     = digits "f"           => [[ first '[ _ format-decimal ] ]]
132 fmt-x     = "x"                  => [[ [ >hex ] ]]
133 fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
134 unknown   = (.)*                 => [[ unknown-printf-directive ]]
135
136 strings_  = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
137 strings   = pad width strings_   => [[ <reversed> compose-all ]]
138
139 numbers_  = fmt-d|fmt-o|fmt-O|fmt-b|fmt-B|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
140 numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
141
142 types     = strings|numbers
143
144 lists     = "[%" types ", %]"    => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
145
146 assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
147
148 formats   = "%" (types|fmt-%|lists|assocs|unknown) => [[ second ]]
149
150 plain-text = (!("%").)+          => [[ >string ]]
151
152 text      = (formats|plain-text)* => [[ ]]
153
154 ;EBNF
155
156 : printf-quot ( format-string -- format-quot n )
157     parse-printf [ [ callable? ] count ] keep [
158         dup string? [ 1quotation ] [ [ 1 - ] dip ] if
159         over [ ndip ] 2curry
160     ] map nip [ compose-all ] [ length ] bi ; inline
161
162 PRIVATE>
163
164 MACRO: printf ( format-string -- quot )
165     printf-quot '[
166         @ output-stream get [ stream-write ] curry _ napply
167     ] ;
168
169 MACRO: sprintf ( format-string -- quot )
170     printf-quot '[
171         @ _ "" nappend-as
172     ] ;
173
174 : vprintf ( seq format-string -- )
175     parse-printf output-stream get '[
176         dup string? [
177             [ unclip-slice ] dip call( x -- y )
178         ] unless _ stream-write
179     ] each drop ;
180
181 : vsprintf ( seq format-string -- result )
182     [ vprintf ] with-string-writer ; inline
183
184 <PRIVATE
185
186 : pad-00 ( n -- string )
187     number>string 2 CHAR: 0 pad-head ; inline
188
189 : pad-000 ( n -- string )
190     number>string 3 CHAR: 0 pad-head ; inline
191
192 : >time ( timestamp -- string )
193     [ hour>> ] [ minute>> ] [ second>> floor ] tri
194     [ pad-00 ] tri@ 3array ":" join ; inline
195
196 : >date ( timestamp -- string )
197     [ month>> ] [ day>> ] [ year>> ] tri
198     [ pad-00 ] tri@ 3array "/" join ; inline
199
200 : >datetime ( timestamp -- string )
201     [
202        {
203           [ day-of-week day-abbreviation3 ]
204           [ month>> month-abbreviation ]
205           [ day>> pad-00 ]
206           [ >time ]
207           [ year>> number>string ]
208        } cleave
209     ] output>array " " join ; inline
210
211 : week-of-year ( timestamp day -- n )
212     [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
213     [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
214
215 : week-of-year-sunday ( timestamp -- n ) 0 week-of-year ; inline
216
217 : week-of-year-monday ( timestamp -- n ) 1 week-of-year ; inline
218
219 EBNF: parse-strftime
220
221 fmt-%     = "%"                  => [[ "%" ]]
222 fmt-a     = "a"                  => [[ [ day-of-week day-abbreviation3 ] ]]
223 fmt-A     = "A"                  => [[ [ day-of-week day-name ] ]]
224 fmt-b     = "b"                  => [[ [ month>> month-abbreviation ] ]]
225 fmt-B     = "B"                  => [[ [ month>> month-name ] ]]
226 fmt-c     = "c"                  => [[ [ >datetime ] ]]
227 fmt-d     = "d"                  => [[ [ day>> pad-00 ] ]]
228 fmt-H     = "H"                  => [[ [ hour>> pad-00 ] ]]
229 fmt-I     = "I"                  => [[ [ hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
230 fmt-j     = "j"                  => [[ [ day-of-year pad-000 ] ]]
231 fmt-m     = "m"                  => [[ [ month>> pad-00 ] ]]
232 fmt-M     = "M"                  => [[ [ minute>> pad-00 ] ]]
233 fmt-p     = "p"                  => [[ [ hour>> 12 < "AM" "PM" ? ] ]]
234 fmt-S     = "S"                  => [[ [ second>> floor pad-00 ] ]]
235 fmt-U     = "U"                  => [[ [ week-of-year-sunday pad-00 ] ]]
236 fmt-w     = "w"                  => [[ [ day-of-week number>string ] ]]
237 fmt-W     = "W"                  => [[ [ week-of-year-monday pad-00 ] ]]
238 fmt-x     = "x"                  => [[ [ >date ] ]]
239 fmt-X     = "X"                  => [[ [ >time ] ]]
240 fmt-y     = "y"                  => [[ [ year>> 100 mod pad-00 ] ]]
241 fmt-Y     = "Y"                  => [[ [ year>> number>string ] ]]
242 fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]]
243 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
244
245 formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
246             fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
247             fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
248
249 formats   = "%" (formats_)       => [[ second ]]
250
251 plain-text = (!("%").)+          => [[ >string ]]
252
253 text      = (formats|plain-text)* => [[ ]]
254
255 ;EBNF
256
257 PRIVATE>
258
259 MACRO: strftime ( format-string -- quot )
260     parse-strftime [
261         dup string? [
262             '[ _ swap push-all ]
263         ] [
264             '[ over @ swap push-all ]
265         ] if
266     ] map '[
267         SBUF" " clone [ _ cleave drop ] keep "" like
268     ] ;