]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/formatting/formatting.factor
formatting: fix strftime %U and %W
[factor.git] / basis / formatting / formatting.factor
index f4e0a4a1eee4c1f2c2a9a319290ec1cd6b505c67..1f5663c959ef9fa920ce8b16b6fcf705875dff31 100644 (file)
@@ -1,11 +1,11 @@
 ! Copyright (C) 2008 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
-USING: accessors arrays assocs calendar calendar.english combinators
-combinators.smart fry generalizations io io.streams.string
-kernel macros math math.functions math.parser namespaces
+USING: accessors arrays assocs calendar calendar.english
+calendar.private combinators combinators.smart generalizations
+io io.streams.string kernel math math.functions
+math.functions.integer-logs math.parser multiline namespaces
 peg.ebnf present prettyprint quotations sequences
-sequences.generalizations strings unicode vectors
-math.functions.integer-logs splitting multiline ;
+sequences.generalizations splitting strings unicode ;
 FROM: math.parser.private => format-float ;
 IN: formatting
 
@@ -37,7 +37,7 @@ ERROR: unknown-format-directive value ;
         [ 10^ * round-to-even >integer number>string ]
         [ 1 + CHAR: 0 pad-head ]
         [ cut* ] tri [ "." glue ] unless-empty
-    ] curry keep neg? [ CHAR: - prefix ] when ;
+    ] keepd neg? [ CHAR: - prefix ] when ;
 
 : format-scientific-mantissa ( x log10x digits -- string rounded-up? )
     [ swap - 10^ * round-to-even >integer number>string ] keep
@@ -55,7 +55,7 @@ ERROR: unknown-format-directive value ;
         [ abs dup integer-log10 ] dip
         [ format-scientific-mantissa ]
         [ drop nip format-scientific-exponent ] 3bi append
-    ] curry keep neg? [ CHAR: - prefix ] when ;
+    ] keepd neg? [ CHAR: - prefix ] when ;
 
 : format-float-fast ( x digits string -- string )
     [ "" -1 ] 2dip "C" format-float ;
@@ -106,7 +106,7 @@ pad       = pad-align pad-char pad-width => [[ <reversed> >quotation dup first 0
 sign_     = [+ ]                 => [[ '[ dup first CHAR: - = [ _ prefix ] unless ] ]]
 sign      = (sign_)?             => [[ [ ] or ]]
 
-width_    = "." ([0-9])*         => [[ second >digits '[ _ short head ] ]]
+width_    = "." ([0-9])*         => [[ second >digits '[ _ index-or-length head ] ]]
 width     = (width_)?            => [[ [ ] or ]]
 
 digits_   = "." ([0-9])*         => [[ second >digits ]]
@@ -136,15 +136,15 @@ numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign
 
 types     = strings|numbers
 
-lists     = "[%" types ", %]"    => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
+lists     = "[%" types ", %]"    => [[ second '[ _ { } map-as ", " join "{ " " }" surround ] ]]
 
-assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
+assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ [ _ _ bi* ":" glue ] { } assoc>map ", " join "{ " " }" surround ] ]]
 
 formats   = "%" (types|fmt-%|lists|assocs|unknown) => [[ second ]]
 
-plain-text = (!("%").)+          => [[ >string ]]
+plain-text = [^%]+               => [[ >string ]]
 
-text      = (formats|plain-text)* => [[ ]]
+text      = (formats|plain-text)*
 
 ]=]
 
@@ -201,11 +201,12 @@ MACRO: sprintf ( format-string -- quot )
           [ >time ]
           [ year>> number>string ]
        } cleave
-    ] output>array " " join ; inline
+    ] output>array join-words ; inline
 
 : week-of-year ( timestamp day -- n )
-    [ dup clone 1 >>month 1 >>day day-of-week dup ] dip > [ 7 swap - ] when
-    [ day-of-year ] dip 2dup < [ 0 2nip ] [ - 7 / 1 + >fixnum ] if ;
+    [ dup clone first-day-of-year dup clone ]
+    [ day-this-week ] bi* swap '[ _ time- duration>days ] bi@
+    dup 0 < [ 7 + - ] [ drop ] if 7 + 7 /i ;
 
 : week-of-year-sunday ( timestamp -- n ) 0 week-of-year ; inline
 
@@ -243,9 +244,9 @@ formats_  = fmt-%|fmt-a|fmt-A|fmt-b|fmt-B|fmt-c|fmt-d|fmt-H|fmt-I|
 
 formats   = "%" (formats_)       => [[ second ]]
 
-plain-text = (!("%").)+          => [[ >string ]]
+plain-text = [^%]+               => [[ >string ]]
 
-text      = (formats|plain-text)* => [[ ]]
+text      = (formats|plain-text)*
 
 ]=]
 
@@ -254,10 +255,8 @@ PRIVATE>
 MACRO: strftime ( format-string -- quot )
     parse-strftime [
         dup string? [
-            '[ _ swap push-all ]
+            '[ _ append! ]
         ] [
-            '[ over @ swap push-all ]
+            '[ over @ append! ]
         ] if
-    ] map '[
-        SBUF" " clone [ _ cleave drop ] keep "" like
-    ] ;
+    ] map concat '[ SBUF" " clone @ nip "" like ] ;