Forum Discussion
NB_
7 years agoRoku Guru
Here is string interpolation sketched (there may be typos but overall this is sound):
' input = "foo ${bar} baz"
' subsAA = {bar: "xyzzy"}
' output = "foo xyzzy baz"
'cache macro regex singleton somewhere
macroRegEx = m.macroRegEx
if macroRegEx = invalid
macroRegEx = createObject("roRegEx", "\${([^}]*)}", "")
m.macroRegEx = macroRegEx
end if
matches = macroRegEx.matchAll(input) ' get all macro matches (think "introns" in a gene)
if matches.count() = 0
output = input ' nothing to replace
else
gaps = macroRegEx.split(input) ' all "exons" between them
' general case is (note that re.split() compresses empty matches at end of list)
' input = gap0 macro0 gap1 macro1 ... macroN gapN [macroN+1 ... macroN+K]
gaps.reset() ' reset() needed to iterate over roList
if gaps.isNext() then output = gaps.next() else output = ""
for each match in matches
val = subsAA[match[1]]
if val = invalid then match = match[0] ' not found - leave macro
output += val.toStr()
if gaps.isNext() then output += gaps.next()
end for
end if