On 14/3/23 11:05, luser droog wrote:
The variable arguments are interpolated in reverse order (ie. the
normal top-of-stack down-ward way). I'm not sure how useful it
actually is. Should it take an array for the variable arguments and
take them left-to-right?
I love this game! :-)
Here's a slightly different implementation that uses search instead of
testing each character.
/sprintf {
% construct array of strings
[ exch (%) {
search not { exit } if
counttomark 2 add -1 roll 256 string cvs 4 2 roll
} loop ]
% merge strings into a single array
0 1 index { length add } forall string exch
0 exch { 3 copy putinterval length add } forall
pop
} def
It's interesting that we mostly came up with identical code.
For fun, this one prints arrays, dictionaries and other stuff (instead
of --nostringval--):
/_sprintf {
dup type {
dup/arraytype eq 1 index/packedarraytype eq or {
pop ([)
exch { _sprintf ( ) } forall
dup ( ) eq {pop} if
(]) exit
} if
dup/dicttype eq {
pop (<<)
exch {
mark 3 -1 roll _sprintf ( => )
counttomark 2 add -2 roll pop
_sprintf (, )
} forall
dup (, ) eq {pop} if
(>>) exit
} if
dup/stringtype eq { pop exit } if
dup/nametype eq { pop (/) exch dup length string cvs exit } if
dup/operatortype eq 1 index/integertype eq or 1 index/realtype eq or
{ pop 20 string cvs exit }
if
dup/booleantype eq { pop {(true)}{(false)} ifelse exit } if
dup/marktype eq { pop (MARK) exit } if
dup/nulltype eq { pop (-) exit } if
dup/savetype eq { pop (-save-) exit } if
dup/filetype eq { pop (-file-) exit } if
dup/fonttype eq { pop (-font-) exit } if
dup/gstatetype eq { pop (-gstate-) exit } if
/sprintf_element cvx errordict/typecheck get exec
} loop
} def
/sprintf {
% construct array of sub-strings plus values to format
[ exch (%) {
search not { exit } if
counttomark 2 add -1 roll 4 2 roll
} loop ]
% turn it into an array of strings
[exch{_sprintf}forall]
% merge strings into a single array
0 1 index { length add } forall string exch
0 exch { 3 copy putinterval length add } forall
pop
} def
Bonus points if you enclose executable arrays and in braces.
Regards,
David
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)