Towards a one line spin printf
jazzed
Posts: 11,803
Lack of a C like printf feature in spin is very annoying. Here is one way to achieve a printf on one line.
Thanks to Peter Verkaik for sharing his format code;·the underlying source is a variant of his work.
Two required methods·are·snprintf and p. Method snprintf fills the buffer up to a specified length
with a string specified by fmt and arg. The fmt string follows the C printf convention. The arg
parameter is a call to the "P" method. Letter·P stands for parameter; using a name like "parm"
would cause a long line. The parameter passed to·P is the content to be substituted in the
format specifier (i.e. %d) in left to right order as appears in fmt. Always use a sum of p() calls
like p(v)+p(v)+p(v) to get a good return code from printf. If the number of format specifiers
does not match the number of p() calls, printf will return 0.
If there are better ways to do this or some good cosmetic suggestions, please let me know.
There may be more room for improvement, but looks·great to me now with Phil's suggestions.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (jazzed) : 7/8/2008 1:13:37 AM GMT
Thanks to Peter Verkaik for sharing his format code;·the underlying source is a variant of his work.
CON ' test clock settings _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 OBJ tv : "TV_Text" s : "snprintf" PUB main | name tv.start(20) name := string("life") ' replace with your favorite noun printf(string("Hello World\n"),0) printf(string("I love my %s.\n"),s.p(name,0)) [s]printf(string("P1 $%x, P2 $%x\nP3 $%x, P4 $%x"),s.p($1,s.p($2,s.p($3,s.p($C,0)))))[/s] printf(string("P1 $%x, P2 $%x\nP3 $%x, P4 $%x"), p($1)+p($2)+p($3)+p($C)) PUB printf(fmt, arg) | pb[noparse][[/noparse]256] result := s.snprintf(@pb,256,fmt,arg) tv.str(@pb) PUB p(v) return s.p(v)
Two required methods·are·snprintf and p. Method snprintf fills the buffer up to a specified length
with a string specified by fmt and arg. The fmt string follows the C printf convention. The arg
parameter is a call to the "P" method. Letter·P stands for parameter; using a name like "parm"
would cause a long line. The parameter passed to·P is the content to be substituted in the
format specifier (i.e. %d) in left to right order as appears in fmt. Always use a sum of p() calls
like p(v)+p(v)+p(v) to get a good return code from printf. If the number of format specifiers
does not match the number of p() calls, printf will return 0.
If there are better ways to do this or some good cosmetic suggestions, please let me know.
There may be more room for improvement, but looks·great to me now with Phil's suggestions.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (jazzed) : 7/8/2008 1:13:37 AM GMT
zip
20K
Comments
Doing it this way will require the "stack" to be treated as a queue to get the parameters off in the proper order.
Going one step further, you could define another, local p that called s.p to avoid all those extra periods:
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Still some PropSTICK Kit bare PCBs left!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔