The spf module implements a simplified version of C's sprintf function. The words in this module expect a format string with specifiers (see below for the format). For every specifier (except %% and %n) a stack item is converted to the character representation and added to the destination. All other characters are simply copied to the destination. The float conversion uses the PRECISION value for precision.
Format: %[flags][width][length]specifier
Flags: 0 = Left-pads the number with zeros instead of spaces
- = Left justify the number
+ = A positive number is preceded with a '+'
space = A positive number is preceded with a space
Width: number = the minimum number of characters written
Double: l = the argument is interpreted as a double
Specifier: c = format a character [char]
d = format a signed number [n or d]
i = format a signed number [n or d]
o = format a signed octal [n or d]
s = format a string [c-addr u]
u = format a unsigned number [u or ud]
x = format a unsigned hexadecimal number [u or ud]
X = format a unsigned hexadecimal number, capital letters
p = format a unsigned hexadecimal number [u or ud]
e = format a float number in scientific notation [r]
E = format a float number in scientific notation [r]
n = store the length of the string in [addr]
q = format a quoted string (non sprintf) [c-addr u]
% = write a '%' []
include ffl/spf.fs
str-new value spf1 \ Create string as destination
.( Format numbers : )
[DEFINED] represent [IF]
18E+0 15 15 10 8 1 s" %d %u %o %x %X %E" spf1 spf-set \ Format the numbers
[ELSE]
15 15 10 8 1 s" %d %u %o %x %X" spf1 spf-set \ Format the numbers
[THEN]
spf1 str-get type cr \ Print the result
.( Format strings and characters : )
char ! s" Hello" s" %s %c" spf1 spf-set \ Format strings and characters
spf1 str-get type cr \ Print result
.( Format numbers with signs : )
-30 20 10 s" %+d % d %d" spf1 spf-set \ Format numbers with signs
spf1 str-get type cr \ Print result
.( Leading and trailing zeros and spaces: )
-40 -30 20 10 s" %5d %-+5d %05d %5d" spf1 spf-set \ Format numbers with leading and trailing spaces
spf1 str-get type cr
.( Format with parsing word : )
time&date spf1 spf" %04d-%02d-%2d %02d:%02d:%02d" \ Format date & time
spf1 str-get type cr \ Print result