single tpl

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <string.h> #include <vector> #include <map> #include <memory> using namespace std; typedef std::shared_ptr<vector<string> > VecStrPtr; typedef vector<string> VecStr; map<string, string> vars; int main() { vars["world"] = "earth"; vars["testName"] = "zivee"; vars["tm"] = "more"; string test("Hello {$world}! {$testName} , again {$tm}"); const char* tpl = test.c_str(); VecStrPtr placeholdsPtr(new VecStr); VecStrPtr phasePtr(new VecStr); char start = '{'; char prefix = '$'; char end = '}'; char tmp[128]; unsigned i,j; unsigned p=0; for (i=0; tpl[i]!='\0'; ++i) { if (tpl[i] == start && tpl[i+1] == prefix ) { phasePtr->push_back(test.substr(p, i-p)); for(j = 0; j<128 && tpl[i+j+2]!=end && tpl[i+j+2]!='\0'; ++j) { tmp[j] = tpl[i+j+2]; } tmp[j] = '\0'; i += j+2; p = i+1; placeholdsPtr->push_back(tmp); } } if (tpl[p]!='\0') { phasePtr->push_back(test.substr(p)); } //share ptr string result; for (size_t i=0; i<placeholdsPtr->size(); ++i){ result.append(phasePtr->at(i)); result.append(vars[placeholdsPtr->at(i)]); } if (i < phasePtr->size()) { result.append(phasePtr->at(i)); } cout <<result << endl; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines