Adding documentation for printf, and improvements based on IRC feedback.
parent
c0e05ca416
commit
3e1303f784
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
USING: help.syntax help.markup kernel prettyprint sequences strings ;
|
||||||
|
|
||||||
|
IN: printf
|
||||||
|
|
||||||
|
HELP: printf
|
||||||
|
{ $values { "format-string" string } }
|
||||||
|
{ $description
|
||||||
|
"Writes the arguments formatted according to the format string."
|
||||||
|
{ $table
|
||||||
|
{ "%%" "Single %" "" }
|
||||||
|
{ "%Wd" "Integer W digits wide (e.g., \"1234\")" "fixnum" }
|
||||||
|
{ "%W.De" "Scientific notation" "fixnum, float" }
|
||||||
|
{ "%W.DE" "Scientific notation" "fixnum, float" }
|
||||||
|
{ "%W.Df" "Fixed format" "fixnum, float" }
|
||||||
|
{ "%Wx" "Hexadecimal" "hex" }
|
||||||
|
{ "%WX" "Hexadecimal uppercase" "hex" }
|
||||||
|
{ "%W.Ds" "String format" "string" }
|
||||||
|
{ "%W.DS" "String format uppercase" "string" }
|
||||||
|
{ "%c" "Character format" "char" }
|
||||||
|
{ "%C" "Character format uppercase" "char" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ $examples
|
||||||
|
{ $example
|
||||||
|
"USING: printf ;"
|
||||||
|
"{ 123 } \"%05d\" printf"
|
||||||
|
"00123" }
|
||||||
|
{ $example
|
||||||
|
"USING: printf ;"
|
||||||
|
"{ HEX: ff } \"04X\" printf"
|
||||||
|
"00FF" }
|
||||||
|
{ $example
|
||||||
|
"USING: printf ;"
|
||||||
|
"{ 1.23456789 } \"%.3f\" printf"
|
||||||
|
"1.234" }
|
||||||
|
{ $example
|
||||||
|
"USING: printf ;"
|
||||||
|
"{ 1234567890 } \"%.5e\" printf"
|
||||||
|
"1.23456e+09" }
|
||||||
|
} ;
|
||||||
|
|
||||||
|
HELP: sprintf
|
||||||
|
{ $values { "params" sequence } { "format-string" string } { "result" string } }
|
||||||
|
{ $description "Returns the arguments formatted according to the format string as a result string." }
|
||||||
|
{ $see-also printf } ;
|
||||||
|
|
|
@ -3,90 +3,98 @@
|
||||||
|
|
||||||
USING: kernel printf tools.test ;
|
USING: kernel printf tools.test ;
|
||||||
|
|
||||||
[ t ] [ "10" [ { 10 } "%d" sprintf ] call = ] unit-test
|
[ "%s" printf ] must-infer
|
||||||
|
|
||||||
[ t ] [ "123.456" [ { 123.456 } "%f" sprintf ] call = ] unit-test
|
[ t ] [ "10" { 10 } "%d" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "123.10" [ { 123.1 } "%01.2f" sprintf ] call = ] unit-test
|
[ t ] [ "123.456" { 123.456 } "%f" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "1.2345" [ { 1.23456789 } "%.4f" sprintf ] call = ] unit-test
|
[ t ] [ "123.10" { 123.1 } "%01.2f" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ " 1.23" [ { 1.23456789 } "%6.2f" sprintf ] call = ] unit-test
|
[ t ] [ "1.2345" { 1.23456789 } "%.4f" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "1.234e+08" [ { 123400000 } "%e" sprintf ] call = ] unit-test
|
[ t ] [ " 1.23" { 1.23456789 } "%6.2f" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "1.234567e+08" [ { 123456700 } "%e" sprintf ] call = ] unit-test
|
[ t ] [ "1.234e+08" { 123400000 } "%e" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "3.625e+08" [ { 362525200 } "%.3e" sprintf ] call = ] unit-test
|
[ t ] [ "1.234567e+08" { 123456700 } "%e" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "2.5e-03" [ { 0.0025 } "%e" sprintf ] call = ] unit-test
|
[ t ] [ "3.625e+08" { 362525200 } "%.3e" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "2.5E-03" [ { 0.0025 } "%E" sprintf ] call = ] unit-test
|
[ t ] [ "2.5e-03" { 0.0025 } "%e" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "ff" [ { HEX: ff } "%x" sprintf ] call = ] unit-test
|
[ t ] [ "2.5E-03" { 0.0025 } "%E" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "FF" [ { HEX: ff } "%X" sprintf ] call = ] unit-test
|
[ t ] [ "ff" { HEX: ff } "%x" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "0f" [ { HEX: f } "%02x" sprintf ] call = ] unit-test
|
[ t ] [ "FF" { HEX: ff } "%X" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "0F" [ { HEX: f } "%02X" sprintf ] call = ] unit-test
|
[ t ] [ "0f" { HEX: f } "%02x" sprintf = ] unit-test
|
||||||
|
|
||||||
|
[ t ] [ "0F" { HEX: f } "%02X" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "2008-09-10"
|
[ t ] [ "2008-09-10"
|
||||||
[ { 2008 9 10 } "%04d-%02d-%02d" sprintf ] call = ] unit-test
|
{ 2008 9 10 } "%04d-%02d-%02d" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "Hello, World!"
|
[ t ] [ "Hello, World!"
|
||||||
[ { "Hello, World!" } "%s" sprintf ] call = ] unit-test
|
{ "Hello, World!" } "%s" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "printf test"
|
[ t ] [ "printf test"
|
||||||
[ { } "printf test" sprintf ] call = ] unit-test
|
{ } "printf test" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "char a = 'a'"
|
[ t ] [ "char a = 'a'"
|
||||||
[ { CHAR: a } "char %c = 'a'" sprintf ] call = ] unit-test
|
{ CHAR: a } "char %c = 'a'" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "00" [ { HEX: 0 } "%02x" sprintf ] call = ] unit-test
|
[ t ] [ "00" { HEX: 0 } "%02x" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "ff" [ { HEX: ff } "%02x" sprintf ] call = ] unit-test
|
[ t ] [ "ff" { HEX: ff } "%02x" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "0 message(s)"
|
[ t ] [ "0 message(s)"
|
||||||
[ { 0 "message" } "%d %s(s)%" sprintf ] call = ] unit-test
|
{ 0 "message" } "%d %s(s)%" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "0 message(s) with %"
|
[ t ] [ "0 message(s) with %"
|
||||||
[ { 0 "message" } "%d %s(s) with %%" sprintf ] call = ] unit-test
|
{ 0 "message" } "%d %s(s) with %%" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "justif: \"left \""
|
[ t ] [ "justif: \"left \""
|
||||||
[ { "left" } "justif: \"%-10s\"" sprintf ] call = ] unit-test
|
{ "left" } "justif: \"%-10s\"" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "justif: \" right\""
|
[ t ] [ "justif: \" right\""
|
||||||
[ { "right" } "justif: \"%10s\"" sprintf ] call = ] unit-test
|
{ "right" } "justif: \"%10s\"" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ " 3: 0003 zero padded"
|
[ t ] [ " 3: 0003 zero padded"
|
||||||
[ { 3 } " 3: %04d zero padded" sprintf ] call = ] unit-test
|
{ 3 } " 3: %04d zero padded" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ " 3: 3 left justif"
|
[ t ] [ " 3: 3 left justif"
|
||||||
[ { 3 } " 3: %-4d left justif" sprintf ] call = ] unit-test
|
{ 3 } " 3: %-4d left justif" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ " 3: 3 right justif"
|
[ t ] [ " 3: 3 right justif"
|
||||||
[ { 3 } " 3: %4d right justif" sprintf ] call = ] unit-test
|
{ 3 } " 3: %4d right justif" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ " -3: -003 zero padded"
|
[ t ] [ " -3: -003 zero padded"
|
||||||
[ { -3 } " -3: %04d zero padded" sprintf ] call = ] unit-test
|
{ -3 } " -3: %04d zero padded" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ " -3: -3 left justif"
|
[ t ] [ " -3: -3 left justif"
|
||||||
[ { -3 } " -3: %-4d left justif" sprintf ] call = ] unit-test
|
{ -3 } " -3: %-4d left justif" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ " -3: -3 right justif"
|
[ t ] [ " -3: -3 right justif"
|
||||||
[ { -3 } " -3: %4d right justif" sprintf ] call = ] unit-test
|
{ -3 } " -3: %4d right justif" sprintf = ] unit-test
|
||||||
|
|
||||||
[ t ] [ "There are 10 monkeys in the kitchen"
|
[ t ] [ "There are 10 monkeys in the kitchen"
|
||||||
[ { 10 "kitchen" } "There are %d monkeys in the %s" sprintf ] call = ] unit-test
|
{ 10 "kitchen" } "There are %d monkeys in the %s" sprintf = ] unit-test
|
||||||
|
|
||||||
[ f ] [ "%d" [ { 10 } "%d" sprintf ] call = ] 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
|
||||||
|
|
||||||
[ t ] [ "[monkey]" [ { "monkey" } "[%s]" sprintf ] call = ] unit-test
|
|
||||||
[ t ] [ "[ monkey]" [ { "monkey" } "[%10s]" sprintf ] call = ] unit-test
|
|
||||||
[ t ] [ "[monkey ]" [ { "monkey" } "[%-10s]" sprintf ] call = ] unit-test
|
|
||||||
[ t ] [ "[0000monkey]" [ { "monkey" } "[%010s]" sprintf ] call = ] unit-test
|
|
||||||
[ t ] [ "[####monkey]" [ { "monkey" } "[%'#10s]" sprintf ] call = ] unit-test
|
|
||||||
[ t ] [ "[many monke]" [ { "many monkeys" } "[%10.10s]" sprintf ] call = ] unit-test
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
! Copyright (C) 2008 John Benediktsson
|
! Copyright (C) 2008 John Benediktsson
|
||||||
! See http://factorcode.org/license.txt for BSD license
|
! See http://factorcode.org/license.txt for BSD license
|
||||||
|
|
||||||
USING: io io.encodings.ascii io.files
|
USING: io io.encodings.ascii io.files io.streams.string
|
||||||
kernel sequences strings vectors math math.parser macros
|
kernel sequences splitting strings vectors math math.parser macros
|
||||||
fry peg.ebnf unicode.case arrays prettyprint quotations ;
|
fry peg.ebnf unicode.case arrays prettyprint quotations ;
|
||||||
|
|
||||||
IN: printf
|
IN: printf
|
||||||
|
@ -15,11 +15,8 @@ IN: printf
|
||||||
: write-all ( seq -- quot )
|
: write-all ( seq -- quot )
|
||||||
[ [ write ] append ] map ;
|
[ [ write ] append ] map ;
|
||||||
|
|
||||||
: append-all ( seq -- string )
|
|
||||||
SBUF" " [ dip swap append ] reduce ;
|
|
||||||
|
|
||||||
: apply-format ( params quot -- params string )
|
: apply-format ( params quot -- params string )
|
||||||
[ dup pop ] dip call ;
|
[ dup pop ] dip call ; inline
|
||||||
|
|
||||||
: fix-neg ( string -- string )
|
: fix-neg ( string -- string )
|
||||||
dup CHAR: 0 swap index 0 =
|
dup CHAR: 0 swap index 0 =
|
||||||
|
@ -28,14 +25,13 @@ IN: printf
|
||||||
[ drop ] if ] when ;
|
[ drop ] if ] when ;
|
||||||
|
|
||||||
: >digits ( string -- digits )
|
: >digits ( string -- digits )
|
||||||
dup length 0 > [ >string string>number ] [ drop 0 ] if ;
|
[ 0 ] [ string>number ] if-empty ;
|
||||||
|
|
||||||
: zero-pad ( string digits -- string )
|
: max-digits ( string digits -- string )
|
||||||
swap dup
|
[ "." split1 ] dip [ CHAR: 0 pad-right ] [ head-slice ] bi "." swap 3append ;
|
||||||
CHAR: . swap index rot + 1+
|
|
||||||
dup rot swap
|
: max-width ( string length -- string )
|
||||||
CHAR: 0 pad-right
|
[ dup length ] dip [ > ] keep swap [ head-slice >string ] [ drop ] if ;
|
||||||
swap head-slice ;
|
|
||||||
|
|
||||||
: >exponential ( n -- base exp )
|
: >exponential ( n -- base exp )
|
||||||
0
|
0
|
||||||
|
@ -49,8 +45,6 @@ IN: printf
|
||||||
[ 0 < [ "-" ] [ "+" ] if ] dip append
|
[ 0 < [ "-" ] [ "+" ] if ] dip append
|
||||||
"e" prepend ;
|
"e" prepend ;
|
||||||
|
|
||||||
PRIVATE>
|
|
||||||
|
|
||||||
EBNF: parse-format-string
|
EBNF: parse-format-string
|
||||||
|
|
||||||
plain-text = (!("%").)+ => [[ >string 1quotation ]]
|
plain-text = (!("%").)+ => [[ >string 1quotation ]]
|
||||||
|
@ -62,31 +56,31 @@ pad-char = "'" (.) => [[ second ]]
|
||||||
pad-char_ = (pad-zero|pad-char)? => [[ CHAR: \s or 1quotation ]]
|
pad-char_ = (pad-zero|pad-char)? => [[ CHAR: \s or 1quotation ]]
|
||||||
pad-align = ("-")? => [[ [ [ pad-right ] ] [ [ pad-left ] ] if ]]
|
pad-align = ("-")? => [[ [ [ pad-right ] ] [ [ pad-left ] ] if ]]
|
||||||
pad-width = ([0-9])* => [[ >digits 1quotation ]]
|
pad-width = ([0-9])* => [[ >digits 1quotation ]]
|
||||||
pad = (pad-align) (pad-char_) (pad-width) => [[ reverse compose-all ]]
|
pad = pad-align pad-char_ pad-width => [[ reverse compose-all ]]
|
||||||
|
|
||||||
width = "." ([0-9])* => [[ second >digits '[ _ head-slice ] ]]
|
width = "." ([0-9])* => [[ second >digits '[ _ max-width ] ]]
|
||||||
width_ = (width)? => [[ [ ] or ]]
|
width_ = (width)? => [[ [ ] or ]]
|
||||||
|
|
||||||
digits = "." ([0-9])* => [[ second >digits '[ _ zero-pad ] ]]
|
digits = "." ([0-9])* => [[ second >digits '[ _ max-digits ] ]]
|
||||||
digits_ = (digits)? => [[ [ ] or ]]
|
digits_ = (digits)? => [[ [ ] or ]]
|
||||||
|
|
||||||
fmt-c = "c" => [[ [ 1string ] ]]
|
fmt-c = "c" => [[ [ 1string ] ]]
|
||||||
fmt-C = "C" => [[ [ 1string >upper ] ]]
|
fmt-C = "C" => [[ [ 1string >upper ] ]]
|
||||||
chars = (fmt-c|fmt-C) => [[ '[ _ apply-format ] ]]
|
chars = (fmt-c | fmt-C) => [[ '[ _ apply-format ] ]]
|
||||||
|
|
||||||
fmt-s = "s" => [[ [ ] ]]
|
fmt-s = "s" => [[ [ ] ]]
|
||||||
fmt-S = "S" => [[ [ >upper ] ]]
|
fmt-S = "S" => [[ [ >upper ] ]]
|
||||||
strings = (pad) (width_) (fmt-s|fmt-S) => [[ reverse compose-all '[ _ apply-format ] ]]
|
strings = pad width_ (fmt-s | fmt-S) => [[ reverse compose-all '[ _ apply-format ] ]]
|
||||||
|
|
||||||
fmt-d = "d" => [[ [ >fixnum number>string ] ]]
|
fmt-d = "d" => [[ [ >fixnum number>string ] ]]
|
||||||
decimals = fmt-d
|
decimals = fmt-d
|
||||||
|
|
||||||
fmt-e = "e" => [[ [ >exponential ] ]]
|
fmt-e = "e" => [[ [ >exponential ] ]]
|
||||||
fmt-E = "E" => [[ [ >exponential >upper ] ]]
|
fmt-E = "E" => [[ [ >exponential >upper ] ]]
|
||||||
exps = (digits_) (fmt-e|fmt-E) => [[ reverse [ swap ] join [ swap append ] append ]]
|
exps = digits_ (fmt-e | fmt-E) => [[ reverse [ swap ] join [ swap append ] append ]]
|
||||||
|
|
||||||
fmt-f = "f" => [[ [ >float number>string ] ]]
|
fmt-f = "f" => [[ [ >float number>string ] ]]
|
||||||
floats = (digits_) (fmt-f) => [[ reverse compose-all ]]
|
floats = digits_ fmt-f => [[ reverse compose-all ]]
|
||||||
|
|
||||||
fmt-x = "x" => [[ [ >hex ] ]]
|
fmt-x = "x" => [[ [ >hex ] ]]
|
||||||
fmt-X = "X" => [[ [ >hex >upper ] ]]
|
fmt-X = "X" => [[ [ >hex >upper ] ]]
|
||||||
|
@ -96,21 +90,18 @@ numbers = (pad) (decimals|floats|hex|exps) => [[ reverse compose-all [ fix-neg
|
||||||
|
|
||||||
formats = "%" (chars|strings|numbers|percents) => [[ second ]]
|
formats = "%" (chars|strings|numbers|percents) => [[ second ]]
|
||||||
|
|
||||||
text = (formats|plain-text)*
|
text = (formats|plain-text)* => [[ write-all compose-all ]]
|
||||||
|
|
||||||
;EBNF
|
;EBNF
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
MACRO: printf ( format-string -- )
|
MACRO: printf ( format-string -- )
|
||||||
parse-format-string
|
parse-format-string '[ reverse >vector @ drop ] ;
|
||||||
'[ reverse >vector _ write-all compose-all call drop ] ;
|
|
||||||
|
|
||||||
MACRO: sprintf ( format-string -- )
|
|
||||||
parse-format-string
|
|
||||||
'[ reverse >vector _ append-all >string swap drop ] ;
|
|
||||||
|
|
||||||
MACRO: fprintf ( format-string -- )
|
|
||||||
parse-format-string
|
|
||||||
'[ reverse >vector _ write-all compose-all rot ascii [ call ] with-file-appender drop ] ;
|
|
||||||
|
|
||||||
|
: sprintf ( params format-string -- result )
|
||||||
|
[ printf ] with-string-writer ;
|
||||||
|
|
||||||
|
: fprintf ( filename params format-string -- )
|
||||||
|
rot ascii [ printf ] with-file-appender ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue