]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting.factor
Fix otug-talk
[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 calendar combinators fry kernel 
5 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 ;
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-right ] [ head-slice ] bi "." glue ;
33
34 : max-digits ( n digits -- n' )
35     10 swap ^ [ * round ] keep / ;
36
37 : max-width ( string length -- string' ) 
38     short head ;
39
40 : >exp ( x -- exp base )
41     [ 
42         abs 0 swap
43         [ dup [ 10.0 >= ] [ 1.0 < ] bi or ]
44         [ dup 10.0 >=
45           [ 10.0 / [ 1+ ] dip ]
46           [ 10.0 * [ 1- ] dip ] if
47         ] [ ] while 
48      ] keep 0 < [ neg ] when ;
49
50 : exp>string ( exp base digits -- string )
51     [ max-digits ] keep -rot
52     [
53         [ 0 < "-" "+" ? ]
54         [ abs number>string 2 CHAR: 0 pad-left ] bi 
55         "e" -rot 3append
56     ]
57     [ number>string ] bi*
58     rot pad-digits prepend ;
59
60 EBNF: parse-printf
61
62 zero      = "0"                  => [[ CHAR: 0 ]]
63 char      = "'" (.)              => [[ second ]]
64
65 pad-char  = (zero|char)?         => [[ CHAR: \s or ]]
66 pad-align = ("-")?               => [[ \ pad-right \ pad-left ? ]] 
67 pad-width = ([0-9])*             => [[ >digits ]]
68 pad       = pad-align pad-char pad-width => [[ reverse >quotation dup first 0 = [ drop [ ] ] when ]]
69
70 sign      = ("+")?               => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]]
71
72 width_    = "." ([0-9])*         => [[ second >digits '[ _ max-width ] ]]
73 width     = (width_)?            => [[ [ ] or ]] 
74
75 digits_   = "." ([0-9])*         => [[ second >digits ]]
76 digits    = (digits_)?           => [[ 6 or ]]
77
78 fmt-%     = "%"                  => [[ [ "%" ] ]] 
79 fmt-c     = "c"                  => [[ [ 1string ] ]]
80 fmt-C     = "C"                  => [[ [ 1string >upper ] ]]
81 fmt-s     = "s"                  => [[ [ ] ]]
82 fmt-S     = "S"                  => [[ [ >upper ] ]]
83 fmt-d     = "d"                  => [[ [ >fixnum number>string ] ]]
84 fmt-e     = digits "e"           => [[ first '[ >exp _ exp>string ] ]]
85 fmt-E     = digits "E"           => [[ first '[ >exp _ exp>string >upper ] ]]
86 fmt-f     = digits "f"           => [[ first dup '[ >float _ max-digits number>string _ pad-digits ] ]] 
87 fmt-x     = "x"                  => [[ [ >hex ] ]]
88 fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
89 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
90
91 strings_  = fmt-c|fmt-C|fmt-s|fmt-S
92 strings   = pad width strings_   => [[ reverse compose-all ]]
93
94 numbers_  = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
95 numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
96
97 formats   = "%" (strings|numbers|fmt-%|unknown) => [[ second '[ _ dip ] ]]
98
99 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
100
101 text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
102
103 ;EBNF
104
105 PRIVATE>
106
107 MACRO: printf ( format-string -- )
108     parse-printf [ length ] keep compose-all '[ _ <vector> @ reverse [ write ] each ] ;
109
110 : sprintf ( format-string -- result )
111     [ printf ] with-string-writer ; inline
112
113
114 <PRIVATE
115
116 : zero-pad ( str -- str' ) 2 CHAR: 0 pad-left ; inline
117
118 : >time ( timestamp -- string )
119     [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
120     [ number>string zero-pad ] map ":" join ; inline
121
122 : >date ( timestamp -- string )
123     [ month>> ] [ day>> ] [ year>> ] tri 3array
124     [ number>string zero-pad ] map "/" join ; inline
125
126 : >datetime ( timestamp -- string )
127     { [ day-of-week day-abbreviation3 ]
128       [ month>> month-abbreviation ]
129       [ day>> number>string zero-pad ]
130       [ >time ]
131       [ year>> number>string ]
132     } cleave 3array [ 2array ] dip append " " join ; inline
133
134 : (week-of-year) ( timestamp day -- n )
135     [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
136     [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1+ >fixnum ] if ;
137
138 : week-of-year-sunday ( timestamp -- n ) 0 (week-of-year) ; inline
139
140 : week-of-year-monday ( timestamp -- n ) 1 (week-of-year) ; inline
141
142 EBNF: parse-strftime
143
144 fmt-%     = "%"                  => [[ [ "%" ] ]]
145 fmt-a     = "a"                  => [[ [ dup day-of-week day-abbreviation3 ] ]]
146 fmt-A     = "A"                  => [[ [ dup day-of-week day-name ] ]]
147 fmt-b     = "b"                  => [[ [ dup month>> month-abbreviation ] ]]
148 fmt-B     = "B"                  => [[ [ dup month>> month-name ] ]]
149 fmt-c     = "c"                  => [[ [ dup >datetime ] ]]
150 fmt-d     = "d"                  => [[ [ dup day>> number>string zero-pad ] ]]
151 fmt-H     = "H"                  => [[ [ dup hour>> number>string zero-pad ] ]]
152 fmt-I     = "I"                  => [[ [ dup hour>> dup 12 > [ 12 - ] when number>string zero-pad ] ]]
153 fmt-j     = "j"                  => [[ [ dup day-of-year number>string ] ]]
154 fmt-m     = "m"                  => [[ [ dup month>> number>string zero-pad ] ]]
155 fmt-M     = "M"                  => [[ [ dup minute>> number>string zero-pad ] ]]
156 fmt-p     = "p"                  => [[ [ dup hour>> 12 < "AM" "PM" ? ] ]]
157 fmt-S     = "S"                  => [[ [ dup second>> round number>string zero-pad ] ]]
158 fmt-U     = "U"                  => [[ [ dup week-of-year-sunday ] ]]
159 fmt-w     = "w"                  => [[ [ dup day-of-week number>string ] ]]
160 fmt-W     = "W"                  => [[ [ dup week-of-year-monday ] ]]
161 fmt-x     = "x"                  => [[ [ dup >date ] ]]
162 fmt-X     = "X"                  => [[ [ dup >time ] ]]
163 fmt-y     = "y"                  => [[ [ dup year>> 100 mod number>string ] ]]
164 fmt-Y     = "Y"                  => [[ [ dup year>> number>string ] ]]
165 fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]]
166 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
167
168 formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
169             fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
170             fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
171
172 formats   = "%" (formats_)       => [[ second '[ _ dip ] ]]
173
174 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
175
176 text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
177
178 ;EBNF
179
180 PRIVATE>
181
182 MACRO: strftime ( format-string -- )
183     parse-strftime [ length ] keep [ ] join
184     '[ _ <vector> @ reverse concat nip ] ;
185
186