1
0
mirror of https://github.com/anope/anope.git synced 2026-07-04 21:33:13 +02:00

Add an option to sepstream to allow it to return empty tokens if multiple separators are found in a row

This commit is contained in:
Adam
2013-05-05 01:18:16 -04:00
parent 10b5b00db4
commit e91de41278
4 changed files with 13 additions and 10 deletions
+4 -1
View File
@@ -554,10 +554,13 @@ class CoreExport sepstream
/** Current string position
*/
size_t pos;
/** If set then GetToken() can return an empty string
*/
bool allow_empty;
public:
/** Create a sepstream and fill it with the provided data
*/
sepstream(const Anope::string &source, char seperator);
sepstream(const Anope::string &source, char seperator, bool allowempty = false);
/** Fetch the next token from the stream
* @param token The next token from the stream is placed here
+1 -1
View File
@@ -112,7 +112,7 @@ void CommandSource::Reply(const Anope::string &message)
{
const char *translated_message = Language::Translate(this->nc, message.c_str());
sepstream sep(translated_message, '\n');
sepstream sep(translated_message, '\n', true);
Anope::string tok;
while (sep.GetToken(tok))
this->reply->SendMessage(this->service, tok);
+7 -7
View File
@@ -92,28 +92,28 @@ bool ci::less::operator()(const Anope::string &s1, const Anope::string &s2) cons
return s1.ci_str().compare(s2.ci_str()) < 0;
}
sepstream::sepstream(const Anope::string &source, char seperator) : tokens(source), sep(seperator), pos(0)
sepstream::sepstream(const Anope::string &source, char seperator, bool ae) : tokens(source), sep(seperator), pos(0), allow_empty(ae)
{
}
bool sepstream::GetToken(Anope::string &token)
{
size_t p = this->pos;
while (p < this->tokens.length() && this->tokens[p] == this->sep)
++p;
if (this->StreamEnd())
{
token.clear();
return false;
}
size_t p = this->pos;
while (p < this->tokens.length() && this->tokens[p] != this->sep)
++p;
token = this->tokens.substr(this->pos, p - this->pos);
this->pos = p + 1;
if (!this->allow_empty && token.empty())
return GetToken(token);
return true;
}
@@ -152,6 +152,6 @@ const Anope::string sepstream::GetRemaining()
bool sepstream::StreamEnd()
{
return this->pos >= this->tokens.length();
return this->pos > this->tokens.length();
}
+1 -1
View File
@@ -278,7 +278,7 @@ void User::SendMessage(const BotInfo *source, const Anope::string &msg)
* - The user is not registered and NSDefMsg is enabled
* - The user is registered and has set /ns set msg on
*/
sepstream sep(translated_message, '\n');
sepstream sep(translated_message, '\n', true);
for (Anope::string tok; sep.GetToken(tok);)
{
if (Config->UsePrivmsg && ((!this->nc && Config->DefPrivmsg) || (this->nc && this->nc->HasExt("MSG"))))