[REBOL] Parse refactoring puzzle
From: SunandaDH:aol at: 30-Jul-2003 2:10
I've just written a function which, though it works, annoys me as I'm sure if
I knew a bit more about parse, I could do it in one parse rather than a parse
in a loop.
The problem is we have a string that has some "place-holders" (identified as
!!xxx!! where xxx can be anything), eg:
{Welcome !!name!! your address is !!street!! !!town!! !!city!! !!state!!
!!country!!}
Code before ours has made substitutions, but may have left some stray
place-holders:
{Welcome John-Paul your address is The Vatican !!town!! Roma !!state!!
Italia}
So I now want to remove the remaining place-holders:
{Welcome John-Paul your address is The Vatican Roma Italia}
(We don't need to clear the doubled spaces. We'll let the caller do a trim if
that's what they want).
The function I wrote, and some test data is below. Can anyone suggest a
one-parse approach?
Thanks!
Sunanda.
;; =======================================
clear-place-holders: func [str [string!]
/local out-str ph
][
out-str: copy str ;;work on copy so str if unchanged if we fail
forever [
ph: none
parse/all out-str [thru "!!" copy ph to "!!"]
if none? ph [break]
replace/all out-str join "!!" [ph "!!"] ""
]
return out-str
]
print clear-place-holders {no place holders in this string}
print clear-place-holders {!!begin!!text!!end!!}
print clear-place-holders {text!!middle!! more text}
print clear-place-holders {!!bad-holder and some text}
print clear-place-holders {!!good!holder, even if a little weird!!and some
text}
print clear-place-holders {same!!same!!!!same!!!!same!! place-holder!!same!!
five!!same!! times}
print clear-place-holders {leaves null place-holder !!!! in output, but
another function might remove it. Either way is fine}
;; =======================================