From 71306895bec495f76c538795ee2b05a6b18e4cdc Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 30 Nov 2023 10:36:12 +0100 Subject: [PATCH] KviCString::getToken(): add option to skip empty elements (default off) --- src/kvilib/core/KviCString.cpp | 12 ++++++++++-- src/kvilib/core/KviCString.h | 4 ++-- .../sparser/KviIrcServerParser_numericHandlers.cpp | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/kvilib/core/KviCString.cpp b/src/kvilib/core/KviCString.cpp index b4c8b4f075..9514fa5ada 100644 --- a/src/kvilib/core/KviCString.cpp +++ b/src/kvilib/core/KviCString.cpp @@ -2657,7 +2657,7 @@ KviCString & KviCString::stripLeft(char c) return (*this); } -bool KviCString::getToken(KviCString & str, char sep) +bool KviCString::getToken(KviCString & str, char sep, bool skipEmpty) { KVI_ASSERT(str.m_ptr); KVI_ASSERT(str.m_ptr != m_ptr); @@ -2673,7 +2673,11 @@ bool KviCString::getToken(KviCString & str, char sep) KviMemory::copy(str.m_ptr, m_ptr, str.m_len); *(str.m_ptr + str.m_len) = '\0'; while(*p && (*p == sep)) + { p++; + if(!skipEmpty) + break; + } cutLeft(p - m_ptr); return (m_len != 0); } @@ -2700,14 +2704,18 @@ bool KviCString::getLine(KviCString & str) return true; } -KviCString KviCString::getToken(char sep) +KviCString KviCString::getToken(char sep, bool skipEmpty) { char * p = m_ptr; while(*p && (*p != sep)) p++; KviCString ret(m_ptr, p); while(*p && (*p == sep)) + { p++; + if(!skipEmpty) + break; + } cutLeft(p - m_ptr); return ret; } diff --git a/src/kvilib/core/KviCString.h b/src/kvilib/core/KviCString.h index 43fb25b2f2..459ee8b29a 100644 --- a/src/kvilib/core/KviCString.h +++ b/src/kvilib/core/KviCString.h @@ -349,10 +349,10 @@ class KVILIB_API KviCString : public KviHeapObject // and returns true if there are more tokens to extract
// Does not strip initial separators!!
// str can NOT be this string. - bool getToken(KviCString & str, char sep); + bool getToken(KviCString & str, char sep, bool skipEmpty = false); // Does not strip initial separators!
// Can assign also to this string. - KviCString getToken(char sep); + KviCString getToken(char sep, bool skipEmpty = false); // Extracts a line from the string.
// Returns false if there was no data to extract bool getLine(KviCString & str); diff --git a/src/kvirc/sparser/KviIrcServerParser_numericHandlers.cpp b/src/kvirc/sparser/KviIrcServerParser_numericHandlers.cpp index 5a4b274a86..208718e00c 100644 --- a/src/kvirc/sparser/KviIrcServerParser_numericHandlers.cpp +++ b/src/kvirc/sparser/KviIrcServerParser_numericHandlers.cpp @@ -907,7 +907,7 @@ void KviIrcServerParser::parseNumericWhoReply(KviIrcMessage * msg) bool bIrcOp = szFlag.indexOf('*') != -1; KviCString trailing = msg->safeTrailing(); - KviCString hops = trailing.getToken(' '); + KviCString hops = trailing.getToken(' ', true); bool bHopsOk = false; int iHops = hops.toInt(&bHopsOk);