Skip to content

Commit

Permalink
KviCString::getToken(): add option to skip empty elements (default of…
Browse files Browse the repository at this point in the history
…f) (#2574)
  • Loading branch information
ctrlaltca authored Nov 30, 2023
1 parent 7916bf0 commit 9babfaf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/kvilib/core/KviCString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/kvilib/core/KviCString.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ class KVILIB_API KviCString : public KviHeapObject
// and returns true if there are more tokens to extract<br>
// Does not strip initial separators!!<br>
// 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!<br>
// Can assign also to this string.
KviCString getToken(char sep);
KviCString getToken(char sep, bool skipEmpty = false);
// Extracts a line from the string.<br>
// Returns false if there was no data to extract
bool getLine(KviCString & str);
Expand Down
2 changes: 1 addition & 1 deletion src/kvirc/sparser/KviIrcServerParser_numericHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 9babfaf

Please sign in to comment.