mirror of
https://github.com/unknownworlds/NS.git
synced 2024-12-11 05:10:56 +00:00
47 lines
1.6 KiB
C++
47 lines
1.6 KiB
C++
|
#include "util/nowarnings.h"
|
||
|
#include "util/Tokenizer.h"
|
||
|
|
||
|
int Tokenizer::split(const string& input, const string& delimiters, StringVector& result, char quoteChar)
|
||
|
{
|
||
|
size_t subStart = 0;
|
||
|
size_t subEnd = 0;
|
||
|
size_t length = 0;
|
||
|
|
||
|
result.clear(); // erase result before we start
|
||
|
|
||
|
while (true)
|
||
|
{
|
||
|
if ((subStart = input.find_first_not_of(delimiters, subEnd)) == string::npos)
|
||
|
break;
|
||
|
|
||
|
// if we're handling quotes
|
||
|
if (quoteChar && (input[subStart] == quoteChar))
|
||
|
{
|
||
|
++subStart; // skip the open quote
|
||
|
// have to turn quoteChar into a string for find_first_of
|
||
|
if ((subEnd = input.find_first_of(string(1, quoteChar), subStart))
|
||
|
== string::npos)
|
||
|
{
|
||
|
subEnd = input.size();
|
||
|
length = subEnd - subStart;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// subStart has already been incremented to skip the open quote;
|
||
|
// subEnd excludes the close quote; so we do NOT have to tweak the
|
||
|
// difference to get a count of the characters between them.
|
||
|
length = subEnd - subStart;
|
||
|
++subEnd; // now skip the close quote
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if ((subEnd = input.find_first_of(delimiters, subStart)) == string::npos)
|
||
|
subEnd = input.size();
|
||
|
length = subEnd - subStart;
|
||
|
}
|
||
|
result.push_back(input.substr(subStart, length));
|
||
|
}
|
||
|
return (int)result.size();
|
||
|
}
|