| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | ! Copyright (C) 2008 John Benediktsson | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license | 
					
						
							| 
									
										
										
										
											2009-05-15 00:23:06 -04:00
										 |  |  | USING: accessors arrays assocs calendar combinators fry kernel | 
					
						
							|  |  |  | generalizations io io.streams.string macros math math.functions | 
					
						
							|  |  |  | math.parser peg.ebnf quotations sequences splitting strings | 
					
						
							| 
									
										
										
										
											2010-04-14 00:24:35 -04:00
										 |  |  | unicode.categories unicode.case vectors combinators.smart | 
					
						
							|  |  |  | present ;
 | 
					
						
							|  |  |  | FROM: math.parser.private => format-float ;
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | IN: formatting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : compose-all ( seq -- quot )
 | 
					
						
							| 
									
										
										
										
											2012-07-20 12:40:30 -04:00
										 |  |  |     [ ] [ compose ] reduce ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : fix-sign ( string -- string )
 | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  |     dup CHAR: 0 swap index 0 =
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  |       [ dup 0 swap [ [ CHAR: 0 = not ] keep digit? and ] find-from
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |          [ dup 1 - rot dup [ nth ] dip swap
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |                { CHAR: - [ [ 1 - ] dip remove-nth "-" prepend ] } | 
					
						
							|  |  |  |                { CHAR: + [ [ 1 - ] dip remove-nth "+" prepend ] } | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  |                [ drop swap drop ] | 
					
						
							|  |  |  |             } case
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  |          ] [ drop ] if
 | 
					
						
							|  |  |  |       ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  | : >digits ( string -- digits )
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  |     [ 0 ] [ string>number ] if-empty ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-14 00:24:35 -04:00
										 |  |  | : format-simple ( x digits string -- string )
 | 
					
						
							|  |  |  |     [ [ >float ] [ number>string ] bi* "%." ] dip
 | 
					
						
							|  |  |  |     surround format-float ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : format-scientific ( x digits -- string ) "e" format-simple ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : format-decimal ( x digits -- string ) "f" format-simple ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ERROR: unknown-printf-directive ;
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | EBNF: parse-printf | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | zero      = "0"                  => [[ CHAR: 0 ]] | 
					
						
							|  |  |  | char      = "'" (.)              => [[ second ]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pad-char  = (zero|char)?         => [[ CHAR: \s or ]] | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  | pad-align = ("-")?               => [[ \ pad-tail \ pad-head ? ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | pad-width = ([0-9])*             => [[ >digits ]] | 
					
						
							| 
									
										
										
										
											2012-07-20 12:40:30 -04:00
										 |  |  | pad       = pad-align pad-char pad-width => [[ <reversed> >quotation dup first 0 = [ drop [ ] ] when ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | sign      = ("+")?               => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  | width_    = "." ([0-9])*         => [[ second >digits '[ _ short head ] ]] | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  | width     = (width_)?            => [[ [ ] or ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | digits_   = "." ([0-9])*         => [[ second >digits ]] | 
					
						
							|  |  |  | digits    = (digits_)?           => [[ 6 or ]] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  | fmt-%     = "%"                  => [[ [ "%" ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | fmt-c     = "c"                  => [[ [ 1string ] ]] | 
					
						
							|  |  |  | fmt-C     = "C"                  => [[ [ 1string >upper ] ]] | 
					
						
							| 
									
										
										
										
											2010-04-14 00:24:35 -04:00
										 |  |  | fmt-s     = "s"                  => [[ [ present ] ]] | 
					
						
							|  |  |  | fmt-S     = "S"                  => [[ [ present >upper ] ]] | 
					
						
							|  |  |  | fmt-d     = "d"                  => [[ [ >integer number>string ] ]] | 
					
						
							|  |  |  | fmt-e     = digits "e"           => [[ first '[ _ format-scientific ] ]] | 
					
						
							|  |  |  | fmt-E     = digits "E"           => [[ first '[ _ format-scientific >upper ] ]] | 
					
						
							|  |  |  | fmt-f     = digits "f"           => [[ first '[ _ format-decimal ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | fmt-x     = "x"                  => [[ [ >hex ] ]] | 
					
						
							|  |  |  | fmt-X     = "X"                  => [[ [ >hex >upper ] ]] | 
					
						
							| 
									
										
										
										
											2010-04-14 00:24:35 -04:00
										 |  |  | unknown   = (.)*                 => [[ unknown-printf-directive ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | strings_  = fmt-c|fmt-C|fmt-s|fmt-S | 
					
						
							| 
									
										
										
										
											2012-07-20 12:40:30 -04:00
										 |  |  | strings   = pad width strings_   => [[ <reversed> compose-all ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | numbers_  = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X | 
					
						
							|  |  |  | numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  | types     = strings|numbers | 
					
						
							| 
									
										
										
										
											2009-01-30 17:17:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-23 03:19:06 -05:00
										 |  |  | lists     = "[%" types ", %]"    => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]] | 
					
						
							| 
									
										
										
										
											2009-01-30 17:17:12 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | formats   = "%" (types|fmt-%|lists|assocs|unknown) => [[ second '[ _ dip ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-20 12:40:30 -04:00
										 |  |  | text      = (formats|plain-text)* => [[ <reversed> [ [ [ push ] keep ] append ] map ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ;EBNF | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: printf ( format-string -- )
 | 
					
						
							| 
									
										
										
										
											2012-07-20 12:40:30 -04:00
										 |  |  |     parse-printf [ length ] keep compose-all | 
					
						
							|  |  |  |     '[ _ <vector> @ <reversed> [ write ] each ] ;
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sprintf ( format-string -- result )
 | 
					
						
							|  |  |  |     [ printf ] with-string-writer ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  | : pad-00 ( n -- string ) number>string 2 CHAR: 0 pad-head ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  | : pad-000 ( n -- string ) number>string 3 CHAR: 0 pad-head ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : >time ( timestamp -- string )
 | 
					
						
							|  |  |  |     [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  |     [ pad-00 ] map ":" join ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : >date ( timestamp -- string )
 | 
					
						
							|  |  |  |     [ month>> ] [ day>> ] [ year>> ] tri 3array
 | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  |     [ pad-00 ] map "/" join ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : >datetime ( timestamp -- string )
 | 
					
						
							| 
									
										
										
										
											2009-05-03 18:23:14 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |        { | 
					
						
							|  |  |  |           [ day-of-week day-abbreviation3 ] | 
					
						
							|  |  |  |           [ month>> month-abbreviation ] | 
					
						
							|  |  |  |           [ day>> pad-00 ] | 
					
						
							|  |  |  |           [ >time ] | 
					
						
							|  |  |  |           [ year>> number>string ] | 
					
						
							|  |  |  |        } cleave
 | 
					
						
							|  |  |  |     ] output>array " " join ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (week-of-year) ( timestamp day -- n )
 | 
					
						
							|  |  |  |     [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
 | 
					
						
							| 
									
										
										
										
											2009-08-14 15:27:23 -04:00
										 |  |  |     [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : week-of-year-sunday ( timestamp -- n ) 0 (week-of-year) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : week-of-year-monday ( timestamp -- n ) 1 (week-of-year) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EBNF: parse-strftime | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fmt-%     = "%"                  => [[ [ "%" ] ]] | 
					
						
							|  |  |  | fmt-a     = "a"                  => [[ [ dup day-of-week day-abbreviation3 ] ]] | 
					
						
							|  |  |  | fmt-A     = "A"                  => [[ [ dup day-of-week day-name ] ]] | 
					
						
							|  |  |  | fmt-b     = "b"                  => [[ [ dup month>> month-abbreviation ] ]] | 
					
						
							|  |  |  | fmt-B     = "B"                  => [[ [ dup month>> month-name ] ]] | 
					
						
							|  |  |  | fmt-c     = "c"                  => [[ [ dup >datetime ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  | fmt-d     = "d"                  => [[ [ dup day>> pad-00 ] ]] | 
					
						
							|  |  |  | fmt-H     = "H"                  => [[ [ dup hour>> pad-00 ] ]] | 
					
						
							|  |  |  | fmt-I     = "I"                  => [[ [ dup hour>> dup 12 > [ 12 - ] when pad-00 ] ]] | 
					
						
							|  |  |  | fmt-j     = "j"                  => [[ [ dup day-of-year pad-000 ] ]] | 
					
						
							|  |  |  | fmt-m     = "m"                  => [[ [ dup month>> pad-00 ] ]] | 
					
						
							|  |  |  | fmt-M     = "M"                  => [[ [ dup minute>> pad-00 ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | fmt-p     = "p"                  => [[ [ dup hour>> 12 < "AM" "PM" ? ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  | fmt-S     = "S"                  => [[ [ dup second>> floor pad-00 ] ]] | 
					
						
							|  |  |  | fmt-U     = "U"                  => [[ [ dup week-of-year-sunday pad-00 ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | fmt-w     = "w"                  => [[ [ dup day-of-week number>string ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  | fmt-W     = "W"                  => [[ [ dup week-of-year-monday pad-00 ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | fmt-x     = "x"                  => [[ [ dup >date ] ]] | 
					
						
							|  |  |  | fmt-X     = "X"                  => [[ [ dup >time ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-16 17:05:30 -05:00
										 |  |  | fmt-y     = "y"                  => [[ [ dup year>> 100 mod pad-00 ] ]] | 
					
						
							| 
									
										
										
										
											2008-12-15 17:43:01 -05:00
										 |  |  | fmt-Y     = "Y"                  => [[ [ dup year>> number>string ] ]] | 
					
						
							|  |  |  | fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]] | 
					
						
							|  |  |  | unknown   = (.)*                 => [[ "Unknown directive" throw ]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I| | 
					
						
							|  |  |  |             fmt-j|fmt-m|fmt-M|fmt-p|fmt-S|fmt-U|fmt-w|fmt-W|fmt-x| | 
					
						
							|  |  |  |             fmt-X|fmt-y|fmt-Y|fmt-Z|unknown | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | formats   = "%" (formats_)       => [[ second '[ _ dip ] ]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ;EBNF | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: strftime ( format-string -- )
 | 
					
						
							|  |  |  |     parse-strftime [ length ] keep [ ] join
 | 
					
						
							|  |  |  |     '[ _ <vector> @ reverse concat nip ] ;
 |