[REBOL] Re: Printf
From: stasil213:yah:oo at: 23-Dec-2003 1:32
Tim,
I'm relatively new to dialects, too, but I gave it a try.
Though the gurus can probably do it better, I have discovered that the rule
copy x skip (append block x)
works like an "else do nothing" at the end of a string substituion parse.
I did get it to keep the s's, but my code can probably be made much more
effiecient, and tested a lot better.
Merry Christmas to all,
Stan Silver
REBOL [
Title: "printf"
Author: "Tim Johnson"
ModifiedBy: "Stan Silver"
]
printf: function[str[string!] subs[block!]]
[delims non-delims blk ndx ][
delims: charset "%s"
non-delims: complement delims
blk: copy []
ndx: 1
parse/all str[
some [
copy txt some non-delims (
append blk txt
if ndx <= (length? subs)[
append blk subs/:ndx
]
ndx: ndx + 1
) |
delims
] ; end some
]
rejoin blk
];end func
printf2: function[str [string!] subs [block!]]
[delim blk ndx x][
delim: "%s"
blk: copy []
ndx: 1
parse/all str [
some [
delim
( if ndx <= (length? subs)[
append blk subs/:ndx
]
ndx: ndx + 1
)
|
copy x skip (append blk x)
] ; end some
]; end parse
rejoin blk
];end func
; ======;
; TESTS ;
; ======;
???: func [result 'ignore desired] [
if not (result = desired) [
print ["TEST FAILED" mold result ignore mold desired]
]
]
??? printf "doesn't%skeep%sthe_s's" [1 2] >> "doe1n't2keepthe_'"
??? printf2 "does%skeep%sthe_s's" [1 2] >> "does1keep2the_s's"
??? 'should >> 'fail