Bugfix Mathlib::isNullValue for hexadecimal numbers (#2517)

This commit is contained in:
Rikard Falkeborn 2020-02-02 16:40:36 +01:00 committed by GitHub
parent cef06741e5
commit bbfd10a69f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -1342,11 +1342,14 @@ bool MathLib::isNullValue(const std::string &str)
if (str.empty() || (!std::isdigit(static_cast<unsigned char>(str[0])) && (str[0] != '.' && str[0] != '-' && str[0] != '+'))) if (str.empty() || (!std::isdigit(static_cast<unsigned char>(str[0])) && (str[0] != '.' && str[0] != '-' && str[0] != '+')))
return false; // Has to be a number return false; // Has to be a number
bool isHex = isIntHex(str) || isFloatHex(str);
for (char i : str) { for (char i : str) {
if (std::isdigit(static_cast<unsigned char>(i)) && i != '0') // May not contain digits other than 0 if (std::isdigit(static_cast<unsigned char>(i)) && i != '0') // May not contain digits other than 0
return false; return false;
if (i == 'E' || i == 'e') if (i == 'p' || i == 'P' || (!isHex && (i == 'E' || i == 'e')))
return true; return true;
if (isHex && isxdigit(i) && i != '0')
return false;
} }
return true; return true;
} }

View File

@ -946,6 +946,8 @@ private:
ASSERT_EQUALS(true, MathLib::isNullValue("0x0")); ASSERT_EQUALS(true, MathLib::isNullValue("0x0"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0x0")); ASSERT_EQUALS(true, MathLib::isNullValue("+0x0"));
ASSERT_EQUALS(true, MathLib::isNullValue("-0x0")); ASSERT_EQUALS(true, MathLib::isNullValue("-0x0"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0X0"));
ASSERT_EQUALS(true, MathLib::isNullValue("-0X0"));
// unsigned integer zero value // unsigned integer zero value
ASSERT_EQUALS(true, MathLib::isNullValue("0U")); ASSERT_EQUALS(true, MathLib::isNullValue("0U"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0U")); ASSERT_EQUALS(true, MathLib::isNullValue("+0U"));
@ -1004,6 +1006,8 @@ private:
ASSERT_EQUALS(true, MathLib::isNullValue("0E0")); ASSERT_EQUALS(true, MathLib::isNullValue("0E0"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0E0")); ASSERT_EQUALS(true, MathLib::isNullValue("+0E0"));
ASSERT_EQUALS(true, MathLib::isNullValue("-0E0")); ASSERT_EQUALS(true, MathLib::isNullValue("-0E0"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0e0"));
ASSERT_EQUALS(true, MathLib::isNullValue("-0e0"));
// integer scientific notation // integer scientific notation
ASSERT_EQUALS(true, MathLib::isNullValue("0E1")); ASSERT_EQUALS(true, MathLib::isNullValue("0E1"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0E1")); ASSERT_EQUALS(true, MathLib::isNullValue("+0E1"));
@ -1066,6 +1070,7 @@ private:
ASSERT_EQUALS(true, MathLib::isNullValue("-0.f")); ASSERT_EQUALS(true, MathLib::isNullValue("-0.f"));
// floating point scientific notation (suffix f) // floating point scientific notation (suffix f)
ASSERT_EQUALS(true, MathLib::isNullValue("0.0f")); ASSERT_EQUALS(true, MathLib::isNullValue("0.0f"));
ASSERT_EQUALS(true, MathLib::isNullValue("0.0F"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0.0f")); ASSERT_EQUALS(true, MathLib::isNullValue("+0.0f"));
ASSERT_EQUALS(true, MathLib::isNullValue("-0.0f")); ASSERT_EQUALS(true, MathLib::isNullValue("-0.0f"));
// floating point scientific notation (suffix f) // floating point scientific notation (suffix f)
@ -1080,6 +1085,12 @@ private:
ASSERT_EQUALS(true, MathLib::isNullValue("00.00E+1f")); ASSERT_EQUALS(true, MathLib::isNullValue("00.00E+1f"));
ASSERT_EQUALS(true, MathLib::isNullValue("+00.00E+1f")); ASSERT_EQUALS(true, MathLib::isNullValue("+00.00E+1f"));
ASSERT_EQUALS(true, MathLib::isNullValue("-00.00E+1f")); ASSERT_EQUALS(true, MathLib::isNullValue("-00.00E+1f"));
// hex float
ASSERT_EQUALS(true, MathLib::isNullValue("0x0p3"));
ASSERT_EQUALS(true, MathLib::isNullValue("0X0P3"));
ASSERT_EQUALS(true, MathLib::isNullValue("0X0p-3"));
ASSERT_EQUALS(true, MathLib::isNullValue("-0x0p3"));
ASSERT_EQUALS(true, MathLib::isNullValue("+0x0p3"));
// binary numbers // binary numbers
ASSERT_EQUALS(true, MathLib::isNullValue("0b00")); ASSERT_EQUALS(true, MathLib::isNullValue("0b00"));
@ -1093,6 +1104,18 @@ private:
ASSERT_EQUALS(false, MathLib::isNullValue("0.1")); ASSERT_EQUALS(false, MathLib::isNullValue("0.1"));
ASSERT_EQUALS(false, MathLib::isNullValue("1.0")); ASSERT_EQUALS(false, MathLib::isNullValue("1.0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0.01")); ASSERT_EQUALS(false, MathLib::isNullValue("0.01"));
ASSERT_EQUALS(false, MathLib::isNullValue("0xF"));
ASSERT_EQUALS(false, MathLib::isNullValue("0XFF"));
ASSERT_EQUALS(false, MathLib::isNullValue("0b01"));
ASSERT_EQUALS(false, MathLib::isNullValue("0B01"));
ASSERT_EQUALS(false, MathLib::isNullValue("0x1p0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0x1P0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0xap0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0xbp0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0xcp0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0xdp0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0xep0"));
ASSERT_EQUALS(false, MathLib::isNullValue("0xfp0"));
ASSERT_EQUALS(false, MathLib::isNullValue("-00.01e-12")); ASSERT_EQUALS(false, MathLib::isNullValue("-00.01e-12"));
ASSERT_EQUALS(false, MathLib::isNullValue("-00.01e+12")); ASSERT_EQUALS(false, MathLib::isNullValue("-00.01e+12"));
ASSERT_EQUALS(false, MathLib::isNullValue("")); ASSERT_EQUALS(false, MathLib::isNullValue(""));