From a7ab5ecf089c07e68fb9f5f3f15e6c2757f89d1e Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Mon, 23 Nov 2015 20:41:21 +0100 Subject: [PATCH] Fix some compiler in MathLib::characterLiteralToLongNumber + some small refactoring --- lib/mathlib.cpp | 22 ++++++++++++++-------- lib/mathlib.h | 1 - test/testmathlib.cpp | 7 +++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 4e5dd414b..0fac3509e 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -313,6 +313,14 @@ MathLib::biguint MathLib::toULongNumber(const std::string & str) return ret; } +static bool isOctalDigitString(const std::string& str) +{ + for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { + if (!MathLib::isOctalDigit(*it)) + return false; + } + return true; +} MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str) { @@ -330,7 +338,7 @@ MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str) for (std::string::const_iterator it=str.begin()+1; it!=str.end(); ++it) { retval = retval<<8 | *it; } - return retval; // str[0] & 0xff; + return retval; } switch (str[1]) { @@ -383,18 +391,16 @@ MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str) break; default: throw InternalError(0, "Internal Error. MathLib::toLongNumber: Unhandled char constant " + str); - break; } return c & 0xff; case 2: - if (isOctalDigit(str[1]) && isOctalDigit(str[2])) - return toLongNumber("0" + str.substr(1)); - break; - case 3: - if (isOctalDigit(str[1]) && isOctalDigit(str[2]) && isOctalDigit(str[3])) - return toLongNumber("0" + str.substr(1)); + case 3: { + const std::string& str1 = str.substr(1); + if (isOctalDigitString(str1)) + return toLongNumber("0" + str1); break; } + } } } diff --git a/lib/mathlib.h b/lib/mathlib.h index 683e60b19..eba2a5931 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -113,7 +113,6 @@ public: * @return true if given character is octal digit. */ static bool isOctalDigit(char c); -private: static MathLib::bigint characterLiteralToLongNumber(const std::string& str); }; diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 44a02a49c..aaa8c99b8 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -293,6 +293,13 @@ private: ASSERT_THROW(MathLib::toLongNumber("'\\u9343'"), InternalError); ASSERT_THROW(MathLib::toLongNumber("'\\U0001f34c'"), InternalError); + { + // some unit-testing for a utility function + ASSERT_EQUALS(0, MathLib::characterLiteralToLongNumber(std::string(""))); + ASSERT_EQUALS(32, MathLib::characterLiteralToLongNumber(std::string(" "))); + ASSERT_EQUALS(538976288, MathLib::characterLiteralToLongNumber(std::string(" "))); + } + ASSERT_EQUALS(-8552249625308161526, MathLib::toLongNumber("0x89504e470d0a1a0a")); ASSERT_EQUALS(-8481036456200365558, MathLib::toLongNumber("0x8a4d4e470d0a1a0a")); ASSERT_EQUALS(9894494448401390090ULL, MathLib::toULongNumber("0x89504e470d0a1a0a"));