Skip to content

Commit cba1879

Browse files
amai2012danmar
authored andcommitted
Fixed cppcheck-opensource#5571 (Clean up MathLib::isInt())
1 parent 4ab04ef commit cba1879

2 files changed

Lines changed: 24 additions & 42 deletions

File tree

lib/mathlib.cpp

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -410,60 +410,31 @@ bool MathLib::isInt(const std::string & s)
410410
if (s.find_last_of(charsToIndicateAFloat) != std::string::npos)
411411
return false;
412412

413-
// prechecking has nothing found,...
414-
// gather information
415-
enum Representation {
416-
eOctal, // starts with 0
417-
eHex, // starts with 0x
418-
eDefault // Numbers with a (possible) trailing u or U or l or L for unsigned or long datatypes
419-
};
420-
// create an instance
421-
Representation Mode = eDefault;
422-
423-
424413
// remember position
425414
unsigned long n = 0;
426415
// eat up whitespace
427416
while (std::isspace(s[n])) ++n;
428417

429418
// determine type
430-
if (isHex(s)) {
431-
Mode = eHex;
432-
} else if (isOct(s)) {
433-
Mode = eOctal;
419+
if (isHex(s) || isOct(s)) {
420+
return true;
434421
}
435422

436423
// check sign
437424
if (s[n] == '-' || s[n] == '+') ++n;
438425

439-
if (Mode == eHex) {
440-
++n; // 0
441-
++n; // x
442-
while (std::isxdigit(s[n]))
443-
++n;
444-
445-
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
446-
}
447-
// check octal notation
448-
else if (Mode == eOctal) {
449-
++n; // 0
450-
while (isOctalDigit(s[n]))
451-
++n;
452-
453-
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
454-
} else if (Mode == eDefault) {
455-
// starts with digit
456-
bool bStartsWithDigit=false;
457-
while (std::isdigit(s[n])) {
458-
bStartsWithDigit=true;
459-
++n;
460-
};
461-
462-
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
463-
464-
if (!bStartsWithDigit)
465-
return false;
426+
// starts with digit
427+
bool bStartsWithDigit=false;
428+
while (std::isdigit(s[n])) {
429+
bStartsWithDigit=true;
430+
++n;
466431
}
432+
433+
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
434+
435+
if (!bStartsWithDigit)
436+
return false;
437+
467438
// eat up whitespace
468439
while (std::isspace(s[n]))
469440
++n;

test/testmathlib.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ class TestMathLib : public TestFixture {
317317
ASSERT_EQUALS(false, MathLib::isInt("E2"));
318318
ASSERT_EQUALS(false, MathLib::isInt(".e2"));
319319
ASSERT_EQUALS(false, MathLib::isInt(".E2"));
320+
ASSERT_EQUALS(false, MathLib::isInt("0x"));
321+
ASSERT_EQUALS(false, MathLib::isInt("0xu"));
322+
ASSERT_EQUALS(false, MathLib::isInt("0xl"));
323+
ASSERT_EQUALS(false, MathLib::isInt("0xul"));
324+
// test empty string
325+
ASSERT_EQUALS(false, MathLib::isInt(""));
320326
}
321327

322328
void isbin() {
@@ -371,6 +377,8 @@ class TestMathLib : public TestFixture {
371377
ASSERT_EQUALS(false, MathLib::isNegative("+1.0"));
372378
ASSERT_EQUALS(false, MathLib::isNegative("+1.0E+2"));
373379
ASSERT_EQUALS(false, MathLib::isNegative("+1.0E-2"));
380+
// test empty string
381+
ASSERT_EQUALS(false, MathLib::isNegative(""));
374382
}
375383

376384
void isoct() {
@@ -505,6 +513,9 @@ class TestMathLib : public TestFixture {
505513
ASSERT_EQUALS(true , MathLib::isPositive("+1.0"));
506514
ASSERT_EQUALS(true , MathLib::isPositive("+1.0E+2"));
507515
ASSERT_EQUALS(true , MathLib::isPositive("+1.0E-2"));
516+
517+
// test empty string
518+
ASSERT_EQUALS(true, MathLib::isPositive("")); // because it has opposite result to MathLib::isNegative
508519
}
509520

510521
void isfloat() const {

0 commit comments

Comments
 (0)