Refactorizations in MathLib.
Fixed recently introduced bug on floating point numbers with multiple preceding zeros (for example 004.123)
This commit is contained in:
parent
bf776044d8
commit
485e836535
|
@ -29,10 +29,8 @@
|
||||||
|
|
||||||
MathLib::bigint MathLib::toLongNumber(const std::string &str)
|
MathLib::bigint MathLib::toLongNumber(const std::string &str)
|
||||||
{
|
{
|
||||||
bool sign = str[0]=='-'||str[0]=='+';
|
|
||||||
// hexadecimal numbers:
|
// hexadecimal numbers:
|
||||||
if (str.compare(sign?1:0, 2, "0x") == 0
|
if (isHex(str)) {
|
||||||
|| str.compare(sign?1:0, 2, "0X") == 0) {
|
|
||||||
bigint ret = 0;
|
bigint ret = 0;
|
||||||
std::istringstream istr(str);
|
std::istringstream istr(str);
|
||||||
istr >> std::hex >> ret;
|
istr >> std::hex >> ret;
|
||||||
|
@ -40,7 +38,7 @@ MathLib::bigint MathLib::toLongNumber(const std::string &str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// octal numbers:
|
// octal numbers:
|
||||||
if (str[sign?1:0] == '0') {
|
if (isOct(str)) {
|
||||||
bigint ret = 0;
|
bigint ret = 0;
|
||||||
std::istringstream istr(str);
|
std::istringstream istr(str);
|
||||||
istr >> std::oct >> ret;
|
istr >> std::oct >> ret;
|
||||||
|
@ -58,9 +56,8 @@ MathLib::bigint MathLib::toLongNumber(const std::string &str)
|
||||||
|
|
||||||
double MathLib::toDoubleNumber(const std::string &str)
|
double MathLib::toDoubleNumber(const std::string &str)
|
||||||
{
|
{
|
||||||
if (str.compare(0, 2, "0x") == 0) {
|
if (isHex(str))
|
||||||
return std::strtoul(str.c_str(), '\0', 16);
|
return static_cast<double>(toLongNumber(str));
|
||||||
}
|
|
||||||
// nullcheck
|
// nullcheck
|
||||||
else if (isNullValue(str))
|
else if (isNullValue(str))
|
||||||
return 0.0;
|
return 0.0;
|
||||||
|
@ -77,24 +74,30 @@ bool MathLib::isFloat(const std::string &s)
|
||||||
if (s.find("." , 0) != std::string::npos)
|
if (s.find("." , 0) != std::string::npos)
|
||||||
return true;
|
return true;
|
||||||
// scientific notation
|
// scientific notation
|
||||||
else if (s.find("E-", 0) != std::string::npos
|
return(s.find("E-", 0) != std::string::npos
|
||||||
|| s.find("e-", 0) != std::string::npos)
|
|| s.find("e-", 0) != std::string::npos);
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MathLib::isNegative(const std::string &s)
|
bool MathLib::isNegative(const std::string &s)
|
||||||
{
|
{
|
||||||
// remember position
|
// remember position
|
||||||
unsigned long n = 0;
|
std::string::size_type n = 0;
|
||||||
// eat up whitespace
|
// eat up whitespace
|
||||||
while (std::isspace(s[n])) ++n;
|
while (std::isspace(s[n])) ++n;
|
||||||
// every negative number has a negative sign
|
// every negative number has a negative sign
|
||||||
if (s[n] == '-')
|
return(s[n] == '-');
|
||||||
return true;
|
}
|
||||||
|
|
||||||
return false;
|
bool MathLib::isOct(const std::string& str)
|
||||||
|
{
|
||||||
|
bool sign = str[0]=='-' || str[0]=='+';
|
||||||
|
return(str[sign?1:0] == '0' && (str.size() == 1 || isOctalDigit(str[sign?2:1])) && !isFloat(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MathLib::isHex(const std::string& str)
|
||||||
|
{
|
||||||
|
bool sign = str[0]=='-' || str[0]=='+';
|
||||||
|
return(str.compare(sign?1:0, 2, "0x") == 0 || str.compare(sign?1:0, 2, "0X") == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MathLib::isInt(const std::string & s)
|
bool MathLib::isInt(const std::string & s)
|
||||||
|
@ -127,9 +130,9 @@ bool MathLib::isInt(const std::string & s)
|
||||||
// determine type
|
// determine type
|
||||||
if (s.find("E", 0) != std::string::npos) {
|
if (s.find("E", 0) != std::string::npos) {
|
||||||
Mode = eScientific;
|
Mode = eScientific;
|
||||||
} else if (s.find("0x", n, 2) != std::string::npos) {
|
} else if (isHex(s)) {
|
||||||
Mode = eHex;
|
Mode = eHex;
|
||||||
} else if (s.length() > 1 && s[0] == '0' && std::isdigit(s[1])) {
|
} else if (isOct(s)) {
|
||||||
Mode = eOctal;
|
Mode = eOctal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,11 +161,16 @@ bool MathLib::isInt(const std::string & s)
|
||||||
++n; // x
|
++n; // x
|
||||||
while (std::isxdigit(s[n]))
|
while (std::isxdigit(s[n]))
|
||||||
++n;
|
++n;
|
||||||
|
|
||||||
|
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
|
||||||
}
|
}
|
||||||
// check octal notation
|
// check octal notation
|
||||||
else if (Mode == eOctal) {
|
else if (Mode == eOctal) {
|
||||||
|
++n; // 0
|
||||||
while (isOctalDigit(s[n]))
|
while (isOctalDigit(s[n]))
|
||||||
++n;
|
++n;
|
||||||
|
|
||||||
|
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
|
||||||
} else if (Mode == eDefault) {
|
} else if (Mode == eDefault) {
|
||||||
// starts with digit
|
// starts with digit
|
||||||
bool bStartsWithDigit=false;
|
bool bStartsWithDigit=false;
|
||||||
|
@ -170,10 +178,10 @@ bool MathLib::isInt(const std::string & s)
|
||||||
bStartsWithDigit=true;
|
bStartsWithDigit=true;
|
||||||
++n;
|
++n;
|
||||||
};
|
};
|
||||||
// unsigned or long
|
|
||||||
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n;
|
|
||||||
|
|
||||||
if (bStartsWithDigit==false)
|
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
|
||||||
|
|
||||||
|
if (!bStartsWithDigit)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// eat up whitespace
|
// eat up whitespace
|
||||||
|
@ -182,10 +190,7 @@ bool MathLib::isInt(const std::string & s)
|
||||||
|
|
||||||
// if everything goes good, we are at the end of the string and no digits/character
|
// if everything goes good, we are at the end of the string and no digits/character
|
||||||
// is here --> return true, but if something was found eg. 12E+12AA return false
|
// is here --> return true, but if something was found eg. 12E+12AA return false
|
||||||
if (s[n])
|
return(n >= s.length());
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string MathLib::add(const std::string & first, const std::string & second)
|
std::string MathLib::add(const std::string & first, const std::string & second)
|
||||||
|
@ -222,30 +227,24 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
|
||||||
|
|
||||||
std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
|
std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
|
||||||
{
|
{
|
||||||
std::string result("0");
|
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case '+':
|
case '+':
|
||||||
result = MathLib::add(first, second);
|
return MathLib::add(first, second);
|
||||||
break;
|
|
||||||
|
|
||||||
case '-':
|
case '-':
|
||||||
result = MathLib::subtract(first, second);
|
return MathLib::subtract(first, second);
|
||||||
break;
|
|
||||||
|
|
||||||
case '*':
|
case '*':
|
||||||
result = MathLib::multiply(first, second);
|
return MathLib::multiply(first, second);
|
||||||
break;
|
|
||||||
|
|
||||||
case '/':
|
case '/':
|
||||||
result = MathLib::divide(first, second);
|
return MathLib::divide(first, second);
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw InternalError(0, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
|
throw InternalError(0, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return "0";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string MathLib::sin(const std::string &tok)
|
std::string MathLib::sin(const std::string &tok)
|
||||||
|
@ -304,23 +303,15 @@ bool MathLib::isLessEqual(const std::string &first, const std::string &second)
|
||||||
|
|
||||||
bool MathLib::isNullValue(const std::string &str)
|
bool MathLib::isNullValue(const std::string &str)
|
||||||
{
|
{
|
||||||
return (str == "-0" || str == "-0.0"
|
return (str == "-0" || str == "0" || str == "+0"
|
||||||
|| str == "0"
|
|| str == "-0.0" || str == "0.0" || str == "+0.0"
|
||||||
|| str == "-0." || str == "-0E-00"
|
|| str == "-0." || str == "+0."
|
||||||
|| str == "-0E+00" || str == "+0E+00"
|
|| str == "-0E-00" || str == "-0E+00" || str == "+0E+00" || str == "+0E-00"
|
||||||
|| str == "+0E-00" || str == "+0"
|
|| str == "-0e-00" || str == "-0e+00" || str == "+0e+00" || str == "+0e-00"
|
||||||
|| str == "+0.0" || str == "+0."
|
|
||||||
|| str == "0.0"
|
|
||||||
|| str == "+0e+00" || str == "-0e+00"
|
|
||||||
|| str == "+0e-00" || str == "-0e-00"
|
|
||||||
|| str == "-0E-0");
|
|| str == "-0E-0");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MathLib::isOctalDigit(char c)
|
bool MathLib::isOctalDigit(char c)
|
||||||
{
|
{
|
||||||
if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
|
return(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7');
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,8 +28,6 @@
|
||||||
|
|
||||||
/** @brief simple math functions that uses operands stored in std::string. useful when performing math on tokens. */
|
/** @brief simple math functions that uses operands stored in std::string. useful when performing math on tokens. */
|
||||||
|
|
||||||
class Tokenizer;
|
|
||||||
|
|
||||||
class MathLib {
|
class MathLib {
|
||||||
public:
|
public:
|
||||||
typedef long long bigint;
|
typedef long long bigint;
|
||||||
|
@ -41,11 +39,7 @@ public:
|
||||||
static std::string toString(const T &d) {
|
static std::string toString(const T &d) {
|
||||||
std::ostringstream result;
|
std::ostringstream result;
|
||||||
result << d;
|
result << d;
|
||||||
std::string strResult(result.str());
|
if (isNullValue(result.str()))
|
||||||
if (strResult == "-0"
|
|
||||||
|| strResult == "+0"
|
|
||||||
|| strResult == "-0."
|
|
||||||
|| strResult == "+0.")
|
|
||||||
return std::string("0");
|
return std::string("0");
|
||||||
return result.str();
|
return result.str();
|
||||||
}
|
}
|
||||||
|
@ -53,6 +47,8 @@ public:
|
||||||
static bool isInt(const std::string & str);
|
static bool isInt(const std::string & str);
|
||||||
static bool isFloat(const std::string &str);
|
static bool isFloat(const std::string &str);
|
||||||
static bool isNegative(const std::string &str);
|
static bool isNegative(const std::string &str);
|
||||||
|
static bool isHex(const std::string& str);
|
||||||
|
static bool isOct(const std::string& str);
|
||||||
|
|
||||||
static std::string add(const std::string & first, const std::string & second);
|
static std::string add(const std::string & first, const std::string & second);
|
||||||
static std::string subtract(const std::string & first, const std::string & second);
|
static std::string subtract(const std::string & first, const std::string & second);
|
||||||
|
|
|
@ -117,7 +117,7 @@ void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsi
|
||||||
|
|
||||||
// Replace hexadecimal value with decimal
|
// Replace hexadecimal value with decimal
|
||||||
std::ostringstream str2;
|
std::ostringstream str2;
|
||||||
if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0 || (str[0] == '0' && std::isdigit(str[1]))) {
|
if (MathLib::isHex(str) || MathLib::isOct(str)) {
|
||||||
str2 << MathLib::toLongNumber(str);
|
str2 << MathLib::toLongNumber(str);
|
||||||
} else if (strncmp(str, "_Bool", 5) == 0) {
|
} else if (strncmp(str, "_Bool", 5) == 0) {
|
||||||
str2 << "bool";
|
str2 << "bool";
|
||||||
|
@ -374,9 +374,9 @@ void Tokenizer::createTokens(std::istream &code)
|
||||||
} else if (strchr("+-", ch) &&
|
} else if (strchr("+-", ch) &&
|
||||||
CurrentToken.length() > 0 &&
|
CurrentToken.length() > 0 &&
|
||||||
std::isdigit(CurrentToken[0]) &&
|
std::isdigit(CurrentToken[0]) &&
|
||||||
CurrentToken.compare(0,2,"0x") != 0 &&
|
|
||||||
(CurrentToken[CurrentToken.length()-1] == 'e' ||
|
(CurrentToken[CurrentToken.length()-1] == 'e' ||
|
||||||
CurrentToken[CurrentToken.length()-1] == 'E')) {
|
CurrentToken[CurrentToken.length()-1] == 'E') &&
|
||||||
|
!MathLib::isHex(CurrentToken)) {
|
||||||
// Don't separate doubles "4.2e+10"
|
// Don't separate doubles "4.2e+10"
|
||||||
} else if (CurrentToken.empty() && ch == '.' && std::isdigit(code.peek())) {
|
} else if (CurrentToken.empty() && ch == '.' && std::isdigit(code.peek())) {
|
||||||
// tokenize .125 into 0.125
|
// tokenize .125 into 0.125
|
||||||
|
|
|
@ -587,6 +587,7 @@ private:
|
||||||
// Ticket #2429: 0.125
|
// Ticket #2429: 0.125
|
||||||
void tokenize15() {
|
void tokenize15() {
|
||||||
ASSERT_EQUALS("0.125", tokenizeAndStringify(".125"));
|
ASSERT_EQUALS("0.125", tokenizeAndStringify(".125"));
|
||||||
|
ASSERT_EQUALS("005.125", tokenizeAndStringify("005.125")); // Don't confuse with octal values
|
||||||
}
|
}
|
||||||
|
|
||||||
// #2612 - segfault for "<><<"
|
// #2612 - segfault for "<><<"
|
||||||
|
@ -683,7 +684,7 @@ private:
|
||||||
|
|
||||||
|
|
||||||
void longtok() {
|
void longtok() {
|
||||||
std::string filedata(10000, 'a');
|
const std::string filedata(10000, 'a');
|
||||||
|
|
||||||
errout.str("");
|
errout.str("");
|
||||||
|
|
||||||
|
@ -695,7 +696,7 @@ private:
|
||||||
tokenizer.tokenize(istr, "test.cpp");
|
tokenizer.tokenize(istr, "test.cpp");
|
||||||
|
|
||||||
// Expected result..
|
// Expected result..
|
||||||
ASSERT_EQUALS(std::string(10000, 'a'), tokenizer.tokens()->str());
|
ASSERT_EQUALS(filedata, tokenizer.tokens()->str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue