formatting: fix docs
parent
3d97bd2ae0
commit
050d042449
|
@ -36,7 +36,7 @@ HELP: printf
|
|||
"For example:\n"
|
||||
{ $list
|
||||
"\"%5s\" formats a string padding with spaces up to 5 characters wide."
|
||||
"\"%08d\" formats an integer padding with zeros up to 3 characters wide."
|
||||
"\"%03d\" formats an integer padding with zeros up to 3 characters wide."
|
||||
"\"%'#5f\" formats a float padding with '#' up to 3 characters wide."
|
||||
"\"%-10d\" formats an integer to 10 characters wide and left-aligns."
|
||||
}
|
||||
|
|
|
@ -12,18 +12,18 @@ IN: formatting
|
|||
[ ] [ compose ] reduce ;
|
||||
|
||||
: fix-sign ( string -- string )
|
||||
dup CHAR: 0 swap index 0 =
|
||||
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 swap drop ]
|
||||
} case
|
||||
] [ drop ] if
|
||||
] when ;
|
||||
|
||||
: >digits ( string -- digits )
|
||||
: >digits ( string -- digits )
|
||||
[ 0 ] [ string>number ] if-empty ;
|
||||
|
||||
: pad-digits ( string digits -- string' )
|
||||
|
@ -33,20 +33,20 @@ IN: formatting
|
|||
10^ [ * 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
|
||||
] while
|
||||
] keep 0 < [ neg ] when ;
|
||||
|
||||
: exp>string ( exp base digits -- string )
|
||||
[ max-digits ] keep -rot
|
||||
[
|
||||
[ 0 < "-" "+" ? ]
|
||||
[ abs number>string 2 CHAR: 0 pad-head ] bi
|
||||
[ abs number>string 2 CHAR: 0 pad-head ] bi
|
||||
"e" -rot 3append
|
||||
]
|
||||
[ number>string ] bi*
|
||||
|
@ -58,19 +58,19 @@ zero = "0" => [[ CHAR: 0 ]]
|
|||
char = "'" (.) => [[ second ]]
|
||||
|
||||
pad-char = (zero|char)? => [[ CHAR: \s or ]]
|
||||
pad-align = ("-")? => [[ \ pad-tail \ pad-head ? ]]
|
||||
pad-align = ("-")? => [[ \ pad-tail \ pad-head ? ]]
|
||||
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 ] [ ] ? ]]
|
||||
|
||||
width_ = "." ([0-9])* => [[ second >digits '[ _ short head ] ]]
|
||||
width = (width_)? => [[ [ ] or ]]
|
||||
width = (width_)? => [[ [ ] or ]]
|
||||
|
||||
digits_ = "." ([0-9])* => [[ second >digits ]]
|
||||
digits = (digits_)? => [[ 6 or ]]
|
||||
|
||||
fmt-% = "%" => [[ [ "%" ] ]]
|
||||
fmt-% = "%" => [[ [ "%" ] ]]
|
||||
fmt-c = "c" => [[ [ 1string ] ]]
|
||||
fmt-C = "C" => [[ [ 1string >upper ] ]]
|
||||
fmt-s = "s" => [[ [ dup number? [ number>string ] when ] ]]
|
||||
|
@ -78,7 +78,7 @@ fmt-S = "S" => [[ [ dup number? [ number>string ] when >upp
|
|||
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-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 ]]
|
||||
|
@ -89,9 +89,9 @@ 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 ]]
|
||||
|
||||
types = strings|numbers
|
||||
types = strings|numbers
|
||||
|
||||
lists = "[%" types ", %]" => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
|
||||
lists = "[%" types ", %]" => [[ second '[ _ map ", " join "{ " prepend " }" append ] ]]
|
||||
|
||||
assocs = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ unzip [ _ map ] dip _ map zip [ ":" join ] map ", " join "{ " prepend " }" append ] ]]
|
||||
|
||||
|
|
Loading…
Reference in New Issue