diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index 50752756a..09948eda0 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -365,7 +365,7 @@ bool Nibbler::getUnsignedInt (int& result) // int frac? exp? // // int: -// -? digit+ +// (-|+)? digit+ // // frac: // . digit+ @@ -381,7 +381,7 @@ bool Nibbler::getNumber (double& result) std::string::size_type i = _cursor; // [+-]? - if (i < _length && _input[i] == '-') + if (i < _length && (_input[i] == '-' || _input[i] == '+')) ++i; // digit+ @@ -432,6 +432,74 @@ bool Nibbler::getNumber (double& result) return false; } +//////////////////////////////////////////////////////////////////////////////// +// number: +// int frac? exp? +// +// int: +// digit+ +// +// frac: +// . digit+ +// +// exp: +// e digit+ +// +// e: +// e|E (+|-)? +// +bool Nibbler::getUnsignedNumber (double& result) +{ + std::string::size_type i = _cursor; + + // digit+ + if (i < _length && isdigit (_input[i])) + { + ++i; + + while (i < _length && isdigit (_input[i])) + ++i; + + // ( . digit+ )? + if (i < _length && _input[i] == '.') + { + ++i; + + while (i < _length && isdigit (_input[i])) + ++i; + } + + // ( [eE] [+-]? digit+ )? + if (i < _length && (_input[i] == 'e' || _input[i] == 'E')) + { + ++i; + + if (i < _length && (_input[i] == '+' || _input[i] == '-')) + ++i; + + if (i < _length && isdigit (_input[i])) + { + ++i; + + while (i < _length && isdigit (_input[i])) + ++i; + + result = strtof (_input.substr (_cursor, i - _cursor).c_str (), NULL); + _cursor = i; + return true; + } + + return false; + } + + result = strtof (_input.substr (_cursor, i - _cursor).c_str (), NULL); + _cursor = i; + return true; + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool Nibbler::getLiteral (const std::string& literal) { diff --git a/src/Nibbler.h b/src/Nibbler.h index 21f16c0cc..12eb40228 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -60,6 +60,7 @@ public: bool getHex (int&); bool getUnsignedInt (int&); bool getNumber (double&); + bool getUnsignedNumber (double&); bool getLiteral (const std::string&); bool getRx (const std::string&, std::string&); bool getUUID (std::string&);