Fixed #5571 (Clean up MathLib::isInt())

This commit is contained in:
Alexander Mai 2014-03-16 11:55:44 +01:00 committed by Daniel Marjamäki
parent 4ab04ef255
commit cba1879fee
2 changed files with 21 additions and 39 deletions

View File

@ -410,60 +410,31 @@ bool MathLib::isInt(const std::string & s)
if (s.find_last_of(charsToIndicateAFloat) != std::string::npos) if (s.find_last_of(charsToIndicateAFloat) != std::string::npos)
return false; return false;
// prechecking has nothing found,...
// gather information
enum Representation {
eOctal, // starts with 0
eHex, // starts with 0x
eDefault // Numbers with a (possible) trailing u or U or l or L for unsigned or long datatypes
};
// create an instance
Representation Mode = eDefault;
// remember position // remember position
unsigned long n = 0; unsigned long n = 0;
// eat up whitespace // eat up whitespace
while (std::isspace(s[n])) ++n; while (std::isspace(s[n])) ++n;
// determine type // determine type
if (isHex(s)) { if (isHex(s) || isOct(s)) {
Mode = eHex; return true;
} else if (isOct(s)) {
Mode = eOctal;
} }
// check sign // check sign
if (s[n] == '-' || s[n] == '+') ++n; if (s[n] == '-' || s[n] == '+') ++n;
if (Mode == eHex) { // starts with digit
++n; // 0 bool bStartsWithDigit=false;
++n; // x while (std::isdigit(s[n])) {
while (std::isxdigit(s[n])) bStartsWithDigit=true;
++n; ++n;
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
} }
// check octal notation
else if (Mode == eOctal) {
++n; // 0
while (isOctalDigit(s[n]))
++n;
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long) while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
} else if (Mode == eDefault) {
// starts with digit
bool bStartsWithDigit=false;
while (std::isdigit(s[n])) {
bStartsWithDigit=true;
++n;
};
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long) if (!bStartsWithDigit)
return false;
if (!bStartsWithDigit)
return false;
}
// eat up whitespace // eat up whitespace
while (std::isspace(s[n])) while (std::isspace(s[n]))
++n; ++n;

View File

@ -317,6 +317,12 @@ private:
ASSERT_EQUALS(false, MathLib::isInt("E2")); ASSERT_EQUALS(false, MathLib::isInt("E2"));
ASSERT_EQUALS(false, MathLib::isInt(".e2")); ASSERT_EQUALS(false, MathLib::isInt(".e2"));
ASSERT_EQUALS(false, MathLib::isInt(".E2")); ASSERT_EQUALS(false, MathLib::isInt(".E2"));
ASSERT_EQUALS(false, MathLib::isInt("0x"));
ASSERT_EQUALS(false, MathLib::isInt("0xu"));
ASSERT_EQUALS(false, MathLib::isInt("0xl"));
ASSERT_EQUALS(false, MathLib::isInt("0xul"));
// test empty string
ASSERT_EQUALS(false, MathLib::isInt(""));
} }
void isbin() { void isbin() {
@ -371,6 +377,8 @@ private:
ASSERT_EQUALS(false, MathLib::isNegative("+1.0")); ASSERT_EQUALS(false, MathLib::isNegative("+1.0"));
ASSERT_EQUALS(false, MathLib::isNegative("+1.0E+2")); ASSERT_EQUALS(false, MathLib::isNegative("+1.0E+2"));
ASSERT_EQUALS(false, MathLib::isNegative("+1.0E-2")); ASSERT_EQUALS(false, MathLib::isNegative("+1.0E-2"));
// test empty string
ASSERT_EQUALS(false, MathLib::isNegative(""));
} }
void isoct() { void isoct() {
@ -505,6 +513,9 @@ private:
ASSERT_EQUALS(true , MathLib::isPositive("+1.0")); ASSERT_EQUALS(true , MathLib::isPositive("+1.0"));
ASSERT_EQUALS(true , MathLib::isPositive("+1.0E+2")); ASSERT_EQUALS(true , MathLib::isPositive("+1.0E+2"));
ASSERT_EQUALS(true , MathLib::isPositive("+1.0E-2")); ASSERT_EQUALS(true , MathLib::isPositive("+1.0E-2"));
// test empty string
ASSERT_EQUALS(true, MathLib::isPositive("")); // because it has opposite result to MathLib::isNegative
} }
void isfloat() const { void isfloat() const {