Skip to content

Commit 95f4bb3

Browse files
committed
Implemented support for binary numbers (cppcheck-opensource#4113)
1 parent 5940d77 commit 95f4bb3

4 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎lib/mathlib.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ MathLib::bigint MathLib::toLongNumber(const std::string &str)
4545
return ret;
4646
}
4747

48+
// binary numbers:
49+
if (isBin(str)) {
50+
bigint ret = 0;
51+
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
52+
ret <<= 1;
53+
if (str[i] == '1')
54+
ret |= 1;
55+
}
56+
if (str[0] == '-')
57+
ret = -ret;
58+
return ret;
59+
}
60+
4861
if (str.find_first_of("eE") != std::string::npos)
4962
return static_cast<bigint>(std::atof(str.c_str()));
5063

@@ -100,6 +113,12 @@ bool MathLib::isHex(const std::string& str)
100113
return(str.compare(sign?1:0, 2, "0x") == 0 || str.compare(sign?1:0, 2, "0X") == 0);
101114
}
102115

116+
bool MathLib::isBin(const std::string& str)
117+
{
118+
bool sign = str[0]=='-' || str[0]=='+';
119+
return(str.compare(sign?1:0, 2, "0b") == 0 && str.find_first_not_of("10b", 1) == std::string::npos);
120+
}
121+
103122
bool MathLib::isInt(const std::string & s)
104123
{
105124
// perform prechecks:

‎lib/mathlib.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class CPPCHECKLIB MathLib {
5151
static bool isNegative(const std::string &str);
5252
static bool isHex(const std::string& str);
5353
static bool isOct(const std::string& str);
54+
static bool isBin(const std::string& str);
5455

5556
static std::string add(const std::string & first, const std::string & second);
5657
static std::string subtract(const std::string & first, const std::string & second);

‎lib/tokenlist.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void TokenList::addtoken(const char str[], const unsigned int lineno, const unsi
9090

9191
// Replace hexadecimal value with decimal
9292
std::ostringstream str2;
93-
if (MathLib::isHex(str) || MathLib::isOct(str)) {
93+
if (MathLib::isHex(str) || MathLib::isOct(str) || MathLib::isBin(str)) {
9494
str2 << MathLib::toLongNumber(str);
9595
} else if (strncmp(str, "_Bool", 5) == 0) {
9696
str2 << "bool";

‎test/testmathlib.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ class TestMathLib : public TestFixture {
164164
ASSERT_EQUALS(125 , MathLib::toLongNumber("+0175"));
165165
ASSERT_EQUALS(-125 , MathLib::toLongNumber("-0175"));
166166

167+
// from binary
168+
ASSERT_EQUALS(0 , MathLib::toLongNumber("0b0"));
169+
ASSERT_EQUALS(1 , MathLib::toLongNumber("0b1"));
170+
ASSERT_EQUALS(1 , MathLib::toLongNumber("+0b1"));
171+
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-0b1"));
172+
ASSERT_EQUALS(215 , MathLib::toLongNumber("0b11010111"));
173+
ASSERT_EQUALS(-215 , MathLib::toLongNumber("-0b11010111"));
174+
167175
// from base 10
168176
ASSERT_EQUALS(10 , MathLib::toLongNumber("10"));
169177
ASSERT_EQUALS(10 , MathLib::toLongNumber("10."));

0 commit comments

Comments
 (0)