diff options
| author | 2024-08-29 15:22:21 +0100 | |
|---|---|---|
| committer | 2024-08-29 22:12:32 +0100 | |
| commit | 7a59ed2a903dbfafdbd878ac998526f56f44f01b (patch) | |
| tree | 2b7e755ddd4b524e1a899f92856726f4a2ebfbfc /src/helperfuncs.cpp | |
| parent | Use auto in GetLevelRequired and GetSyntax. (diff) | |
Refactor InspIRCd::ProcessColors.
Diffstat (limited to 'src/helperfuncs.cpp')
| -rw-r--r-- | src/helperfuncs.cpp | 76 |
1 files changed, 32 insertions, 44 deletions
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 5ccb8ed6c..3e50f6439 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -125,60 +125,48 @@ void InspIRCd::ProcessColors(std::vector<std::string>& input) ProcessColors(line); } -void InspIRCd::ProcessColors(std::string& ret) +void InspIRCd::ProcessColors(std::string& line) { - /* - * Replace all color codes from the special[] array to actual - * color code chars using C++ style escape sequences. You - * can append other chars to replace if you like -- Justasic - */ - static struct special_chars final - { - std::string character; - std::string replace; - special_chars(const std::string& c, const std::string& r) - : character(c) - , replace(r) - { - } - } special[] = { - special_chars("\\b", "\x02"), // Bold - special_chars("\\c", "\x03"), // Color - special_chars("\\h", "\x04"), // Hex Color - special_chars("\\i", "\x1D"), // Italic - special_chars("\\m", "\x11"), // Monospace - special_chars("\\r", "\x16"), // Reverse - special_chars("\\s", "\x1E"), // Strikethrough - special_chars("\\u", "\x1F"), // Underline - special_chars("\\x", "\x0F"), // Reset - special_chars("", "") + static const insp::flat_map<std::string::value_type, std::string> formats = { + { '\\', "\\" }, // Escape + { 'b', "\x02" }, // Bold + { 'c', "\x03" }, // Color + { 'h', "\x04" }, // Hex Color + { 'i', "\x1D" }, // Italic + { 'm', "\x11" }, // Monospace + { 'r', "\x16" }, // Reverse + { 's', "\x1E" }, // Strikethrough + { 'u', "\x1F" }, // Underline + { 'x', "\x0F" }, // Reset }; + for (size_t idx = 0; idx < line.length(); ) { - for(int i = 0; !special[i].character.empty(); ++i) + if (line[idx] != '\\') { - std::string::size_type pos = ret.find(special[i].character); - if(pos == std::string::npos) // Couldn't find the character, skip this line - continue; - - if((pos > 0) && (ret[pos-1] == '\\') && (ret[pos] == '\\')) - continue; // Skip double slashes. + // Regular character. + idx++; + continue; + } - // Replace all our characters in the array - while(pos != std::string::npos) - { - ret = ret.substr(0, pos) + special[i].replace + ret.substr(pos + special[i].character.size()); - pos = ret.find(special[i].character, pos + special[i].replace.size()); - } + auto start = idx; + if (++idx >= line.length()) + { + // Stray \ at the end of the string; strip. + line.pop_back(); + continue; } - // Replace double slashes with a single slash before we return - std::string::size_type pos = ret.find("\\\\"); - while(pos != std::string::npos) + const auto it = formats.find(line[idx]); + if (it == formats.end()) { - ret = ret.substr(0, pos) + "\\" + ret.substr(pos + 2); - pos = ret.find("\\\\", pos + 1); + // Unknown escape, strip. + line.erase(start, 2); + continue; } + + line.replace(start, 2, it->second); + idx = start + it->second.length(); } } |
