diff options
author | Sadie Powell <sadie@witchery.services> | 2025-04-14 11:31:19 +0100 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2025-04-14 11:31:19 +0100 |
commit | d04a312d0d1edd1223b55ed89b7fb707ceca3117 (patch) | |
tree | 39b0c165b17d5ff4571423b2bc279b34546c3b72 /src/misc.cpp | |
parent | 099f0ce43a7e34e4a6e74d91aa911c745c262ba8 (diff) |
Add Anope::Templace and switch all template strings to use it.
Diffstat (limited to 'src/misc.cpp')
-rw-r--r-- | src/misc.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/misc.cpp b/src/misc.cpp index c1c963f6d..73a6fd9bd 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -856,3 +856,41 @@ bool Anope::ParseCTCP(const Anope::string &text, Anope::string &name, Anope::str body = text.substr(start_of_body, text.length() - start_of_body - end_of_ctcp); return true; } + +Anope::string Anope::Template(const Anope::string &str, const Anope::map<Anope::string> &vars) +{ + Anope::string out; + for (size_t idx = 0; idx < str.length(); ++idx) + { + if (str[idx] != '{') + { + out.push_back(str[idx]); + continue; + } + + for (size_t endidx = idx + 1; endidx < str.length(); ++endidx) + { + if (str[endidx] == '}') + { + if (endidx - idx == 1) + { + // foo{{bar is an escape of foo{bar + out.push_back('{'); + idx = endidx; + break; + } + + auto var = vars.find(str.substr(idx + 1, endidx - idx - 1)); + if (var != vars.end()) + { + // We have a variable, replace it in the string. + out.append(var->second); + } + + idx = endidx; + break; + } + } + } + return out; +} |