]> gitweb.factorcode.org Git - factor.git/commitdiff
Merge branch 'master' of git://factorcode.org/git/factor
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Dec 2008 14:50:42 +0000 (06:50 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Dec 2008 14:50:42 +0000 (06:50 -0800)
1  2 
basis/formatting/formatting-tests.factor
basis/formatting/formatting.factor

index 0000000000000000000000000000000000000000,8616325a817abf42ac013363dc726fd41f63e469..c7e9fb985ef2e4b25b780b096fa1f905ab2aa960
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,97 +1,97 @@@
 -
+ ! Copyright (C) 2008 John Benediktsson
+ ! See http://factorcode.org/license.txt for BSD license
+ USING: calendar kernel formatting tools.test ;
+ IN: formatting.tests
+ [ "%s" printf ] must-infer 
+ [ "%s" sprintf ] must-infer
+ [ t ] [ "" "" sprintf = ] unit-test
+ [ t ] [ "asdf" "asdf" sprintf = ] unit-test
+ [ t ] [ "10" 10 "%d" sprintf = ] unit-test
+ [ t ] [ "+10" 10 "%+d" sprintf = ] unit-test
+ [ t ] [ "-10" -10 "%d" sprintf = ] unit-test
+ [ t ] [ "  -10" -10 "%5d" sprintf = ] unit-test
+ [ t ] [ "-0010" -10 "%05d" sprintf = ] unit-test
+ [ t ] [ "+0010" 10 "%+05d" sprintf = ] unit-test
+ [ t ] [ "123.456000" 123.456 "%f" sprintf = ] unit-test
+ [ t ] [ "2.44" 2.436 "%.2f" sprintf = ] unit-test
+ [ t ] [ "123.10" 123.1 "%01.2f" sprintf = ] unit-test
+ [ t ] [ "1.2346" 1.23456789 "%.4f" sprintf = ] unit-test
+ [ t ] [ "  1.23" 1.23456789 "%6.2f" sprintf = ] unit-test
+ [ t ] [ "1.234000e+08" 123400000 "%e" sprintf = ] unit-test
+ [ t ] [ "-1.234000e+08" -123400000 "%e" sprintf = ] unit-test
+ [ t ] [ "1.234567e+08" 123456700 "%e" sprintf = ] unit-test
+ [ t ] [ "3.625e+08" 362525200 "%.3e" sprintf = ] unit-test
+ [ t ] [ "2.500000e-03" 0.0025 "%e" sprintf = ] unit-test
+ [ t ] [ "2.500000E-03" 0.0025 "%E" sprintf = ] unit-test
+ [ t ] [ "   1.0E+01" 10 "%10.1E" sprintf = ] unit-test
+ [ t ] [ "  -1.0E+01" -10 "%10.1E" sprintf = ] unit-test
+ [ t ] [ "  -1.0E+01" -10 "%+10.1E" sprintf = ] unit-test
+ [ t ] [ "  +1.0E+01" 10 "%+10.1E" sprintf = ] unit-test
+ [ t ] [ "-001.0E+01" -10 "%+010.1E" sprintf = ] unit-test
+ [ t ] [ "+001.0E+01" 10 "%+010.1E" sprintf = ] unit-test
+ [ t ] [ "ff" HEX: ff "%x" sprintf = ] unit-test
+ [ t ] [ "FF" HEX: ff "%X" sprintf = ] unit-test
+ [ t ] [ "0f" HEX: f "%02x" sprintf = ] unit-test
+ [ t ] [ "0F" HEX: f "%02X" sprintf = ] unit-test
+ [ t ] [ "2008-09-10" 
+         2008 9 10 "%04d-%02d-%02d" sprintf = ] unit-test
+ [ t ] [ "Hello, World!" 
+         "Hello, World!" "%s" sprintf = ] unit-test
+ [ t ] [ "printf test" 
+         "printf test" sprintf = ] unit-test
+ [ t ] [ "char a = 'a'"
+         CHAR: a "char %c = 'a'" sprintf = ] unit-test
+ [ t ] [ "00" HEX: 0 "%02x" sprintf = ] unit-test
+ [ t ] [ "ff" HEX: ff "%02x" sprintf = ] unit-test
+ [ t ] [ "0 message(s)"
+         0 "message" "%d %s(s)" sprintf = ] unit-test
+ [ t ] [ "0 message(s) with %"
+         0 "message" "%d %s(s) with %%" sprintf = ] unit-test
+ [ t ] [ "justif: \"left      \""
+         "left" "justif: \"%-10s\"" sprintf = ] unit-test
+ [ t ] [ "justif: \"     right\""
+         "right" "justif: \"%10s\"" sprintf = ] unit-test
+ [ t ] [ " 3: 0003 zero padded" 
+         3 " 3: %04d zero padded" sprintf = ] unit-test
+ [ t ] [ " 3: 3    left justif" 
+         3 " 3: %-4d left justif" sprintf = ] unit-test
+ [ t ] [ " 3:    3 right justif" 
+         3 " 3: %4d right justif" sprintf = ] unit-test
+ [ t ] [ " -3: -003 zero padded"
+         -3 " -3: %04d zero padded" sprintf = ] unit-test
+ [ t ] [ " -3: -3   left justif"
+         -3 " -3: %-4d left justif" sprintf = ] unit-test
+ [ t ] [ " -3:   -3 right justif"
+         -3 " -3: %4d right justif" sprintf = ] unit-test
+ [ t ] [ "There are 10 monkeys in the kitchen" 
+         10 "kitchen" "There are %d monkeys in the %s" sprintf = ] unit-test
+ [ f ] [ "%d" 10 "%d" sprintf = ] unit-test
+ [ t ] [ "[monkey]" "monkey" "[%s]" sprintf = ] unit-test
+ [ t ] [ "[    monkey]" "monkey" "[%10s]" sprintf = ] unit-test
+ [ t ] [ "[monkey    ]" "monkey" "[%-10s]" sprintf = ] unit-test
+ [ t ] [ "[0000monkey]" "monkey" "[%010s]" sprintf = ] unit-test
+ [ t ] [ "[####monkey]" "monkey" "[%'#10s]" sprintf = ] unit-test
+ [ t ] [ "[many monke]" "many monkeys" "[%10.10s]" sprintf = ] unit-test
+ [ "%H:%M:%S" strftime ] must-infer
+ : testtime ( -- timestamp )
+     2008 10 9 12 3 15 instant <timestamp> ;
+ [ t ] [ "12:03:15" testtime "%H:%M:%S" strftime = ] unit-test
+ [ t ] [ "12:03:15" testtime "%X" strftime = ] unit-test
 -
+ [ t ] [ "10/09/2008" testtime "%m/%d/%Y" strftime = ] unit-test
+ [ t ] [ "10/09/2008" testtime "%x" strftime = ] unit-test
 -
++[ t ] [ "10/09/08" testtime "%m/%d/%y" strftime = ] unit-test
+ [ t ] [ "Thu" testtime "%a" strftime = ] unit-test
+ [ t ] [ "Thursday" testtime "%A" strftime = ] unit-test
+ [ t ] [ "Oct" testtime "%b" strftime = ] unit-test
+ [ t ] [ "October" testtime "%B" strftime = ] unit-test
++[ t ] [ "Thu Oct 09 12:03:15 2008" testtime "%c" strftime = ] unit-test
++[ t ] [ "PM" testtime "%p" strftime = ] unit-test
index 0000000000000000000000000000000000000000,7dd845848828da31c927ad0fbd1951d7993e9f03..3f12c36bbd8b9b92046f976e9149e3ea5c20874f
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,186 +1,185 @@@
 -io io.encodings.ascii io.files io.streams.string
+ ! Copyright (C) 2008 John Benediktsson
+ ! See http://factorcode.org/license.txt for BSD license
+ USING: accessors arrays ascii calendar combinators fry kernel 
 -    10 swap ^ [ * round ] keep / ;
 -
 -: max-width ( string length -- string' ) 
 -    short head ;
++generalizations io io.encodings.ascii io.files io.streams.string
+ macros math math.functions math.parser peg.ebnf quotations
+ sequences splitting strings unicode.case vectors ;
+ IN: formatting
+ <PRIVATE
+ : compose-all ( seq -- quot )
+     [ ] [ compose ] reduce ;
+ : fix-sign ( string -- string )
+     dup CHAR: 0 swap index 0 = 
+       [ dup 0 swap [ [ CHAR: 0 = not ] keep digit? and ] find-from
+          [ dup 1- rot dup [ nth ] dip swap
+             {
+                { CHAR: - [ [ 1- ] dip remove-nth "-" prepend ] }
+                { CHAR: + [ [ 1- ] dip remove-nth "+" prepend ] }
+                [ drop swap drop ] 
+             } case 
+          ] [ drop ] if
+       ] when ;
+ : >digits ( string -- digits ) 
+     [ 0 ] [ string>number ] if-empty ;
+ : pad-digits ( string digits -- string' )
+     [ "." split1 ] dip [ CHAR: 0 pad-right ] [ head-slice ] bi "." glue ;
+ : max-digits ( n digits -- n' )
 -width_    = "." ([0-9])*         => [[ second >digits '[ _ max-width ] ]]
++    10 swap ^ [ * round ] keep / ; inline
+ : >exp ( x -- exp base )
+     [ 
+         abs 0 swap
+         [ dup [ 10.0 >= ] [ 1.0 < ] bi or ]
+         [ dup 10.0 >=
+           [ 10.0 / [ 1+ ] dip ]
+           [ 10.0 * [ 1- ] dip ] if
+         ] [ ] while 
+      ] keep 0 < [ neg ] when ;
+ : exp>string ( exp base digits -- string )
+     [ max-digits ] keep -rot
+     [
+         [ 0 < "-" "+" ? ]
+         [ abs number>string 2 CHAR: 0 pad-left ] bi 
+         "e" -rot 3append
+     ]
+     [ number>string ] bi*
+     rot pad-digits prepend ;
+ EBNF: parse-printf
+ zero      = "0"                  => [[ CHAR: 0 ]]
+ char      = "'" (.)              => [[ second ]]
+ pad-char  = (zero|char)?         => [[ CHAR: \s or ]]
+ pad-align = ("-")?               => [[ \ pad-right \ pad-left ? ]] 
+ pad-width = ([0-9])*             => [[ >digits ]]
+ pad       = pad-align pad-char pad-width => [[ reverse >quotation dup first 0 = [ drop [ ] ] when ]]
+ sign      = ("+")?               => [[ [ dup CHAR: - swap index [ "+" prepend ] unless ] [ ] ? ]]
 -: zero-pad ( str -- str' ) 2 CHAR: 0 pad-left ; inline
++width_    = "." ([0-9])*         => [[ second >digits '[ _ short head ] ]]
+ width     = (width_)?            => [[ [ ] or ]] 
+ digits_   = "." ([0-9])*         => [[ second >digits ]]
+ digits    = (digits_)?           => [[ 6 or ]]
+ fmt-%     = "%"                  => [[ [ "%" ] ]] 
+ fmt-c     = "c"                  => [[ [ 1string ] ]]
+ fmt-C     = "C"                  => [[ [ 1string >upper ] ]]
+ fmt-s     = "s"                  => [[ [ ] ]]
+ fmt-S     = "S"                  => [[ [ >upper ] ]]
+ fmt-d     = "d"                  => [[ [ >fixnum number>string ] ]]
+ fmt-e     = digits "e"           => [[ first '[ >exp _ exp>string ] ]]
+ fmt-E     = digits "E"           => [[ first '[ >exp _ exp>string >upper ] ]]
+ fmt-f     = digits "f"           => [[ first dup '[ >float _ max-digits number>string _ pad-digits ] ]] 
+ fmt-x     = "x"                  => [[ [ >hex ] ]]
+ fmt-X     = "X"                  => [[ [ >hex >upper ] ]]
+ unknown   = (.)*                 => [[ "Unknown directive" throw ]]
+ strings_  = fmt-c|fmt-C|fmt-s|fmt-S
+ strings   = pad width strings_   => [[ reverse compose-all ]]
+ 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 ]]
+ formats   = "%" (strings|numbers|fmt-%|unknown) => [[ second '[ _ dip ] ]]
+ plain-text = (!("%").)+          => [[ >string '[ _ swap ] ]]
+ text      = (formats|plain-text)* => [[ reverse [ [ [ push ] keep ] append ] map ]]
+ ;EBNF
+ PRIVATE>
+ MACRO: printf ( format-string -- )
+     parse-printf [ length ] keep compose-all '[ _ <vector> @ reverse [ write ] each ] ;
+ : sprintf ( format-string -- result )
+     [ printf ] with-string-writer ; inline
+ <PRIVATE
 -    [ number>string zero-pad ] map ":" join ; inline
++: pad-00 ( n -- string ) number>string 2 CHAR: 0 pad-left ; inline
++
++: pad-000 ( n -- string ) number>string 3 CHAR: 0 pad-left ; inline
+ : >time ( timestamp -- string )
+     [ hour>> ] [ minute>> ] [ second>> floor ] tri 3array
 -    [ number>string zero-pad ] map "/" join ; inline
++    [ pad-00 ] map ":" join ; inline
+ : >date ( timestamp -- string )
+     [ month>> ] [ day>> ] [ year>> ] tri 3array
 -      [ day>> number>string zero-pad ]
++    [ pad-00 ] map "/" join ; inline
+ : >datetime ( timestamp -- string )
+     { [ day-of-week day-abbreviation3 ]
+       [ month>> month-abbreviation ]
 -    } cleave 3array [ 2array ] dip append " " join ; inline
++      [ day>> pad-00 ]
+       [ >time ]
+       [ year>> number>string ]
 -fmt-d     = "d"                  => [[ [ dup day>> number>string zero-pad ] ]]
 -fmt-H     = "H"                  => [[ [ dup hour>> number>string zero-pad ] ]]
 -fmt-I     = "I"                  => [[ [ dup hour>> dup 12 > [ 12 - ] when number>string zero-pad ] ]]
 -fmt-j     = "j"                  => [[ [ dup day-of-year number>string ] ]]
 -fmt-m     = "m"                  => [[ [ dup month>> number>string zero-pad ] ]]
 -fmt-M     = "M"                  => [[ [ dup minute>> number>string zero-pad ] ]]
++    } cleave 5 narray " " join ; 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 ;
+ : 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 ] ]]
 -fmt-S     = "S"                  => [[ [ dup second>> round number>string zero-pad ] ]]
 -fmt-U     = "U"                  => [[ [ dup week-of-year-sunday ] ]]
++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 ] ]]
+ fmt-p     = "p"                  => [[ [ dup hour>> 12 < "AM" "PM" ? ] ]]
 -fmt-W     = "W"                  => [[ [ dup week-of-year-monday ] ]]
++fmt-S     = "S"                  => [[ [ dup second>> floor pad-00 ] ]]
++fmt-U     = "U"                  => [[ [ dup week-of-year-sunday pad-00 ] ]]
+ fmt-w     = "w"                  => [[ [ dup day-of-week number>string ] ]]
 -fmt-y     = "y"                  => [[ [ dup year>> 100 mod number>string ] ]]
++fmt-W     = "W"                  => [[ [ dup week-of-year-monday pad-00 ] ]]
+ fmt-x     = "x"                  => [[ [ dup >date ] ]]
+ fmt-X     = "X"                  => [[ [ dup >time ] ]]
++fmt-y     = "y"                  => [[ [ dup year>> 100 mod pad-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 ] ;