]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/formatting/formatting.factor
formatting: fix strftime %U and %W
[factor.git] / basis / formatting / formatting.factor
index bdb81c1338b1ac8d6aadcd969721feb285613674..1f5663c959ef9fa920ce8b16b6fcf705875dff31 100644 (file)
@@ -1,14 +1,16 @@
 ! Copyright (C) 2008 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
-USING: accessors arrays assocs calendar 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.case
-unicode.categories vectors ;
+sequences.generalizations splitting strings unicode ;
 FROM: math.parser.private => format-float ;
 IN: formatting
 
+ERROR: unknown-format-directive value ;
+
 <PRIVATE
 
 : compose-all ( seq -- quot )
@@ -29,16 +31,69 @@ IN: formatting
 : >digits ( string -- digits )
     [ 0 ] [ string>number ] if-empty ;
 
-: format-simple ( x digits string -- string )
-    [ >float "" -1 ] 2dip "C" format-float ;
-
-: format-scientific ( x digits -- string ) "e" format-simple ;
-
-: format-decimal ( x digits -- string ) "f" format-simple ;
-
-ERROR: unknown-printf-directive ;
-
-EBNF: parse-printf
+: format-decimal-simple ( x digits -- string )
+    [
+        [ abs ] dip
+        [ 10^ * round-to-even >integer number>string ]
+        [ 1 + CHAR: 0 pad-head ]
+        [ cut* ] tri [ "." glue ] unless-empty
+    ] keepd neg? [ CHAR: - prefix ] when ;
+
+: format-scientific-mantissa ( x log10x digits -- string rounded-up? )
+    [ swap - 10^ * round-to-even >integer number>string ] keep
+    over length 1 - < [
+        [ but-last >string ] when ! 9.9 rounded to 1e+01
+        1 cut [ "." glue ] unless-empty
+    ] keep ;
+
+: format-scientific-exponent ( rounded-up? log10x -- string )
+    swap [ 1 + ] when number>string 2 CHAR: 0 pad-head
+    dup CHAR: - swap index "e" "e+" ? prepend ;
+
+: format-scientific-simple ( x digits -- string )
+    [
+        [ abs dup integer-log10 ] dip
+        [ format-scientific-mantissa ]
+        [ drop nip format-scientific-exponent ] 3bi append
+    ] keepd neg? [ CHAR: - prefix ] when ;
+
+: format-float-fast ( x digits string -- string )
+    [ "" -1 ] 2dip "C" format-float ;
+
+: format-fast-scientific? ( x digits -- x' digits ? )
+    over float? [ t ]
+    [ 2dup
+        [ [ t ] [ abs integer-log10 abs 308 < ] if-zero ]
+        [ 15 < ] bi* and
+        [ [ [ >float ] dip ] when ] keep
+    ] if ;
+
+: format-scientific ( x digits -- string )
+    format-fast-scientific?  [
+        [ "e" format-float-fast ]
+        [ [ ".0e" "e" replace ] [ drop ] if-zero ] bi
+    ] [ format-scientific-simple ] if ;
+
+: format-fast-decimal? ( x digits -- x' digits ? )
+    over float? [ t ]
+    [
+        2dup
+        [ drop dup integer?  [ abs 53 2^ < ] [ drop f ] if ]
+        [ over ratio?
+            [ [ abs integer-log10 ] dip
+              [ drop abs 308 < ] [ + 15 <= ] 2bi and ]
+            [ 2drop f ] if
+        ] 2bi or
+        [ [ [ >float ] dip ] when ] keep
+    ] if ; inline
+
+: format-decimal ( x digits -- string )
+    format-fast-decimal? [
+        [ "f" format-float-fast ]
+        [ [ ".0" ?tail drop ] [ drop ] if-zero ] bi
+    ] [ format-decimal-simple ] if ;
+
+EBNF: parse-printf [=[
 
 zero      = "0"                  => [[ CHAR: 0 ]]
 char      = "'" (.)              => [[ second ]]
@@ -48,10 +103,10 @@ pad-align = ("-")?               => [[ \ pad-tail \ pad-head ? ]]
 pad-width = ([0-9])*             => [[ >digits ]]
 pad       = pad-align pad-char pad-width => [[ <reversed> >quotation dup first 0 = [ drop [ ] ] when ]]
 
-sign_     = [+ ]                 => [[ '[ dup CHAR: - swap index [ _ prefix ] unless ] ]]
+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 ]]
@@ -69,9 +124,9 @@ fmt-b     = "b"                  => [[ [ >integer >bin ] ]]
 fmt-e     = digits "e"           => [[ first '[ _ format-scientific ] ]]
 fmt-E     = digits "E"           => [[ first '[ _ format-scientific >upper ] ]]
 fmt-f     = digits "f"           => [[ first '[ _ format-decimal ] ]]
-fmt-x     = "x"                  => [[ [ >hex ] ]]
-fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
-unknown   = (.)*                 => [[ unknown-printf-directive ]]
+fmt-x     = "x"                  => [[ [ >integer >hex ] ]]
+fmt-X     = "X"                  => [[ [ >integer >hex >upper ] ]]
+unknown   = (.)*                 => [[ "" like unknown-format-directive ]]
 
 strings_  = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
 strings   = pad width strings_   => [[ <reversed> compose-all ]]
@@ -81,17 +136,17 @@ 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)*
 
-;EBNF
+]=]
 
 : printf-quot ( format-string -- format-quot n )
     parse-printf [ [ callable? ] count ] keep [
@@ -146,17 +201,18 @@ 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
 
 : week-of-year-monday ( timestamp -- n ) 1 week-of-year ; inline
 
-EBNF: parse-strftime
+EBNF: parse-strftime [=[
 
 fmt-%     = "%"                  => [[ "%" ]]
 fmt-a     = "a"                  => [[ [ day-of-week day-abbreviation3 ] ]]
@@ -180,7 +236,7 @@ fmt-X     = "X"                  => [[ [ >time ] ]]
 fmt-y     = "y"                  => [[ [ year>> 100 mod pad-00 ] ]]
 fmt-Y     = "Y"                  => [[ [ year>> number>string ] ]]
 fmt-Z     = "Z"                  => [[ [ "Not yet implemented" throw ] ]]
-unknown   = (.)*                 => [[ "Unknown directive" throw ]]
+unknown   = (.)*                 => [[ "" like unknown-format-directive ]]
 
 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|
@@ -188,21 +244,19 @@ 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)*
 
-;EBNF
+]=]
 
 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 ] ;