]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting.factor
Merge branch 'master' of git://factorcode.org/git/factor into mongo-factor-driver
[factor.git] / basis / formatting / formatting.factor
1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors arrays ascii assocs calendar combinators fry kernel 
5 generalizations io io.encodings.ascii io.files io.streams.string
6 macros math math.functions math.parser peg.ebnf quotations
7 sequences splitting strings unicode.case vectors combinators.smart ;
8
9 IN: formatting
10
11 <PRIVATE
12
13 : compose-all ( seq -- quot )
14     [ ] [ compose ] reduce ;
15
16 : fix-sign ( string -- string )
17     dup CHAR: 0 swap index 0 = 
18       [ dup 0 swap [ [ CHAR: 0 = not ] keep digit? and ] find-from
19          [ dup 1- rot dup [ nth ] dip swap
20             {
21                { CHAR: - [ [ 1- ] dip remove-nth "-" prepend ] }
22                { CHAR: + [ [ 1- ] dip remove-nth "+" prepend ] }
23                [ drop swap drop ] 
24             } case 
25          ] [ drop ] if
26       ] when ;
27
28 : >digits ( string -- digits ) 
29     [ 0 ] [ string>number ] if-empty ;
30
31 : pad-digits ( string digits -- string' )
32     [ "." split1 ] dip [ CHAR: 0 pad-tail ] [ head-slice ] bi "." glue ;
33
34 : max-digits ( n digits -- n' )
35     10 swap ^ [ * round ] keep / ; inline
36
37 : >exp ( x -- exp base )
38     [ 
39         abs 0 swap
40         [ dup [ 10.0 >= ] [ 1.0 < ] bi or ]
41         [ dup 10.0 >=
42           [ 10.0 / [ 1+ ] dip ]
43           [ 10.0 * [ 1- ] dip ] if
44         ] while 
45      ] keep 0 < [ neg ] when ;
46
47 : exp>string ( exp base digits -- string )
48     [ max-digits ] keep -rot
49     [
50         [ 0 < "-" "+" ? ]
51         [ abs number>string 2 CHAR: 0 pad-head ] bi 
52         "e" -rot 3append
53     ]
54     [ number>string ] bi*
55     rot pad-digits prepend ;
56
57 EBNF: parse-printf
58
59 zero      = "0"                  => [[ CHAR: 0 ]]
60 char      = "'" (.)              => [[ second ]]
61
62 pad-char  = (zero|char)?         => [[ CHAR: \s or ]]
63 pad-align = ("-")?               => [[ \ pad-tail \ pad-head ? ]] 
64 pad-width = ([0-9])*             => [[ >digits ]]
65 pad       = pad-align pad-char pad-width => [[ reverse >quotation dup first 0 = [ drop [ ] ] when ]]
66
67 sign      = ("+")?               => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]]
68
69 width_    = "." ([0-9])*         => [[ second >digits '[ _ short head ] ]]
70 width     = (width_)?            => [[ [ ] or ]] 
71
72 digits_   = "." ([0-9])*         => [[ second >digits ]]
73 digits    = (digits_)?           => [[ 6 or ]]
74
75 fmt-%     = "%"                  => [[ [ "%" ] ]] 
76 fmt-c     = "c"                  => [[ [ 1string ] ]]
77 fmt-C     = "C"                  => [[ [ 1string >upper ] ]]
78 fmt-s     = "s"                  => [[ [ dup number? [ number>string ] when ] ]]
79 fmt-S     = "S"                  => [[ [ dup number? [ number>string ] when >upper ] ]]
80 fmt-d     = "d"                  => [[ [ >fixnum number>string ] ]]
81 fmt-e     = digits "e"           => [[ first '[ >exp _ exp>string ] ]]
82 fmt-E     = digits "E"           => [[ first '[ >exp _ exp>string >upper ] ]]
83 fmt-f     = digits "f"           => [[ first dup '[ >float _ max-digits number>string _ pad-digits ] ]] 
84 fmt-x     = "x"                  => [[ [ >hex ] ]]
85 fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
86 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
87
88 strings_  = fmt-c|fmt-C|fmt-s|fmt-S
89 strings   = pad width strings_   => [[ reverse compose-all ]]
90
91 numbers_  = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
92 numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
93
94 types     = strings|numbers 
95
96 lists     = "[%" types ", %]"    => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]] 
97
98 assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
99
100 formats   = "%" (types|fmt-%|lists|assocs|unknown) => [[ second '[ _ dip ] ]]
101
102 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
103
104 text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
105
106 ;EBNF
107
108 PRIVATE>
109
110 MACRO: printf ( format-string -- )
111     parse-printf [ length ] keep compose-all '[ _ <vector> @ reverse [ write ] each ] ;
112
113 : sprintf ( format-string -- result )
114     [ printf ] with-string-writer ; inline
115
116 <PRIVATE
117
118 : pad-00 ( n -- string ) number>string 2 CHAR: 0 pad-head ; inline
119
120 : pad-000 ( n -- string ) number>string 3 CHAR: 0 pad-head ; inline
121
122 : >time ( timestamp -- string )
123     [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
124     [ pad-00 ] map ":" join ; inline
125
126 : >date ( timestamp -- string )
127     [ month>> ] [ day>> ] [ year>> ] tri 3array
128     [ pad-00 ] map "/" join ; inline
129
130 : >datetime ( timestamp -- string )
131     [
132        {
133           [ day-of-week day-abbreviation3 ]
134           [ month>> month-abbreviation ]
135           [ day>> pad-00 ]
136           [ >time ]
137           [ year>> number>string ]
138        } cleave
139     ] output>array " " join ; inline
140
141 : (week-of-year) ( timestamp day -- n )
142     [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
143     [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1+ >fixnum ] if ;
144
145 : week-of-year-sunday ( timestamp -- n ) 0 (week-of-year) ; inline
146
147 : week-of-year-monday ( timestamp -- n ) 1 (week-of-year) ; inline
148
149 EBNF: parse-strftime
150
151 fmt-%     = "%"                  => [[ [ "%" ] ]]
152 fmt-a     = "a"                  => [[ [ dup day-of-week day-abbreviation3 ] ]]
153 fmt-A     = "A"                  => [[ [ dup day-of-week day-name ] ]]
154 fmt-b     = "b"                  => [[ [ dup month>> month-abbreviation ] ]]
155 fmt-B     = "B"                  => [[ [ dup month>> month-name ] ]]
156 fmt-c     = "c"                  => [[ [ dup >datetime ] ]]
157 fmt-d     = "d"                  => [[ [ dup day>> pad-00 ] ]]
158 fmt-H     = "H"                  => [[ [ dup hour>> pad-00 ] ]]
159 fmt-I     = "I"                  => [[ [ dup hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
160 fmt-j     = "j"                  => [[ [ dup day-of-year pad-000 ] ]]
161 fmt-m     = "m"                  => [[ [ dup month>> pad-00 ] ]]
162 fmt-M     = "M"                  => [[ [ dup minute>> pad-00 ] ]]
163 fmt-p     = "p"                  => [[ [ dup hour>> 12 < "AM" "PM" ? ] ]]
164 fmt-S     = "S"                  => [[ [ dup second>> floor pad-00 ] ]]
165 fmt-U     = "U"                  => [[ [ dup week-of-year-sunday pad-00 ] ]]
166 fmt-w     = "w"                  => [[ [ dup day-of-week number>string ] ]]
167 fmt-W     = "W"                  => [[ [ dup week-of-year-monday pad-00 ] ]]
168 fmt-x     = "x"                  => [[ [ dup >date ] ]]
169 fmt-X     = "X"                  => [[ [ dup >time ] ]]
170 fmt-y     = "y"                  => [[ [ dup year>> 100 mod pad-00 ] ]]
171 fmt-Y     = "Y"                  => [[ [ dup year>> number>string ] ]]
172 fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]]
173 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
174
175 formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
176             fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
177             fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
178
179 formats   = "%" (formats_)       => [[ second '[ _ dip ] ]]
180
181 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
182
183 text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
184
185 ;EBNF
186
187 PRIVATE>
188
189 MACRO: strftime ( format-string -- )
190     parse-strftime [ length ] keep [ ] join
191     '[ _ <vector> @ reverse concat nip ] ;