From 74d10cd6d9760a85190099a1bf49b081fa9376c3 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Fri, 14 Mar 2014 11:46:41 +0100 Subject: [PATCH] Added function MathLib::isValidSuffix() --- lib/mathlib.cpp | 108 +++++++++++------------------------------------- lib/mathlib.h | 2 + 2 files changed, 27 insertions(+), 83 deletions(-) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index a7b6e11d0..24ba56731 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -137,7 +137,7 @@ bool MathLib::isPositive(const std::string &s) **/ bool MathLib::isOct(const std::string& s) { - enum {START, PLUSMINUS, OCTAL_PREFIX, DIGITS, UNSIGNED_SUFFIX, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU} state = START; + enum {START, PLUSMINUS, OCTAL_PREFIX, DIGITS} state = START; for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) { switch (state) { case START: @@ -154,7 +154,6 @@ bool MathLib::isOct(const std::string& s) else return false; break; - case OCTAL_PREFIX: if (isOctalDigit(*it)) state = DIGITS; @@ -164,50 +163,14 @@ bool MathLib::isOct(const std::string& s) case DIGITS: if (isOctalDigit(*it)) state = DIGITS; - else if (*it == 'u' || *it == 'U') - state = SUFFIX_U; - else if (*it == 'l' || *it == 'L') - state = SUFFIX_L; else - return false; - break; - case SUFFIX_U: - if (*it == 'l' || *it == 'L') - state = SUFFIX_UL; // UL - else - return false; - break; - case SUFFIX_UL: - if (*it == 'l' || *it == 'L') - state = SUFFIX_ULL; // ULL - else - return false; - break; - case SUFFIX_L: - if (*it == 'u' || *it == 'U') - state = SUFFIX_LU; // LU - else if (*it == 'l' || *it == 'L') - state = SUFFIX_LL; // LL - else - return false; - break; - case SUFFIX_LU: - return false; - break; - case SUFFIX_LL: - if (*it == 'u' || *it == 'U') - state = SUFFIX_LLU; // LLU - else - return false; + return isValidSuffix(it,s.end()); break; default: return false; } } - return (state == DIGITS) - || (state == SUFFIX_U) || (state == SUFFIX_L) - || (state == SUFFIX_UL) || (state == SUFFIX_LU) || (state == SUFFIX_LL) - || (state == SUFFIX_ULL) || (state == SUFFIX_LLU); + return state == DIGITS; } bool MathLib::isHex(const std::string& s) @@ -216,7 +179,7 @@ bool MathLib::isHex(const std::string& s) if (s.empty()) return false; - enum {START, PLUSMINUS, HEX_PREFIX, DIGIT, DIGITS, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU} state = START; + enum {START, PLUSMINUS, HEX_PREFIX, DIGIT, DIGITS} state = START; for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) { switch (state) { case START: @@ -248,7 +211,23 @@ bool MathLib::isHex(const std::string& s) case DIGITS: if (isxdigit(*it)) state = DIGITS; - else if (*it == 'u' || *it == 'U') + else + return isValidSuffix(it,s.end()); + break; + default: + return false; + } + } + return state == DIGITS; +} + +bool MathLib::isValidSuffix(std::string::const_iterator it, std::string::const_iterator end) +{ + enum {START, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU} state = START; + for (; it != end; ++it) { + switch (state) { + case START: + if (*it == 'u' || *it == 'U') state = SUFFIX_U; else if (*it == 'l' || *it == 'L') state = SUFFIX_L; @@ -288,8 +267,7 @@ bool MathLib::isHex(const std::string& s) return false; } } - return (state == DIGITS) - || (state == SUFFIX_U) || (state == SUFFIX_L) + return (state == SUFFIX_U) || (state == SUFFIX_L) || (state == SUFFIX_UL) || (state == SUFFIX_LU) || (state == SUFFIX_LL) || (state == SUFFIX_ULL) || (state == SUFFIX_LLU); } @@ -305,7 +283,7 @@ bool MathLib::isHex(const std::string& s) **/ bool MathLib::isBin(const std::string& s) { - enum {START, PLUSMINUS, GNU_BIN_PREFIX, DIGIT, DIGITS, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU} state = START; + enum {START, PLUSMINUS, GNU_BIN_PREFIX, DIGIT, DIGITS} state = START; for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) { switch (state) { case START: @@ -337,50 +315,14 @@ bool MathLib::isBin(const std::string& s) case DIGITS: if (*it == '0' || *it == '1') state = DIGITS; - else if (*it == 'u' || *it == 'U') - state = SUFFIX_U; - else if (*it == 'l' || *it == 'L') - state = SUFFIX_L; else - return false; - break; - case SUFFIX_U: - if (*it == 'l' || *it == 'L') - state = SUFFIX_UL; // UL - else - return false; - break; - case SUFFIX_UL: - if (*it == 'l' || *it == 'L') - state = SUFFIX_ULL; // ULL - else - return false; - break; - case SUFFIX_L: - if (*it == 'u' || *it == 'U') - state = SUFFIX_LU; // LU - else if (*it == 'l' || *it == 'L') - state = SUFFIX_LL; // LL - else - return false; - break; - case SUFFIX_LU: - return false; - break; - case SUFFIX_LL: - if (*it == 'u' || *it == 'U') - state = SUFFIX_LLU; // LLU - else - return false; + return isValidSuffix(it,s.end()); break; default: return false; } } - return (state == DIGITS) - || (state == SUFFIX_U) || (state == SUFFIX_L) - || (state == SUFFIX_UL) || (state == SUFFIX_LU) || (state == SUFFIX_LL) - || (state == SUFFIX_ULL) || (state == SUFFIX_LLU); + return state == DIGITS; } bool MathLib::isInt(const std::string & s) diff --git a/lib/mathlib.h b/lib/mathlib.h index 708cff03b..8dd8df604 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -50,6 +50,8 @@ public: static bool isOct(const std::string& str); static bool isBin(const std::string& str); + static bool isValidSuffix(std::string::const_iterator it, std::string::const_iterator end); + static std::string add(const std::string & first, const std::string & second); static std::string subtract(const std::string & first, const std::string & second); static std::string multiply(const std::string & first, const std::string & second);