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