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