diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 837441ff5..aa0ee2607 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -368,9 +368,7 @@ bool MathLib::isHex(const std::string& s) bool MathLib::isValidSuffix(std::string::const_iterator it, std::string::const_iterator end) { - enum Status { - START, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU - } state = START; + enum {START, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU, SUFFIX_I, SUFFIX_I6, SUFFIX_I64} state = START; for (; it != end; ++it) { switch (state) { case START: @@ -378,6 +376,8 @@ bool MathLib::isValidSuffix(std::string::const_iterator it, std::string::const_i state = SUFFIX_U; else if (*it == 'l' || *it == 'L') state = SUFFIX_L; + else if (*it == 'i') + state = SUFFIX_I; else return false; break; @@ -409,13 +409,31 @@ bool MathLib::isValidSuffix(std::string::const_iterator it, std::string::const_i else return false; break; + case SUFFIX_I: + if (*it == '6') + state = SUFFIX_I6; + else + return false; + break; + case SUFFIX_I6: + if (*it == '4') + state = SUFFIX_I64; + else + return false; + break; default: return false; + break; } } - return (state == SUFFIX_U) || (state == SUFFIX_L) - || (state == SUFFIX_UL) || (state == SUFFIX_LU) || (state == SUFFIX_LL) - || (state == SUFFIX_ULL) || (state == SUFFIX_LLU); + return ((state == SUFFIX_U) || + (state == SUFFIX_L) || + (state == SUFFIX_UL) || + (state == SUFFIX_LU) || + (state == SUFFIX_LL) || + (state == SUFFIX_ULL) || + (state == SUFFIX_LLU) || + (state == SUFFIX_I64)); } /*! \brief Does the string represent a binary number? diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index c353b7700..fc1360ca0 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -36,6 +36,7 @@ private: TEST_CASE(isdec); TEST_CASE(isoct); TEST_CASE(ishex); + TEST_CASE(isValidSuffix); TEST_CASE(isnegative); TEST_CASE(ispositive); TEST_CASE(isfloat); @@ -533,6 +534,77 @@ private: ASSERT_EQUALS(false, MathLib::isHex("")); } + void isValidSuffix(void) + { + // negative testing + std::string value = "ux"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "ulx"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "lx"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "lux"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "lll"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "garbage"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = ""; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "llu "; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "i"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "iX"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "i6X"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "i64X"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "i64 "; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "i66"; + ASSERT_EQUALS(false, MathLib::isValidSuffix(value.begin(), value.end())); + + // positive testing + value = "u"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "ul"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "ull"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "l"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "lu"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "ll"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "llu"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + + value = "i64"; + ASSERT_EQUALS(true, MathLib::isValidSuffix(value.begin(), value.end())); + } + void ispositive() const { ASSERT_EQUALS(false, MathLib::isPositive("-1"));