diff options
Diffstat (limited to 'src/utils/stringutils.cpp')
-rw-r--r-- | src/utils/stringutils.cpp | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index ba5f575b..7b05b365 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -111,14 +111,13 @@ std::string &replaceCharacters(std::string &str, return str; } -std::string removeColors(std::string msg) +std::string &removeColors(std::string &msg) { - for (unsigned int f = 0; f < msg.length() - 2 && msg.length() > 2; f++) + auto pos = msg.find("##"); + while (pos != std::string::npos && msg.length() - pos >= 3) { - while (msg.length() > f + 2 && msg.at(f) == '#' && msg.at(f + 1) == '#') - { - msg = msg.erase(f, 3); - } + msg.erase(pos, 3); + pos = msg.find("##", pos); } return msg; } @@ -153,6 +152,32 @@ bool getBoolFromString(std::string text, bool def) return def; } +// Overload for std::vector<int> to parse comma-separated integers +void fromString(const char *str, std::vector<int> &value) +{ + value.clear(); + + const char *p = str; + while (*p) + { + while (*p == ' ' || *p == ',') + ++p; // skip spaces and commas + if (!*p) + break; + char *end = nullptr; + int v = static_cast<int>(strtol(p, &end, 10)); + if (end != p) + { + value.push_back(v); + p = end; + } + else + { + ++p; // skip invalid character to avoid infinite loop + } + } +} + std::string autocomplete(const std::vector<std::string> &candidates, std::string base) { |