Skip to content

Instantly share code, notes, and snippets.

@dreamiurg
Created March 14, 2011 17:04
Show Gist options
  • Save dreamiurg/869465 to your computer and use it in GitHub Desktop.
Save dreamiurg/869465 to your computer and use it in GitHub Desktop.
void tokenize(const string& instr, Tokens& tokens)
{
// trim spaces & not valid chars
string str;
trim_acс trimmer(str);
for_each(instr.begin(), instr.end(), trimmer);
string::size_type numPos = str.find_first_of(NUMS, 0);
string::size_type endPos = str.find_first_not_of(NUMS, numPos);
string::size_type opPos;
while (std::string::npos != endPos || std::string::npos != numPos)
{
// push number
tokens.push_back(str.substr(numPos, endPos - numPos));
// find & push op
opPos = str.find_first_of(OPS, endPos);
if (std::string::npos != opPos)
{
tokens.push_back(str.substr(opPos, 1));
}
numPos = str.find_first_of(NUMS, opPos);
endPos = str.find_first_not_of(NUMS, numPos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment