diff options
| author | 2026-03-14 02:15:38 +0000 | |
|---|---|---|
| committer | 2026-03-14 03:09:11 +0000 | |
| commit | 76f3f24c03c22576324e5af199d3e61d02a79b0d (patch) | |
| tree | efa6b9a0295e7b99dc8c0c47c8dd6030238c8cac /src/stringutils.cpp | |
| parent | Convert some methods to use string_view. (diff) | |
Rewrite sepstream and move to stringutils.
Diffstat (limited to 'src/stringutils.cpp')
| -rw-r--r-- | src/stringutils.cpp | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/stringutils.cpp b/src/stringutils.cpp index a38063c31..7416eae7a 100644 --- a/src/stringutils.cpp +++ b/src/stringutils.cpp @@ -283,7 +283,7 @@ TokenList::TokenList(const std::string& tokenlist) void TokenList::AddList(const std::string& tokenlist) { std::string token; - irc::spacesepstream tokenstream(tokenlist); + StringSplitter tokenstream(tokenlist); while (tokenstream.GetToken(token)) { if (token[0] == '-') @@ -465,3 +465,56 @@ bool MessageTokenizer::GetTrailing(std::string_view& token) // There is no <trailing> token so it must be a <middle> token. return GetMiddle(token); } + +StringSplitter::StringSplitter(const std::string& str, std::string::value_type sep, bool ae, std::string::size_type start, std::string::size_type end) + : allow_empty(ae) + , separator(sep) + , string(str, start, end) +{ +} + +std::string_view StringSplitter::GetRemaining() const +{ + return AtEnd() + ? std::string_view() + : std::string_view(this->string.begin() + this->position, this->string.end()); +} + +bool StringSplitter::GetToken(std::string& token) +{ + if (std::string_view sv; GetToken(sv)) + { + token.assign(sv); + return true; + } + token.clear(); + return false; +} + +bool StringSplitter::GetToken(std::string_view& token) +{ + if (AtEnd()) + { + token = std::string_view(); + return false; + } + + if (!this->allow_empty) + { + this->position = this->string.find_first_not_of(this->separator, this->position); + if (this->position == std::string::npos) + { + token = std::string_view(); + this->position = this->string.length(); + return false; + } + } + + auto pos = this->string.find(this->separator, this->position); + if (pos == std::string::npos) + pos = this->string.length(); + + token = std::string_view(this->string.begin() + this->position, this->string.begin() + pos); + this->position = pos + 1; + return true; +} |
