From ccbb8116fcd76f2b1c3e5acc5bfd177baa153dd4 Mon Sep 17 00:00:00 2001 From: Jon Harper Date: Sun, 26 Feb 2017 15:50:38 +0100 Subject: [PATCH] formatting, make %d, %o, %b work for ratios and floats --- basis/formatting/formatting-docs.factor | 16 ++++++++-------- basis/formatting/formatting-tests.factor | 11 +++++++++++ basis/formatting/formatting.factor | 6 +++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/basis/formatting/formatting-docs.factor b/basis/formatting/formatting-docs.factor index a56c493cfd..01772a81b0 100755 --- a/basis/formatting/formatting-docs.factor +++ b/basis/formatting/formatting-docs.factor @@ -19,14 +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 (base 10)" "integer" } - { { $snippet "%+Po" } "Octal format (base 8)" "integer" } - { { $snippet "%+Pb" } "Binary format (base 2)" "integer" } - { { $snippet "%+P.De" } "Scientific notation" "real" } - { { $snippet "%+P.DE" } "Scientific notation" "real" } - { { $snippet "%+P.Df" } "Fixed format" "real" } - { { $snippet "%+Px" } "Hexadecimal (base 16)" "integer" } - { { $snippet "%+PX" } "Hexadecimal (base 16) uppercase" "integer" } + { { $snippet "%+Pd" } "Base 10 General format" "real" } + { { $snippet "%+Px" } "Base 16 General format" "real" } + { { $snippet "%+PX" } "Base 16 General format uppercase" "real" } + { { $snippet "%+Po" } "Base 8 General format" "real" } + { { $snippet "%+Pb" } "Base 2 General format" "real" } + { { $snippet "%+P.De" } "Base 10 Scientific format" "real" } + { { $snippet "%+P.DE" } "Base 10 Scientific format uppercase" "real" } + { { $snippet "%+P.Df" } "Base 10 Fixed format" "real" } { { $snippet "%[%?, %]" } "Sequence format" "sequence" } { { $snippet "%[%?: %? %]" } "Assocs format" "assocs" } } diff --git a/basis/formatting/formatting-tests.factor b/basis/formatting/formatting-tests.factor index 02682b1854..d2cff20037 100755 --- a/basis/formatting/formatting-tests.factor +++ b/basis/formatting/formatting-tests.factor @@ -85,6 +85,17 @@ IN: formatting.tests { "-9007199254740992.0" } [ 53 2^ neg "%.1f" sprintf ] unit-test { "-9007199254740993.0" } [ 53 2^ 1 + neg "%.1f" sprintf ] unit-test +{ "1.5625" } [ 1.5625 "%d" sprintf ] unit-test +{ "1.9p0" } [ 1.5625 "%x" sprintf ] unit-test +{ "1.9P0" } [ 1.5625 "%X" sprintf ] unit-test +{ "1.44p0" } [ 1.5625 "%o" sprintf ] unit-test +{ "1.1001p0" } [ 1.5625 "%b" sprintf ] unit-test +{ "14+17/20" } [ 14+17/20 "%d" sprintf ] unit-test +{ "e+11/14" } [ 14+17/20 "%x" sprintf ] unit-test +{ "E+11/14" } [ 14+17/20 "%X" sprintf ] unit-test +{ "16+21/24" } [ 14+17/20 "%o" sprintf ] unit-test +{ "1110+10001/10100" } [ 14+17/20 "%b" sprintf ] unit-test + { "ff" } [ 0xff "%x" sprintf ] unit-test { "FF" } [ 0xff "%X" sprintf ] unit-test { "0f" } [ 0xf "%02x" sprintf ] unit-test diff --git a/basis/formatting/formatting.factor b/basis/formatting/formatting.factor index 1dc1232a9f..5269f0f148 100644 --- a/basis/formatting/formatting.factor +++ b/basis/formatting/formatting.factor @@ -113,9 +113,9 @@ fmt-C = "C" => [[ [ 1string >upper ] ]] 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-d = "d" => [[ [ number>string ] ]] +fmt-o = "o" => [[ [ >oct ] ]] +fmt-b = "b" => [[ [ >bin ] ]] fmt-e = digits "e" => [[ first '[ _ format-scientific ] ]] fmt-E = digits "E" => [[ first '[ _ format-scientific >upper ] ]] fmt-f = digits "f" => [[ first '[ _ format-decimal ] ]] -- 2.34.1