diff options
| author | 2022-06-15 12:19:23 +0100 | |
|---|---|---|
| committer | 2022-06-15 12:30:24 +0100 | |
| commit | 429b973af669230e2a81fc83d74bdd56b88549c8 (patch) | |
| tree | ef5f06becfd39d96b63771d03312cdb716e3ae02 /src/inspstring.cpp | |
| parent | Update UNIX CI images to use the latest tooling. (diff) | |
Add a basic function for templating strings.
Diffstat (limited to 'src/inspstring.cpp')
| -rw-r--r-- | src/inspstring.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/inspstring.cpp b/src/inspstring.cpp index 7571333ae..7ac9974ee 100644 --- a/src/inspstring.cpp +++ b/src/inspstring.cpp @@ -185,6 +185,48 @@ std::string Base64::Decode(const void* data, size_t length, const char* table) return buffer; } +std::string Template::Replace(const std::string& str, const insp::flat_map<std::string, std::string>& vars) +{ + std::string out; + out.reserve(str.length()); + + for (size_t idx = 0; idx < str.size(); ++idx) + { + if (str[idx] != '%') + { + out.push_back(str[idx]); + continue; + } + + for (size_t endidx = idx + 1; endidx < str.size(); ++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; +} + + bool InspIRCd::TimingSafeCompare(const std::string& one, const std::string& two) { if (one.length() != two.length()) |
