Fix some compiler in MathLib::characterLiteralToLongNumber + some small refactoring

This commit is contained in:
Alexander Mai 2015-11-23 20:41:21 +01:00
parent 5135bae777
commit a7ab5ecf08
3 changed files with 21 additions and 9 deletions

View File

@ -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;
}
}
}
}

View File

@ -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);
};

View File

@ -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"));