]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting.factor
formatting: new format specifier for unparsed representation
[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 fry kernel
4 generalizations io io.streams.string macros math math.functions
5 math.parser peg.ebnf prettyprint quotations sequences splitting
6 strings unicode.categories unicode.case vectors combinators.smart
7 present ;
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 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 : 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 [ "+" prepend ] unless ] [ ] ? ]]
52
53 width_    = "." ([0-9])*         => [[ second >digits '[ _ short head ] ]]
54 width     = (width_)?            => [[ [ ] or ]]
55
56 digits_   = "." ([0-9])*         => [[ second >digits ]]
57 digits    = (digits_)?           => [[ 6 or ]]
58
59 fmt-%     = "%"                  => [[ [ "%" ] ]]
60 fmt-c     = "c"                  => [[ [ 1string ] ]]
61 fmt-C     = "C"                  => [[ [ 1string >upper ] ]]
62 fmt-s     = "s"                  => [[ [ present ] ]]
63 fmt-S     = "S"                  => [[ [ present >upper ] ]]
64 fmt-u     = "u"                  => [[ [ unparse ] ]]
65 fmt-d     = "d"                  => [[ [ >integer number>string ] ]]
66 fmt-e     = digits "e"           => [[ first '[ _ format-scientific ] ]]
67 fmt-E     = digits "E"           => [[ first '[ _ format-scientific >upper ] ]]
68 fmt-f     = digits "f"           => [[ first '[ _ format-decimal ] ]]
69 fmt-x     = "x"                  => [[ [ >hex ] ]]
70 fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
71 unknown   = (.)*                 => [[ unknown-printf-directive ]]
72
73 strings_  = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
74 strings   = pad width strings_   => [[ <reversed> compose-all ]]
75
76 numbers_  = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
77 numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
78
79 types     = strings|numbers
80
81 lists     = "[%" types ", %]"    => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
82
83 assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
84
85 formats   = "%" (types|fmt-%|lists|assocs|unknown) => [[ second '[ _ dip ] ]]
86
87 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
88
89 text      = (formats|plain-text)* => [[ <reversed> [ [ [ push ] keep ] append ] map ]]
90
91 ;EBNF
92
93 PRIVATE>
94
95 MACRO: printf ( format-string -- )
96     parse-printf [ length ] keep compose-all
97     '[ _ <vector> @ <reversed> [ write ] each ] ;
98
99 : sprintf ( format-string -- result )
100     [ printf ] with-string-writer ; inline
101
102 : vprintf ( seq format-string -- )
103     parse-printf reverse! [
104         first dup string?
105         [ '[ _ write ] ] [ '[ unclip-slice @ write ] ] if
106     ] map concat call( x -- x ) drop ;
107
108 : vsprintf ( seq format-string -- result )
109     [ vprintf ] with-string-writer ; inline
110
111 <PRIVATE
112
113 : pad-00 ( n -- string ) number>string 2 CHAR: 0 pad-head ; inline
114
115 : pad-000 ( n -- string ) number>string 3 CHAR: 0 pad-head ; inline
116
117 : >time ( timestamp -- string )
118     [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
119     [ pad-00 ] map ":" join ; inline
120
121 : >date ( timestamp -- string )
122     [ month>> ] [ day>> ] [ year>> ] tri 3array
123     [ pad-00 ] map "/" join ; inline
124
125 : >datetime ( timestamp -- string )
126     [
127        {
128           [ day-of-week day-abbreviation3 ]
129           [ month>> month-abbreviation ]
130           [ day>> pad-00 ]
131           [ >time ]
132           [ year>> number>string ]
133        } cleave
134     ] output>array " " join ; inline
135
136 : (week-of-year) ( timestamp day -- n )
137     [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
138     [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
139
140 : week-of-year-sunday ( timestamp -- n ) 0 (week-of-year) ; inline
141
142 : week-of-year-monday ( timestamp -- n ) 1 (week-of-year) ; inline
143
144 EBNF: parse-strftime
145
146 fmt-%     = "%"                  => [[ [ "%" ] ]]
147 fmt-a     = "a"                  => [[ [ dup day-of-week day-abbreviation3 ] ]]
148 fmt-A     = "A"                  => [[ [ dup day-of-week day-name ] ]]
149 fmt-b     = "b"                  => [[ [ dup month>> month-abbreviation ] ]]
150 fmt-B     = "B"                  => [[ [ dup month>> month-name ] ]]
151 fmt-c     = "c"                  => [[ [ dup >datetime ] ]]
152 fmt-d     = "d"                  => [[ [ dup day>> pad-00 ] ]]
153 fmt-H     = "H"                  => [[ [ dup hour>> pad-00 ] ]]
154 fmt-I     = "I"                  => [[ [ dup hour>> dup 12 > [ 12 - ] when pad-00 ] ]]
155 fmt-j     = "j"                  => [[ [ dup day-of-year pad-000 ] ]]
156 fmt-m     = "m"                  => [[ [ dup month>> pad-00 ] ]]
157 fmt-M     = "M"                  => [[ [ dup minute>> pad-00 ] ]]
158 fmt-p     = "p"                  => [[ [ dup hour>> 12 < "AM" "PM" ? ] ]]
159 fmt-S     = "S"                  => [[ [ dup second>> floor pad-00 ] ]]
160 fmt-U     = "U"                  => [[ [ dup week-of-year-sunday pad-00 ] ]]
161 fmt-w     = "w"                  => [[ [ dup day-of-week number>string ] ]]
162 fmt-W     = "W"                  => [[ [ dup week-of-year-monday pad-00 ] ]]
163 fmt-x     = "x"                  => [[ [ dup >date ] ]]
164 fmt-X     = "X"                  => [[ [ dup >time ] ]]
165 fmt-y     = "y"                  => [[ [ dup year>> 100 mod pad-00 ] ]]
166 fmt-Y     = "Y"                  => [[ [ dup year>> number>string ] ]]
167 fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]]
168 unknown   = (.)*                 => [[ "Unknown directive" throw ]]
169
170 formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
171             fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x|
172             fmt-X|fmt-y|fmt-Y|fmt-Z|unknown
173
174 formats   = "%" (formats_)       => [[ second '[ _ dip ] ]]
175
176 plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
177
178 text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
179
180 ;EBNF
181
182 PRIVATE>
183
184 MACRO: strftime ( format-string -- )
185     parse-strftime [ length ] keep [ ] join
186     '[ _ <vector> @ reverse concat nip ] ;