[REBOL] Re: Printf
From: tim:johnsons-web at: 22-Dec-2003 17:23
* Stan Silver <[stasil213--yahoo--com]> [031222 15:59]:
> Tim,
>
> I'm relatively new to dialects, too, but I gave it a try.
Hi Stan:
Ahah! I think you nailed it by adding the 'skip in printf2
- that is one thing I was missing
I made one more change, so that 'subs is wrapped backed
to the begining if there are more delims than subs.
Thanks! My addition is below:
Happy Holidays
tim (still trolling for gurus :-))
;; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
printf2: function[str [string!] subs [block!]]
[delim blk ndx x][
delim: "%s"
blk: copy []
ndx: 1
parse/all str [
some [
delim
(
append blk subs/:ndx
either ndx < (length? subs)[
ndx: ndx + 1
][ ; more delims that subs
ndx: 1 ; 'wrap' subs
]
)
|
copy x skip (append blk x)
] ; end some
]; end parse
rejoin blk
];end func
> 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
>
> --
> To unsubscribe from this list, just send an email to
> [rebol-request--rebol--com] with unsubscribe as the subject.
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.alaska-internet-solutions.com