Fixed #5571 (Clean up MathLib::isInt())
This commit is contained in:
parent
4ab04ef255
commit
cba1879fee
|
@ -410,60 +410,31 @@ bool MathLib::isInt(const std::string & s)
|
|||
if (s.find_last_of(charsToIndicateAFloat) != std::string::npos)
|
||||
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
|
||||
unsigned long n = 0;
|
||||
// eat up whitespace
|
||||
while (std::isspace(s[n])) ++n;
|
||||
|
||||
// determine type
|
||||
if (isHex(s)) {
|
||||
Mode = eHex;
|
||||
} else if (isOct(s)) {
|
||||
Mode = eOctal;
|
||||
if (isHex(s) || isOct(s)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// check sign
|
||||
if (s[n] == '-' || s[n] == '+') ++n;
|
||||
|
||||
if (Mode == eHex) {
|
||||
++n; // 0
|
||||
++n; // x
|
||||
while (std::isxdigit(s[n]))
|
||||
++n;
|
||||
|
||||
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
|
||||
// starts with digit
|
||||
bool bStartsWithDigit=false;
|
||||
while (std::isdigit(s[n])) {
|
||||
bStartsWithDigit=true;
|
||||
++n;
|
||||
}
|
||||
// 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)
|
||||
} 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)
|
||||
|
||||
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
|
||||
while (std::isspace(s[n]))
|
||||
++n;
|
||||
|
|
|
@ -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("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() {
|
||||
|
@ -371,6 +377,8 @@ private:
|
|||
ASSERT_EQUALS(false, MathLib::isNegative("+1.0"));
|
||||
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() {
|
||||
|
@ -505,6 +513,9 @@ private:
|
|||
ASSERT_EQUALS(true , MathLib::isPositive("+1.0"));
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue