1
0
mirror of https://github.com/anope/anope.git synced 2026-07-06 07:13:13 +02:00

Add Anope::Templace and switch all template strings to use it.

This commit is contained in:
Sadie Powell
2025-04-14 11:31:19 +01:00
parent 099f0ce43a
commit d04a312d0d
21 changed files with 199 additions and 132 deletions
+38
View File
@@ -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;
}