Skip to content

Commit 618c26f

Browse files
committed
Merge pull request cppcheck-opensource#260 from orbitcowboy/master
MathLib::isOct() fix missing detection of U and L suffix combinations an...
2 parents 3b4d024 + 0360840 commit 618c26f

2 files changed

Lines changed: 143 additions & 3 deletions

File tree

lib/mathlib.cpp

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,88 @@ bool MathLib::isPositive(const std::string &s)
202202
return !MathLib::isNegative(s);
203203
}
204204

205-
bool MathLib::isOct(const std::string& str)
205+
/*! \brief Does the string represent an octal number?
206+
* In case leading or trailing white space is provided, the function
207+
* returns false.
208+
* Additional information can be found here:
209+
* http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
210+
*
211+
* \param[in] s The string to check. In case the string is empty, the function returns false.
212+
* \return Return true in case a octal number is provided and false otherwise.
213+
**/
214+
bool MathLib::isOct(const std::string& s)
206215
{
207-
const bool sign = str[0]=='-' || str[0]=='+';
208-
return (str[sign?1:0] == '0' && (str.size() == 1 || isOctalDigit(str[sign?2:1])) && !isFloat(str));
216+
enum {START, PLUSMINUS, OCTAL_PREFIX, DIGITS, UNSIGNED_SUFFIX, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU} state = START;
217+
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
218+
switch (state) {
219+
case START:
220+
if (*it == '+' || *it == '-')
221+
state = PLUSMINUS;
222+
else if (*it == '0')
223+
state = OCTAL_PREFIX;
224+
else
225+
return false;
226+
break;
227+
case PLUSMINUS:
228+
if (*it == '0')
229+
state = OCTAL_PREFIX;
230+
else
231+
return false;
232+
break;
233+
234+
case OCTAL_PREFIX:
235+
if (isOctalDigit(*it))
236+
state = DIGITS;
237+
else
238+
return false;
239+
break;
240+
case DIGITS:
241+
if (isOctalDigit(*it))
242+
state = DIGITS;
243+
else if (*it == 'u' || *it == 'U')
244+
state = SUFFIX_U;
245+
else if (*it == 'l' || *it == 'L')
246+
state = SUFFIX_L;
247+
else
248+
return false;
249+
break;
250+
case SUFFIX_U:
251+
if (*it == 'l' || *it == 'L')
252+
state = SUFFIX_UL; // UL
253+
else
254+
return false;
255+
break;
256+
case SUFFIX_UL:
257+
if (*it == 'l' || *it == 'L')
258+
state = SUFFIX_ULL; // ULL
259+
else
260+
return false;
261+
break;
262+
case SUFFIX_L:
263+
if (*it == 'u' || *it == 'U')
264+
state = SUFFIX_LU; // LU
265+
else if (*it == 'l' || *it == 'L')
266+
state = SUFFIX_LL; // LL
267+
else
268+
return false;
269+
break;
270+
case SUFFIX_LU:
271+
return false;
272+
break;
273+
case SUFFIX_LL:
274+
if (*it == 'u' || *it == 'U')
275+
state = SUFFIX_LLU; // LLU
276+
else
277+
return false;
278+
break;
279+
default:
280+
return false;
281+
}
282+
}
283+
return (state == DIGITS)
284+
|| (state == SUFFIX_U) || (state == SUFFIX_L)
285+
|| (state == SUFFIX_UL) || (state == SUFFIX_LU) || (state == SUFFIX_LL)
286+
|| (state == SUFFIX_ULL) || (state == SUFFIX_LLU);
209287
}
210288

211289
bool MathLib::isHex(const std::string& str)

test/testmathlib.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TestMathLib : public TestFixture {
3434
TEST_CASE(convert);
3535
TEST_CASE(isint);
3636
TEST_CASE(isbin);
37+
TEST_CASE(isoct);
3738
TEST_CASE(isnegative);
3839
TEST_CASE(ispositive);
3940
TEST_CASE(isfloat);
@@ -371,6 +372,67 @@ class TestMathLib : public TestFixture {
371372
ASSERT_EQUALS(false, MathLib::isNegative("+1.0E-2"));
372373
}
373374

375+
void isoct() {
376+
// octal number format: [+|-]0[0-7][suffix]
377+
// positive testing
378+
ASSERT_EQUALS(true, MathLib::isOct("010"));
379+
ASSERT_EQUALS(true, MathLib::isOct("+010"));
380+
ASSERT_EQUALS(true, MathLib::isOct("-010"));
381+
ASSERT_EQUALS(true, MathLib::isOct("0175"));
382+
ASSERT_EQUALS(true, MathLib::isOct("+0175"));
383+
ASSERT_EQUALS(true, MathLib::isOct("-0175"));
384+
ASSERT_EQUALS(true, MathLib::isOct("00"));
385+
ASSERT_EQUALS(true, MathLib::isOct("02"));
386+
ASSERT_EQUALS(true, MathLib::isOct("+042"));
387+
ASSERT_EQUALS(true, MathLib::isOct("-042"));
388+
ASSERT_EQUALS(true, MathLib::isOct("+042U"));
389+
ASSERT_EQUALS(true, MathLib::isOct("-042U"));
390+
ASSERT_EQUALS(true, MathLib::isOct("+042L"));
391+
ASSERT_EQUALS(true, MathLib::isOct("-042L"));
392+
ASSERT_EQUALS(true, MathLib::isOct("+042LU"));
393+
ASSERT_EQUALS(true, MathLib::isOct("-042LU"));
394+
ASSERT_EQUALS(true, MathLib::isOct("+042UL"));
395+
ASSERT_EQUALS(true, MathLib::isOct("-042UL"));
396+
ASSERT_EQUALS(true, MathLib::isOct("+042ULL"));
397+
ASSERT_EQUALS(true, MathLib::isOct("-042ULL"));
398+
ASSERT_EQUALS(true, MathLib::isOct("+042LLU"));
399+
ASSERT_EQUALS(true, MathLib::isOct("-042LLU"));
400+
401+
// test empty string
402+
ASSERT_EQUALS(false, MathLib::isOct(""));
403+
404+
// negative testing
405+
ASSERT_EQUALS(false, MathLib::isOct("0"));
406+
ASSERT_EQUALS(false, MathLib::isOct("-0x175"));
407+
ASSERT_EQUALS(false, MathLib::isOct("-0_garbage_"));
408+
ASSERT_EQUALS(false, MathLib::isOct(" "));
409+
ASSERT_EQUALS(false, MathLib::isOct(" "));
410+
ASSERT_EQUALS(false, MathLib::isOct("02."));
411+
ASSERT_EQUALS(false, MathLib::isOct("02E2"));
412+
ASSERT_EQUALS(false, MathLib::isOct("+042x"));
413+
ASSERT_EQUALS(false, MathLib::isOct("-042x"));
414+
ASSERT_EQUALS(false, MathLib::isOct("+042Ux"));
415+
ASSERT_EQUALS(false, MathLib::isOct("-042Ux"));
416+
ASSERT_EQUALS(false, MathLib::isOct("+042Lx"));
417+
ASSERT_EQUALS(false, MathLib::isOct("-042Lx"));
418+
ASSERT_EQUALS(false, MathLib::isOct("+042ULx"));
419+
ASSERT_EQUALS(false, MathLib::isOct("-042ULx"));
420+
ASSERT_EQUALS(false, MathLib::isOct("+042LLx"));
421+
ASSERT_EQUALS(false, MathLib::isOct("-042LLx"));
422+
ASSERT_EQUALS(false, MathLib::isOct("+042ULLx"));
423+
ASSERT_EQUALS(false, MathLib::isOct("-042ULLx"));
424+
ASSERT_EQUALS(false, MathLib::isOct("+042LLUx"));
425+
ASSERT_EQUALS(false, MathLib::isOct("-042LLUx"));
426+
ASSERT_EQUALS(false, MathLib::isOct("+042LUL"));
427+
ASSERT_EQUALS(false, MathLib::isOct("-042LUL"));
428+
// white space in front
429+
ASSERT_EQUALS(false, MathLib::isOct(" -042ULL"));
430+
// trailing white space
431+
ASSERT_EQUALS(false, MathLib::isOct("-042ULL "));
432+
// front and trailing white space
433+
ASSERT_EQUALS(false, MathLib::isOct(" -042ULL "));
434+
}
435+
374436
void ispositive() const {
375437
ASSERT_EQUALS(false, MathLib::isPositive("-1"));
376438
ASSERT_EQUALS(false, MathLib::isPositive("-1."));

0 commit comments

Comments
 (0)