diff --git a/src/Pig.cpp b/src/Pig.cpp index d67ce695..1e555b68 100644 --- a/src/Pig.cpp +++ b/src/Pig.cpp @@ -79,7 +79,7 @@ bool Pig::skipWS () //////////////////////////////////////////////////////////////////////////////// bool Pig::skipLiteral (const std::string& literal) { - if (_text.find (literal) == _cursor) + if (_text.find (literal, _cursor) == _cursor) { _cursor += literal.length (); return true; @@ -88,11 +88,40 @@ bool Pig::skipLiteral (const std::string& literal) return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Pig::getUntilWS (std::string& result) +{ + auto save = _cursor; + + int c; + auto prev = _cursor; + while ((c = utf8_next_char (_text, _cursor))) + { + if (eos ()) + { + result = _text.substr (save, _cursor - save); + return true; + } + + else if (Lexer::isWhitespace (c)) + { + _cursor = prev; + result = _text.substr (save, _cursor - save); + return true; + } + + prev = _cursor; + } + + return _cursor > save; +} + //////////////////////////////////////////////////////////////////////////////// bool Pig::getDigit (int& result) { int c = _text[_cursor]; - if (Lexer::isDigit (c)) + if (c && + Lexer::isDigit (c)) { result = c - '0'; ++_cursor; diff --git a/src/Pig.h b/src/Pig.h index ae505761..43ce6d3b 100644 --- a/src/Pig.h +++ b/src/Pig.h @@ -38,6 +38,7 @@ public: bool skipWS (); bool skipLiteral (const std::string&); + bool getUntilWS (std::string&); bool getDigit (int&); bool getDigits (int&); bool getRemainder (std::string&);