[Common] Update stdstr::Tokenize to handle empty items

This commit is contained in:
zilmar 2017-11-15 20:02:44 +11:00
parent 169f9d92bf
commit 2d7524ae2f
1 changed files with 13 additions and 4 deletions

View File

@ -30,12 +30,17 @@ strvector stdstr::Tokenize(const char * delimiter) const
stdstr::size_type lastPos = find_first_not_of(delimiter, 0); stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
stdstr::size_type pos = find_first_of(delimiter, lastPos); stdstr::size_type pos = find_first_of(delimiter, lastPos);
while (stdstr::npos != pos || stdstr::npos != lastPos) size_t delimiter_len = strlen(delimiter);
while (stdstr::npos != pos)
{ {
tokens.push_back(substr(lastPos, pos - lastPos)); tokens.push_back(substr(lastPos, pos - lastPos));
lastPos = find_first_not_of(delimiter, pos); lastPos = pos + delimiter_len;
pos = find_first_of(delimiter, lastPos); pos = find_first_of(delimiter, lastPos);
} }
if (stdstr::npos != lastPos)
{
tokens.push_back(substr(lastPos));
}
return tokens; return tokens;
} }
@ -45,12 +50,16 @@ strvector stdstr::Tokenize(char delimiter) const
stdstr::size_type lastPos = find_first_not_of(delimiter, 0); stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
stdstr::size_type pos = find_first_of(delimiter, lastPos); stdstr::size_type pos = find_first_of(delimiter, lastPos);
while (stdstr::npos != pos || stdstr::npos != lastPos) while (stdstr::npos != pos)
{ {
tokens.push_back(substr(lastPos, pos - lastPos)); tokens.push_back(substr(lastPos, pos - lastPos));
lastPos = find_first_not_of(delimiter, pos); lastPos = pos + 1;
pos = find_first_of(delimiter, lastPos); pos = find_first_of(delimiter, lastPos);
} }
if (stdstr::npos != lastPos)
{
tokens.push_back(substr(lastPos));
}
return tokens; return tokens;
} }