Here's something simple I cooked up. Pretty basic, it simply searches for a pair of matching $ $s, takes what's between them and searches the map for it, and replaces.
Code:
#include <iostream>
#include <string>
#include <map>
typedef std::map<std::string, std::string> stringMap;
std::string parse(std::string s, const stringMap& m){
std::string::size_type first = 0,second;
while(true){
if ((first = s.find("$",first)) == s.npos) break;
if ((second = s.find("$",first + 1)) == s.npos) break;
std::string key = s.substr(first + 1, second-first-1), val;
stringMap::const_iterator i = m.find(key);
if (i != m.end()) val = i->second;
s.replace(first, second-first + 1, val);
first = second + 1;
}
return s;
}
int main(){
stringMap m;
m["cd"] = "[Test string here]";
std::string s = parse("ab$cd$$$efg$h$i$jkl",m);
std::cout << s << std::endl;
}
This does the following replacements:
$cd$ is replaced by [Test string here]
$$ is replaced by nothing (key doesn't exist)
$h$ is replaced by nothing (key doesn't exist)
Last $ is left alone (it's unmatched)
so the output is:
Code:
ab[Test string here]efgi$jkl
This does present problems if you have any $s in the string that you don't want to be made into a variable name. You could use some kind of escape sequence for the character $, like use $$. You'd have to modify the position searching algorithm slightly; you'd probably want to code your own find() command.