Mathlib: Fix value of suffixed binary integer literal (#2487)

This commit is contained in:
Rikard Falkeborn 2020-01-12 08:11:12 +01:00 committed by Daniel Marjamäki
parent c29f9eaf44
commit c6e74f4c10
2 changed files with 16 additions and 0 deletions

View File

@ -319,6 +319,8 @@ MathLib::biguint MathLib::toULongNumber(const std::string & str)
if (isBin(str)) {
biguint ret = 0;
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
if (str[i] != '1' && str[i] != '0')
break;
ret <<= 1;
if (str[i] == '1')
ret |= 1;
@ -510,6 +512,8 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str)
if (isBin(str)) {
bigint ret = 0;
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
if (str[i] != '1' && str[i] != '0')
break;
ret <<= 1;
if (str[i] == '1')
ret |= 1;

View File

@ -272,6 +272,11 @@ private:
// from binary
ASSERT_EQUALS(0, MathLib::toLongNumber("0b0"));
ASSERT_EQUALS(1, MathLib::toLongNumber("0b1"));
ASSERT_EQUALS(1, MathLib::toLongNumber("0b1U"));
ASSERT_EQUALS(1, MathLib::toLongNumber("0b1L"));
ASSERT_EQUALS(1, MathLib::toLongNumber("0b1LU"));
ASSERT_EQUALS(1, MathLib::toLongNumber("0b1LL"));
ASSERT_EQUALS(1, MathLib::toLongNumber("0b1LLU"));
ASSERT_EQUALS(1, MathLib::toLongNumber("+0b1"));
ASSERT_EQUALS(-1, MathLib::toLongNumber("-0b1"));
ASSERT_EQUALS(215, MathLib::toLongNumber("0b11010111"));
@ -347,6 +352,13 @@ private:
ASSERT_EQUALS(10000U, MathLib::toULongNumber("1e4"));
ASSERT_EQUALS(0xFF00000000000000UL, MathLib::toULongNumber("0xFF00000000000000UL"));
ASSERT_EQUALS(0x0A00000000000000UL, MathLib::toULongNumber("0x0A00000000000000UL"));
ASSERT_EQUALS(0, MathLib::toULongNumber("0b0"));
ASSERT_EQUALS(1, MathLib::toULongNumber("0b1"));
ASSERT_EQUALS(1, MathLib::toULongNumber("0b1U"));
ASSERT_EQUALS(1, MathLib::toULongNumber("0b1L"));
ASSERT_EQUALS(1, MathLib::toULongNumber("0b1LU"));
ASSERT_EQUALS(1, MathLib::toULongNumber("0b1LL"));
ASSERT_EQUALS(1, MathLib::toULongNumber("0b1LLU"));
ASSERT_EQUALS(9U, MathLib::toULongNumber("011"));
ASSERT_EQUALS(5U, MathLib::toULongNumber("0b101"));