def self.dsub(text, dict, offset=nil)
text = text.to_s
j = text.index('}', offset || 0)
return text unless j
t = text[0, j]
i = t.rindex('${')
ii = t.rindex("\\${")
iii = t.rindex('{')
iii = nil if offset
return text unless i
return dsub(text, dict, j+1) if (iii) and (iii-1 > i)
return unescape(text) if (i) and (i != 0) and (ii == i-1)
key = text[i+2..j-1]
quote = false
if m = key.match(/^['"](.+)$/)
key = m[1]
quote = true
end
value = dict[key]
value = if value.nil?
''
elsif value.is_a?(String)
value
else
value.inspect
end
value = value.inspect if quote
pre = (i > 0) ? text[0..i-1] : ''
dsub("#{pre}#{value}#{text[j+1..-1]}", dict)
end