formatting: adding octal and binary format directives.

db4
John Benediktsson 2014-08-05 09:39:50 -07:00
parent 9954257d60
commit 0287355e2a
3 changed files with 14 additions and 4 deletions

View File

@ -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"

View File

@ -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

View 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