]> gitweb.factorcode.org Git - factor.git/commitdiff
formatting: adding octal and binary format directives.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 5 Aug 2014 16:39:50 +0000 (09:39 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 5 Aug 2014 16:39:50 +0000 (09:39 -0700)
basis/formatting/formatting-docs.factor
basis/formatting/formatting-tests.factor
basis/formatting/formatting.factor

index 1030b2212a932cdc13e69cc58ac72d334854a971..a08fd439bd6fe848621378515e12af70acd66051 100755 (executable)
@@ -19,12 +19,14 @@ HELP: printf
         { { $snippet "%P.Du" }       "Unparsed format"            "object" }
         { { $snippet "%c" }          "Character format"           "char" }
         { { $snippet "%C" }          "Character format uppercase" "char" }
-        { { $snippet "%+Pd" }        "Integer format"             "fixnum" }
+        { { $snippet "%+Pd" }        "Integer format (base 10)"   "fixnum" }
+        { { $snippet "%+Po" }        "Octal format (base 8)"    "fixnum" }
+        { { $snippet "%+Pb" }        "Binary format (base 2)"    "fixnum" }
         { { $snippet "%+P.De" }      "Scientific notation"        "fixnum, float" }
         { { $snippet "%+P.DE" }      "Scientific notation"        "fixnum, float" }
         { { $snippet "%+P.Df" }      "Fixed format"               "fixnum, float" }
-        { { $snippet "%+Px" }        "Hexadecimal"                "hex" }
-        { { $snippet "%+PX" }        "Hexadecimal uppercase"      "hex" }
+        { { $snippet "%+Px" }        "Hexadecimal (base 16)"      "fixnum" }
+        { { $snippet "%+PX" }        "Hexadecimal (base 16) uppercase" "fixnum" }
         { { $snippet "%[%?, %]" }    "Sequence format"            "sequence" }
         { { $snippet "%[%?: %? %]" } "Assocs format"              "assocs" }
     }
@@ -62,6 +64,10 @@ HELP: printf
         "USING: formatting ;"
         "0xff \"%04X\" printf"
         "00FF" }
+    { $example
+        "USING: formatting ;"
+        "12 \"%b\" printf"
+        "1100" }
     { $example
         "USING: formatting ;"
         "1.23456789 \"%.3f\" printf"
index 413a524b671bbaedcb9c97311699d6332fd04823..a5a463ff04f67f60eb3d0506151aebaf186495a3 100755 (executable)
@@ -20,6 +20,8 @@ IN: formatting.tests
 [ "123.10" ] [ 123.1 "%01.2f" sprintf ] unit-test
 [ "1.2346" ] [ 1.23456789 "%.4f" sprintf ] unit-test
 [ "  1.23" ] [ 1.23456789 "%6.2f" sprintf ] unit-test
+[ "001100" ] [ 12 "%06b" sprintf ] unit-test
+[ "==14" ] [ 12 "%'=4o" sprintf ] unit-test
 
 { "foo: 1 bar: 2" } [ { 1 2 3 } "foo: %d bar: %s" vsprintf ] unit-test
 
index 99ac0f6de0b727bf8eeaa436cdd42c3f5ff80c10..00e4041924ff7a90f3a742364c66268b1df1a544 100644 (file)
@@ -63,6 +63,8 @@ fmt-s     = "s"                  => [[ [ present ] ]]
 fmt-S     = "S"                  => [[ [ present >upper ] ]]
 fmt-u     = "u"                  => [[ [ unparse ] ]]
 fmt-d     = "d"                  => [[ [ >integer number>string ] ]]
+fmt-o     = "o"                  => [[ [ >integer >oct ] ]]
+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 ] ]]
@@ -73,7 +75,7 @@ unknown   = (.)*                 => [[ unknown-printf-directive ]]
 strings_  = fmt-c|fmt-C|fmt-s|fmt-S|fmt-u
 strings   = pad width strings_   => [[ <reversed> compose-all ]]
 
-numbers_  = fmt-d|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
+numbers_  = fmt-d|fmt-o|fmt-b|fmt-e|fmt-E|fmt-f|fmt-x|fmt-X
 numbers   = sign pad numbers_    => [[ unclip-last prefix compose-all [ fix-sign ] append ]]
 
 types     = strings|numbers