]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting.factor
formatting: support space prefix for numbers.
[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 strings
7 unicode.case unicode.categories 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 ] [ number>string ] bi* "%." ] dip
33     surround 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 PRIVATE>
97
98 MACRO: printf ( format-string -- )
99     parse-printf [ [ callable? ] count ] keep [
100         dup string? [ 1quotation ] [ [ 1 - ] dip ] if
101         over [ ndip ] 2curry
102     ] map nip [ compose-all ] [ length ] bi '[
103         @ output-stream get [ stream-write ] curry _ napply
104     ] ;
105
106 : sprintf ( format-string -- result )
107     [ printf ] with-string-writer ; inline
108
109 : vprintf ( seq format-string -- )
110     parse-printf output-stream get '[
111         dup string? [
112             [ unclip-slice ] dip call( x -- y )
113         ] unless _ stream-write
114     ] each drop ;
115
116 : vsprintf ( seq format-string -- result )
117     [ vprintf ] with-string-writer ; inline
118
119 <PRIVATE
120
121 : pad-00 ( n -- string ) number>string 2 CHAR: 0 pad-head ; inline
122
123 : pad-000 ( n -- string ) number>string 3 CHAR: 0 pad-head ; inline
124
125 : >time ( timestamp -- string )
126     [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
127     [ pad-00 ] map ":" join ; inline
128
129 : >date ( timestamp -- string )
130     [ month>> ] [ day>> ] [ year>> ] tri 3array
131     [ pad-00 ] map "/" join ; inline
132
133 : >datetime ( timestamp -- string )
134     [
135        {
136           [ day-of-week day-abbreviation3 ]
137           [ month>> month-abbreviation ]
138           [ day>> pad-00 ]
139           [ >time ]
140           [ year>> number>string ]
141        } cleave
142     ] output>array " " join ; inline
143
144 : (week-of-year) ( timestamp day -- n )
145     [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
146     [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
147
148 : week-of-year-sunday ( timestamp -- n ) 0 (week-of-year) ; inline
149
150 : week-of-year-monday ( timestamp -- n ) 1 (week-of-year) ; inline
151
152 EBNF: parse-strftime
153
154 fmt-%     = "%"                  => [[ "%" ]]
155 fmt-a     = "a"                  => [[ [ day-of-week day-abbreviation3 ] ]]
156 fmt-A     = "A"                  => [[ [ day-of-week day-name ] ]]
157 fmt-b     = "b"                  => [[ [ month>> month-abbreviation ] ]]
158 fmt-B     = "B"                  => [[ [ month>> month-name ] ]]
159 fmt-c     = "c"                  => [[ [ >datetime ] ]]
160 fmt-d     = "d"                  => [[ [ day>> pad-00 ] ]]
161 fmt-H     = "H"                  => [[ [ hour>> pad-00 ] ]]
162 fmt-I     = "I"                  => [[ [ hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
163 fmt-j     = "j"                  => [[ [ day-of-year pad-000 ] ]]
164 fmt-m     = "m"                  => [[ [ month>> pad-00 ] ]]
165 fmt-M     = "M"                  => [[ [ minute>> pad-00 ] ]]
166 fmt-p     = "p"                  => [[ [ hour>> 12 < "AM" "PM" ? ] ]]
167 fmt-S     = "S"                  => [[ [ second>> floor pad-00 ] ]]
168 fmt-U     = "U"                  => [[ [ week-of-year-sunday pad-00 ] ]]
169 fmt-w     = "w"                  => [[ [ day-of-week number>string ] ]]
170 fmt-W     = "W"                  => [[ [ week-of-year-monday pad-00 ] ]]
171 fmt-x     = "x"                  => [[ [ >date ] ]]
172 fmt-X     = "X"                  => [[ [ >time ] ]]
173 fmt-y     = "y"                  => [[ [ year>> 100 mod pad-00 ] ]]
174 fmt-Y     = "Y"                  => [[ [ year>> number>string ] ]]
175 fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]]
176 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
177
178 formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
179             fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
180             fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
181
182 formats   = "%" (formats_)       => [[ second ]]
183
184 plain-text = (!("%").)+          => [[ >string ]]
185
186 text      = (formats|plain-text)* => [[ ]]
187
188 ;EBNF
189
190 PRIVATE>
191
192 MACRO: strftime ( format-string -- )
193     parse-strftime [
194         dup string? [
195             '[ _ swap push-all ]
196         ] [
197             '[ over @ swap push-all ]
198         ] if
199     ] map '[
200         SBUF" " clone [ _ cleave drop ] keep "" like
201     ] ;