MCPcopy Create free account
hub / github.com/QuEST-Kit/QuEST / getStringWithSubstitutedVars

Function getStringWithSubstitutedVars

quest/src/core/validation.cpp:1259–1291  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1257using tokenSubs = std::map<string, long long int>;
1258
1259string getStringWithSubstitutedVars(string oldStr, tokenSubs varsAndVals) {
1260
1261 string newStr = oldStr;
1262
1263 // substitute every var,val pair into newStr
1264 for (auto varAndVal : varsAndVals) {
1265
1266 // unpack var -> val
1267 string var = std::get<0>(varAndVal);
1268 string val = std::to_string(std::get<1>(varAndVal));
1269
1270 // assert var is well-formed
1271 if (var[0] != '$' || var[1] != '{' || var.back() != '}' )
1272 error_validationMessageVarWasIllFormed(newStr, var);
1273
1274 // assert var appears at least once in string
1275 if (newStr.find(var) == string::npos)
1276 error_validationMessageVarNotSubstituted(newStr, var);
1277
1278 // replace every occurrence of var with val
1279 size_t ind = newStr.find(var);
1280 while (ind != string::npos) {
1281 newStr = newStr.replace(ind, var.length(), val);
1282 ind = newStr.find(var, ind);
1283 }
1284 }
1285
1286 // assert there is no $ left in the strings
1287 if (newStr.find("$") != string::npos)
1288 error_validationMessageContainedUnsubstitutedVars(newStr);
1289
1290 return newStr;
1291}
1292
1293void assertThat(bool valid, string msg, const char* func) {
1294

Callers 2

assertThatFunction · 0.85
assertAllNodesAgreeThatFunction · 0.85

Tested by

no test coverage detected