From efae57d56bd3df885fd23f7ab11655a684b24e40 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 8 Dec 2013 16:19:38 -0500 Subject: [PATCH] Merge from libexpr - Incorporated Nibbler changes from libexpr. --- src/Nibbler.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++- src/Nibbler.h | 6 +++- test/nibbler.t.cpp | 18 ++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index 421612eb1..a42fc73e1 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -294,6 +294,89 @@ bool Nibbler::getDigit (int& result) return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getDigit6 (int& result) +{ + std::string::size_type i = _cursor; + if (i < _length && + _length - i >= 6) + { + if (isdigit (_input[i + 0]) && + isdigit (_input[i + 1]) && + isdigit (_input[i + 2]) && + isdigit (_input[i + 3]) && + isdigit (_input[i + 4]) && + isdigit (_input[i + 5])) + { + result = strtoimax (_input.substr (_cursor, 6).c_str (), NULL, 10); + _cursor += 6; + return true; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getDigit4 (int& result) +{ + std::string::size_type i = _cursor; + if (i < _length && + _length - i >= 4) + { + if (isdigit (_input[i + 0]) && + isdigit (_input[i + 1]) && + isdigit (_input[i + 2]) && + isdigit (_input[i + 3])) + { + result = strtoimax (_input.substr (_cursor, 4).c_str (), NULL, 10); + _cursor += 4; + return true; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getDigit3 (int& result) +{ + std::string::size_type i = _cursor; + if (i < _length && + _length - i >= 3) + { + if (isdigit (_input[i + 0]) && + isdigit (_input[i + 1]) && + isdigit (_input[i + 2])) + { + result = strtoimax (_input.substr (_cursor, 3).c_str (), NULL, 10); + _cursor += 3; + return true; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getDigit2 (int& result) +{ + std::string::size_type i = _cursor; + if (i < _length && + _length - i >= 2) + { + if (isdigit (_input[i + 0]) && + isdigit (_input[i + 1])) + { + result = strtoimax (_input.substr (_cursor, 2).c_str (), NULL, 10); + _cursor += 2; + return true; + } + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool Nibbler::getInt (int& result) { @@ -716,7 +799,6 @@ bool Nibbler::getDateISO (time_t& t) } //////////////////////////////////////////////////////////////////////////////// -#ifdef NIBBLER_FEATURE_DATE // Parse the longest integer using the next 'limit' characters of 'result' // following position 'i' (when strict is true, the number of digits must be // equal to limit). @@ -758,6 +840,7 @@ bool Nibbler::parseDigits(std::string::size_type& i, } //////////////////////////////////////////////////////////////////////////////// +#ifdef NIBBLER_FEATURE_DATE bool Nibbler::getDate (const std::string& format, time_t& t) { std::string::size_type i = _cursor; diff --git a/src/Nibbler.h b/src/Nibbler.h index fa04a824f..3b84d1d7d 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -63,6 +63,10 @@ public: bool getN (const int, std::string&); bool getQuoted (char, std::string&, bool quote = false); bool getDigit (int&); + bool getDigit6 (int&); + bool getDigit4 (int&); + bool getDigit3 (int&); + bool getDigit2 (int&); bool getInt (int&); bool getHex (int&); bool getUnsignedInt (int&); @@ -76,8 +80,8 @@ public: bool getUUID (std::string&); bool getPartialUUID (std::string&); bool getDateISO (time_t&); -#ifdef NIBBLER_FEATURE_DATE bool parseDigits(std::string::size_type&, int&, unsigned int, bool strict = true); +#ifdef NIBBLER_FEATURE_DATE bool getDate (const std::string&, time_t&); #endif bool getOneOf (const std::vector &, std::string&); diff --git a/test/nibbler.t.cpp b/test/nibbler.t.cpp index 727b9b94a..612f2c9db 100644 --- a/test/nibbler.t.cpp +++ b/test/nibbler.t.cpp @@ -268,6 +268,24 @@ int main (int argc, char** argv) t.is (i, 2, " '2x' : getDigit () -> 2"); t.notok (n.getDigit (i), " 'x' : getDigit () -> false"); + // bool getDigit6 (int&); + t.diag ("Nibbler::getDigit6"); + n = Nibbler ("654321"); + t.ok (n.getDigit6 (i), " 654321 : getDigit6 () -> true"); + t.is (i, 654321, " 654321 : getDigit6 () -> 654321"); + + // bool getDigit4 (int&); + t.diag ("Nibbler::getDigit4"); + n = Nibbler ("4321"); + t.ok (n.getDigit4 (i), " 4321 : getDigit4 () -> true"); + t.is (i, 4321, " 4321 : getDigit4 () -> 4321"); + + // bool getDigit2 (int&); + t.diag ("Nibbler::getDigit2"); + n = Nibbler ("21"); + t.ok (n.getDigit2 (i), " 21 : getDigit2 () -> true"); + t.is (i, 21, " 21 : getDigit2 () -> 21"); + // bool getInt (int&); t.diag ("Nibbler::getInt"); n = Nibbler ("123 -4");