2009-05-30 07:48:12 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-01-03 12:14:58 +01:00
|
|
|
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
|
2009-06-02 22:32:58 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-02 22:32:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "mathlib.h"
|
2012-01-08 21:19:44 +01:00
|
|
|
#include "errorlogger.h"
|
2009-06-02 22:32:58 +02:00
|
|
|
|
|
|
|
#include <cmath>
|
2009-08-17 21:24:17 +02:00
|
|
|
#include <cctype>
|
2013-05-09 18:50:24 +02:00
|
|
|
#include <limits>
|
2009-06-02 22:32:58 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
MathLib::biguint MathLib::toULongNumber(const std::string & str)
|
|
|
|
{
|
|
|
|
// hexadecimal numbers:
|
|
|
|
if (isHex(str)) {
|
|
|
|
if (str[0] == '-') {
|
2014-04-14 20:30:38 +02:00
|
|
|
biguint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
2014-04-15 06:32:25 +02:00
|
|
|
istr >> std::hex >> ret;
|
2014-04-14 20:30:38 +02:00
|
|
|
return ret;
|
2014-04-15 06:32:25 +02:00
|
|
|
} else {
|
|
|
|
unsigned long long ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> std::hex >> ret;
|
|
|
|
return (biguint)ret;
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
// octal numbers:
|
|
|
|
if (isOct(str)) {
|
2014-04-14 20:30:38 +02:00
|
|
|
biguint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
2014-04-15 06:32:25 +02:00
|
|
|
istr >> std::oct >> ret;
|
2014-04-14 20:30:38 +02:00
|
|
|
return ret;
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
// binary numbers:
|
|
|
|
if (isBin(str)) {
|
|
|
|
biguint ret = 0;
|
|
|
|
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
|
|
|
|
ret <<= 1;
|
|
|
|
if (str[i] == '1')
|
|
|
|
ret |= 1;
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
/* if (str[0] == '-')
|
|
|
|
ret = -ret; */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFloat(str)) {
|
2014-05-21 21:36:17 +02:00
|
|
|
// Things are going to be less precise now: the value can't b represented in the biguint type.
|
|
|
|
// Use min/max values as an approximation. See #5843
|
|
|
|
const double doubleval = std::atof(str.c_str());
|
|
|
|
if (doubleval > (double)std::numeric_limits<biguint>::max())
|
|
|
|
return std::numeric_limits<biguint>::max();
|
|
|
|
else
|
|
|
|
return static_cast<biguint>(doubleval);
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
biguint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> ret;
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
MathLib::bigint MathLib::toLongNumber(const std::string & str)
|
|
|
|
{
|
|
|
|
// hexadecimal numbers:
|
|
|
|
if (isHex(str)) {
|
|
|
|
if (str[0] == '-') {
|
2014-04-14 20:30:38 +02:00
|
|
|
bigint ret = 0;
|
2014-04-15 06:32:25 +02:00
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> std::hex >> ret;
|
2014-04-14 20:30:38 +02:00
|
|
|
return ret;
|
2014-04-15 06:32:25 +02:00
|
|
|
} else {
|
|
|
|
unsigned long long ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> std::hex >> ret;
|
|
|
|
return (bigint)ret;
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
// octal numbers:
|
|
|
|
if (isOct(str)) {
|
2014-04-14 20:30:38 +02:00
|
|
|
bigint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
2014-04-15 06:32:25 +02:00
|
|
|
istr >> std::oct >> ret;
|
2014-04-14 20:30:38 +02:00
|
|
|
return ret;
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// binary numbers:
|
|
|
|
if (isBin(str)) {
|
|
|
|
bigint ret = 0;
|
|
|
|
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
|
|
|
|
ret <<= 1;
|
|
|
|
if (str[i] == '1')
|
|
|
|
ret |= 1;
|
|
|
|
}
|
|
|
|
if (str[0] == '-')
|
|
|
|
ret = -ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFloat(str)) {
|
2014-05-21 21:36:17 +02:00
|
|
|
// Things are going to be less precise now: the value can't be represented in the bigint type.
|
|
|
|
// Use min/max values as an approximation. See #5843
|
|
|
|
const double doubleval = std::atof(str.c_str());
|
|
|
|
if (doubleval > (double)std::numeric_limits<bigint>::max())
|
|
|
|
return std::numeric_limits<bigint>::max();
|
|
|
|
else if (doubleval < (double)std::numeric_limits<bigint>::min())
|
|
|
|
return std::numeric_limits<bigint>::min();
|
|
|
|
else
|
|
|
|
return static_cast<bigint>(doubleval);
|
2014-04-15 06:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bigint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> ret;
|
|
|
|
return ret;
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
double MathLib::toDoubleNumber(const std::string &str)
|
|
|
|
{
|
2012-02-17 19:54:53 +01:00
|
|
|
if (isHex(str))
|
|
|
|
return static_cast<double>(toLongNumber(str));
|
2010-04-02 20:42:06 +02:00
|
|
|
// nullcheck
|
2010-04-02 22:41:54 +02:00
|
|
|
else if (isNullValue(str))
|
2010-04-02 20:42:06 +02:00
|
|
|
return 0.0;
|
2015-04-12 14:51:57 +02:00
|
|
|
else if (isFloat(str)) // Workaround libc++ bug at http://llvm.org/bugs/show_bug.cgi?id=17782
|
|
|
|
return std::strtod(str.c_str(), 0);
|
2010-04-02 20:42:06 +02:00
|
|
|
// otherwise, convert to double
|
2011-12-08 21:28:34 +01:00
|
|
|
std::istringstream istr(str);
|
2009-11-15 19:07:36 +01:00
|
|
|
double ret;
|
|
|
|
istr >> ret;
|
|
|
|
return ret;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2013-09-21 18:48:48 +02:00
|
|
|
template<> std::string MathLib::toString(double value)
|
2012-09-11 16:50:42 +02:00
|
|
|
{
|
|
|
|
std::ostringstream result;
|
|
|
|
result.precision(12);
|
|
|
|
result << value;
|
|
|
|
if (result.str() == "-0")
|
|
|
|
return "0.0";
|
|
|
|
if (result.str().find(".") == std::string::npos)
|
|
|
|
return result.str() + ".0";
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:19:38 +02:00
|
|
|
bool MathLib::isFloat(const std::string &s)
|
|
|
|
{
|
2014-02-24 18:52:31 +01:00
|
|
|
if (s.empty())
|
2014-02-20 16:56:49 +01:00
|
|
|
return false;
|
2014-04-30 21:01:47 +02:00
|
|
|
enum State {
|
2014-11-17 06:53:32 +01:00
|
|
|
START, BASE_PLUSMINUS, BASE_DIGITS1, LEADING_DECIMAL, TRAILING_DECIMAL, BASE_DIGITS2, E, MANTISSA_PLUSMINUS, MANTISSA_DIGITS, F, L
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2014-02-26 06:38:48 +01:00
|
|
|
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
|
2014-02-24 18:52:31 +01:00
|
|
|
switch (state) {
|
2014-04-30 21:01:47 +02:00
|
|
|
case START:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='+' || *it=='-')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=BASE_PLUSMINUS;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='.')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=LEADING_DECIMAL;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (std::isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state=BASE_DIGITS1;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case BASE_PLUSMINUS:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='.')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=LEADING_DECIMAL;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (std::isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state=BASE_DIGITS1;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='e' || *it=='E')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=E;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case LEADING_DECIMAL:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (std::isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state=BASE_DIGITS2;
|
2014-02-27 16:08:49 +01:00
|
|
|
else if (*it=='e' || *it=='E')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=E;
|
2014-02-27 16:08:49 +01:00
|
|
|
else
|
|
|
|
return false;
|
2014-02-24 18:52:31 +01:00
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case BASE_DIGITS1:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=E;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='.')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=TRAILING_DECIMAL;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (!std::isdigit(*it))
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case TRAILING_DECIMAL:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=E;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='f' || *it=='F')
|
|
|
|
state=F;
|
|
|
|
else if (*it=='l' || *it=='L')
|
|
|
|
state=L;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (std::isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state=BASE_DIGITS2;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case BASE_DIGITS2:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=E;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='f' || *it=='F')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
|
|
|
state=L;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (!std::isdigit(*it))
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case E:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='+' || *it=='-')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=MANTISSA_PLUSMINUS;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (std::isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state=MANTISSA_DIGITS;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case MANTISSA_PLUSMINUS:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (!std::isdigit(*it))
|
|
|
|
return false;
|
|
|
|
else
|
2014-04-30 21:01:47 +02:00
|
|
|
state=MANTISSA_DIGITS;
|
2014-02-24 18:52:31 +01:00
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case MANTISSA_DIGITS:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='f' || *it=='F')
|
2014-04-30 21:01:47 +02:00
|
|
|
state=F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
|
|
|
state=L;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (!std::isdigit(*it))
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case F:
|
2014-02-20 16:56:49 +01:00
|
|
|
return false;
|
2014-11-17 06:53:32 +01:00
|
|
|
case L:
|
|
|
|
return false;
|
2014-02-24 18:52:31 +01:00
|
|
|
}
|
2014-02-20 16:56:49 +01:00
|
|
|
}
|
2014-11-17 06:53:32 +01:00
|
|
|
return (state==BASE_DIGITS2 || state==MANTISSA_DIGITS || state==TRAILING_DECIMAL || state==F || state==L);
|
2010-04-02 02:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isNegative(const std::string &s)
|
|
|
|
{
|
|
|
|
// remember position
|
2012-02-17 19:54:53 +01:00
|
|
|
std::string::size_type n = 0;
|
2010-04-02 02:19:38 +02:00
|
|
|
// eat up whitespace
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isspace(s[n])) ++n;
|
2010-04-02 07:32:03 +02:00
|
|
|
// every negative number has a negative sign
|
2013-08-07 16:27:37 +02:00
|
|
|
return (s[n] == '-');
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
2010-04-02 07:32:03 +02:00
|
|
|
|
2013-09-29 15:11:09 +02:00
|
|
|
bool MathLib::isPositive(const std::string &s)
|
|
|
|
{
|
|
|
|
return !MathLib::isNegative(s);
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:14:14 +01:00
|
|
|
/*! \brief Does the string represent an octal number?
|
|
|
|
* In case leading or trailing white space is provided, the function
|
|
|
|
* returns false.
|
|
|
|
* Additional information can be found here:
|
|
|
|
* http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
|
|
|
|
*
|
|
|
|
* \param[in] s The string to check. In case the string is empty, the function returns false.
|
|
|
|
* \return Return true in case a octal number is provided and false otherwise.
|
|
|
|
**/
|
|
|
|
bool MathLib::isOct(const std::string& s)
|
2012-02-17 19:54:53 +01:00
|
|
|
{
|
2014-04-30 21:01:47 +02:00
|
|
|
enum Status {
|
2014-04-26 20:44:21 +02:00
|
|
|
START, PLUSMINUS, OCTAL_PREFIX, DIGITS
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2014-03-09 21:14:14 +01:00
|
|
|
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
|
|
|
|
switch (state) {
|
2014-04-30 21:01:47 +02:00
|
|
|
case START:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == '+' || *it == '-')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = PLUSMINUS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else if (*it == '0')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = OCTAL_PREFIX;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case PLUSMINUS:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == '0')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = OCTAL_PREFIX;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case OCTAL_PREFIX:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (isOctalDigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case DIGITS:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (isOctalDigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
2014-03-14 11:46:41 +01:00
|
|
|
return isValidSuffix(it,s.end());
|
2014-03-09 21:14:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 21:01:47 +02:00
|
|
|
return state == DIGITS;
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-11 20:02:04 +01:00
|
|
|
bool MathLib::isHex(const std::string& s)
|
2012-02-17 19:54:53 +01:00
|
|
|
{
|
2014-04-30 21:01:47 +02:00
|
|
|
enum Status {
|
2014-04-26 20:44:21 +02:00
|
|
|
START, PLUSMINUS, HEX_PREFIX, DIGIT, DIGITS
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2014-03-11 20:02:04 +01:00
|
|
|
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
|
|
|
|
switch (state) {
|
2014-04-30 21:01:47 +02:00
|
|
|
case START:
|
2014-03-11 20:02:04 +01:00
|
|
|
if (*it == '+' || *it == '-')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = PLUSMINUS;
|
2014-03-11 20:02:04 +01:00
|
|
|
else if (*it == '0')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = HEX_PREFIX;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case PLUSMINUS:
|
2014-03-11 20:02:04 +01:00
|
|
|
if (*it == '0')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = HEX_PREFIX;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case HEX_PREFIX:
|
2014-03-11 20:02:04 +01:00
|
|
|
if (*it == 'x' || *it == 'X')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGIT;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case DIGIT:
|
2014-03-14 09:33:20 +01:00
|
|
|
if (isxdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case DIGITS:
|
2014-03-14 09:33:20 +01:00
|
|
|
if (isxdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-14 11:46:41 +01:00
|
|
|
else
|
|
|
|
return isValidSuffix(it,s.end());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 21:01:47 +02:00
|
|
|
return state == DIGITS;
|
2014-03-14 11:46:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isValidSuffix(std::string::const_iterator it, std::string::const_iterator end)
|
|
|
|
{
|
2014-11-20 10:23:09 +01:00
|
|
|
enum {START, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU, SUFFIX_I, SUFFIX_I6, SUFFIX_I64} state = START;
|
2014-03-14 11:46:41 +01:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
switch (state) {
|
2014-04-30 21:01:47 +02:00
|
|
|
case START:
|
2014-03-14 11:46:41 +01:00
|
|
|
if (*it == 'u' || *it == 'U')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = SUFFIX_U;
|
2014-03-09 21:14:14 +01:00
|
|
|
else if (*it == 'l' || *it == 'L')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = SUFFIX_L;
|
2014-11-20 10:23:09 +01:00
|
|
|
else if (*it == 'i')
|
|
|
|
state = SUFFIX_I;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case SUFFIX_U:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == 'l' || *it == 'L')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = SUFFIX_UL; // UL
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case SUFFIX_UL:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == 'l' || *it == 'L')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = SUFFIX_ULL; // ULL
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case SUFFIX_L:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == 'u' || *it == 'U')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = SUFFIX_LU; // LU
|
2014-03-09 21:14:14 +01:00
|
|
|
else if (*it == 'l' || *it == 'L')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = SUFFIX_LL; // LL
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case SUFFIX_LU:
|
2014-03-09 21:14:14 +01:00
|
|
|
return false;
|
2014-04-30 21:01:47 +02:00
|
|
|
case SUFFIX_LL:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == 'u' || *it == 'U')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = SUFFIX_LLU; // LLU
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-11-20 10:23:09 +01:00
|
|
|
case SUFFIX_I:
|
|
|
|
if (*it == '6')
|
|
|
|
state = SUFFIX_I6;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SUFFIX_I6:
|
|
|
|
if (*it == '4')
|
|
|
|
state = SUFFIX_I64;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-03-09 21:14:14 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 10:23:09 +01:00
|
|
|
return ((state == SUFFIX_U) ||
|
|
|
|
(state == SUFFIX_L) ||
|
|
|
|
(state == SUFFIX_UL) ||
|
|
|
|
(state == SUFFIX_LU) ||
|
|
|
|
(state == SUFFIX_LL) ||
|
|
|
|
(state == SUFFIX_ULL) ||
|
|
|
|
(state == SUFFIX_LLU) ||
|
|
|
|
(state == SUFFIX_I64));
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-10 17:23:07 +01:00
|
|
|
/*! \brief Does the string represent a binary number?
|
|
|
|
* In case leading or trailing white space is provided, the function
|
|
|
|
* returns false.
|
|
|
|
* Additional information can be found here:
|
|
|
|
* http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
|
|
|
|
*
|
|
|
|
* \param[in] s The string to check. In case the string is empty, the function returns false.
|
|
|
|
* \return Return true in case a binary number is provided and false otherwise.
|
|
|
|
**/
|
|
|
|
bool MathLib::isBin(const std::string& s)
|
2012-09-10 21:13:32 +02:00
|
|
|
{
|
2014-04-30 21:01:47 +02:00
|
|
|
enum Status {
|
2014-04-26 20:44:21 +02:00
|
|
|
START, PLUSMINUS, GNU_BIN_PREFIX, DIGIT, DIGITS
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2014-03-10 17:23:07 +01:00
|
|
|
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
|
|
|
|
switch (state) {
|
2014-04-30 21:01:47 +02:00
|
|
|
case START:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '+' || *it == '-')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = PLUSMINUS;
|
2014-03-10 17:23:07 +01:00
|
|
|
else if (*it == '0')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = GNU_BIN_PREFIX;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case PLUSMINUS:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = GNU_BIN_PREFIX;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case GNU_BIN_PREFIX:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == 'b' || *it == 'B')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGIT;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case DIGIT:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0' || *it == '1')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case DIGITS:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0' || *it == '1')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
2014-03-14 11:46:41 +01:00
|
|
|
return isValidSuffix(it,s.end());
|
2014-03-10 17:23:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 21:01:47 +02:00
|
|
|
return state == DIGITS;
|
2012-09-10 21:13:32 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 22:15:25 +02:00
|
|
|
bool MathLib::isDec(const std::string & s)
|
2009-06-02 22:32:58 +02:00
|
|
|
{
|
2014-04-30 21:01:47 +02:00
|
|
|
enum Status {
|
2014-11-14 04:29:35 +01:00
|
|
|
START, PLUSMINUS, DIGIT
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2014-04-10 22:46:55 +02:00
|
|
|
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
|
|
|
|
switch (state) {
|
2014-04-30 21:01:47 +02:00
|
|
|
case START:
|
2014-04-10 22:46:55 +02:00
|
|
|
if (*it == '+' || *it == '-')
|
2014-04-30 21:01:47 +02:00
|
|
|
state = PLUSMINUS;
|
2014-04-10 22:46:55 +02:00
|
|
|
else if (isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGIT;
|
2014-04-10 22:46:55 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case PLUSMINUS:
|
2014-04-10 22:46:55 +02:00
|
|
|
if (isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGIT;
|
2014-04-10 22:46:55 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-30 21:01:47 +02:00
|
|
|
case DIGIT:
|
2014-04-10 22:46:55 +02:00
|
|
|
if (isdigit(*it))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGIT;
|
2014-04-10 22:46:55 +02:00
|
|
|
else
|
|
|
|
return isValidSuffix(it,s.end());
|
|
|
|
break;
|
|
|
|
}
|
2009-08-16 18:45:26 +02:00
|
|
|
}
|
2014-04-30 21:01:47 +02:00
|
|
|
return state == DIGIT;
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 22:15:25 +02:00
|
|
|
bool MathLib::isInt(const std::string & s)
|
|
|
|
{
|
2014-11-19 06:43:52 +01:00
|
|
|
return isDec(s) || isHex(s) || isOct(s) || isBin(s);
|
2014-04-11 22:15:25 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 19:01:56 +02:00
|
|
|
static std::string intsuffix(const std::string & first, const std::string & second)
|
|
|
|
{
|
|
|
|
std::string suffix1, suffix2;
|
|
|
|
for (std::size_t i = 1U; i < first.size(); ++i) {
|
|
|
|
char c = first[first.size() - i];
|
|
|
|
if (c == 'l' || c == 'u')
|
|
|
|
c = c - 'a' + 'A';
|
|
|
|
if (c != 'L' && c != 'U')
|
|
|
|
break;
|
|
|
|
suffix1 = c + suffix1;
|
|
|
|
}
|
|
|
|
for (std::size_t i = 1U; i < second.size(); ++i) {
|
|
|
|
char c = second[second.size() - i];
|
|
|
|
if (c == 'l' || c == 'u')
|
|
|
|
c = c - 'a' + 'A';
|
|
|
|
if (c != 'L' && c != 'U')
|
|
|
|
break;
|
|
|
|
suffix2 = c + suffix2;
|
|
|
|
}
|
|
|
|
|
2015-06-16 19:12:18 +02:00
|
|
|
if (suffix1 == "ULL" || suffix2 == "ULL"
|
|
|
|
|| suffix1 == "LLU" || suffix2 == "LLU")
|
2015-06-15 19:01:56 +02:00
|
|
|
return "ULL";
|
|
|
|
if (suffix1 == "LL" || suffix2 == "LL")
|
|
|
|
return "LL";
|
2015-06-16 19:12:18 +02:00
|
|
|
if (suffix1 == "UL" || suffix2 == "UL"
|
|
|
|
|| suffix1 == "LU" || suffix2 == "LU")
|
2015-06-15 19:01:56 +02:00
|
|
|
return "UL";
|
|
|
|
if (suffix1 == "L" || suffix2 == "L")
|
|
|
|
return "L";
|
|
|
|
if (suffix1 == "U" || suffix2 == "U")
|
|
|
|
return "U";
|
|
|
|
|
|
|
|
return suffix1.empty() ? suffix2 : suffix1;
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
std::string MathLib::add(const std::string & first, const std::string & second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) + toLongNumber(second)) + intsuffix(first, second);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2012-08-14 16:17:18 +02:00
|
|
|
|
|
|
|
double d1 = toDoubleNumber(first);
|
|
|
|
double d2 = toDoubleNumber(second);
|
2012-08-22 20:50:39 +02:00
|
|
|
|
|
|
|
int count = 0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d1 > 100000.0 * d2 && toString(d1+d2)==first && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d2 *= 10.0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d2 > 100000.0 * d1 && toString(d1+d2)==second && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d1 *= 10.0;
|
|
|
|
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(d1 + d2);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::subtract(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) - toLongNumber(second)) + intsuffix(first, second);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2012-08-14 16:17:18 +02:00
|
|
|
|
2012-09-05 13:57:57 +02:00
|
|
|
if (first == second)
|
|
|
|
return "0.0" ;
|
|
|
|
|
2012-08-14 16:17:18 +02:00
|
|
|
double d1 = toDoubleNumber(first);
|
|
|
|
double d2 = toDoubleNumber(second);
|
2012-08-22 20:50:39 +02:00
|
|
|
|
|
|
|
int count = 0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d1 > 100000.0 * d2 && toString(d1-d2)==first && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d2 *= 10.0;
|
2013-09-21 18:48:48 +02:00
|
|
|
while (d2 > 100000.0 * d1 && toString(d1-d2)==second && ++count<5)
|
2012-08-14 16:17:18 +02:00
|
|
|
d1 *= 10.0;
|
|
|
|
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(d1 - d2);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 18:25:01 +02:00
|
|
|
std::string MathLib::incdec(const std::string & var, const std::string & op)
|
|
|
|
{
|
|
|
|
if (op == "++")
|
|
|
|
return MathLib::add(var, "1");
|
|
|
|
else if (op == "--")
|
|
|
|
return MathLib::subtract(var, "1");
|
|
|
|
|
|
|
|
throw InternalError(0, std::string("Unexpected operation '") + op + "' in MathLib::incdec(). Please report this to Cppcheck developers.");
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
std::string MathLib::divide(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2013-09-29 15:49:02 +02:00
|
|
|
const bigint a = toLongNumber(first);
|
|
|
|
const bigint b = toLongNumber(second);
|
2012-09-06 20:15:32 +02:00
|
|
|
if (b == 0)
|
|
|
|
throw InternalError(0, "Internal Error: Division by zero");
|
2015-06-20 23:15:35 +02:00
|
|
|
if (a == std::numeric_limits<bigint>::min() && std::abs(b)<=1)
|
|
|
|
throw InternalError(0, "Internal Error: Division overflow");
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) / b) + intsuffix(first, second);
|
2014-05-31 18:25:23 +02:00
|
|
|
} else if (isNullValue(second)) {
|
|
|
|
if (isNullValue(first))
|
2013-11-04 11:56:43 +01:00
|
|
|
return "nan.0";
|
2014-05-16 23:01:21 +02:00
|
|
|
const int sign_first = (isPositive(first)) ? 1 : -1;
|
|
|
|
const int sign_second = (isPositive(second)) ? 1 : -1;
|
2014-05-31 18:25:23 +02:00
|
|
|
return (sign_first*sign_second == 1) ? "inf.0" : "-inf.0";
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toDoubleNumber(first) / toDoubleNumber(second));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::multiply(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) * toLongNumber(second)) + intsuffix(first, second);
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toDoubleNumber(first) * toDoubleNumber(second));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2012-04-12 14:02:09 +02:00
|
|
|
std::string MathLib::mod(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
if (MathLib::isInt(first) && MathLib::isInt(second)) {
|
2015-04-07 17:21:28 +02:00
|
|
|
const bigint b = toLongNumber(second);
|
2012-09-06 20:15:32 +02:00
|
|
|
if (b == 0)
|
|
|
|
throw InternalError(0, "Internal Error: Division by zero");
|
2015-06-15 19:01:56 +02:00
|
|
|
return toString(toLongNumber(first) % b) + intsuffix(first, second);
|
2012-04-12 14:02:09 +02:00
|
|
|
}
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::fmod(toDoubleNumber(first),toDoubleNumber(second)));
|
2012-04-12 14:02:09 +02:00
|
|
|
}
|
|
|
|
|
2012-01-08 21:19:44 +01:00
|
|
|
std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
|
2009-08-02 14:22:39 +02:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
switch (action) {
|
2009-08-02 21:11:17 +02:00
|
|
|
case '+':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::add(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
|
|
|
case '-':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::subtract(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
|
|
|
case '*':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::multiply(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
|
|
|
case '/':
|
2012-02-17 19:54:53 +01:00
|
|
|
return MathLib::divide(first, second);
|
2009-08-02 21:11:17 +02:00
|
|
|
|
2012-04-12 14:02:09 +02:00
|
|
|
case '%':
|
|
|
|
return MathLib::mod(first, second);
|
|
|
|
|
2012-05-16 18:48:33 +02:00
|
|
|
case '&':
|
2015-06-15 19:01:56 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) & MathLib::toLongNumber(second)) + intsuffix(first,second);
|
2012-05-16 18:48:33 +02:00
|
|
|
|
|
|
|
case '|':
|
2015-06-15 19:01:56 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) | MathLib::toLongNumber(second)) + intsuffix(first,second);
|
2012-05-16 18:48:33 +02:00
|
|
|
|
|
|
|
case '^':
|
2015-06-15 19:01:56 +02:00
|
|
|
return MathLib::toString(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second)) + intsuffix(first,second);
|
2012-05-16 18:48:33 +02:00
|
|
|
|
2009-08-02 21:11:17 +02:00
|
|
|
default:
|
2012-01-08 21:19:44 +01:00
|
|
|
throw InternalError(0, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
|
2009-08-02 14:22:39 +02:00
|
|
|
}
|
|
|
|
}
|
2009-06-02 22:32:58 +02:00
|
|
|
|
|
|
|
std::string MathLib::sin(const std::string &tok)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::sin(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::cos(const std::string &tok)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::cos(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::tan(const std::string &tok)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::tan(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string MathLib::abs(const std::string &tok)
|
|
|
|
{
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(std::abs(toDoubleNumber(tok)));
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
2011-07-17 05:05:35 +02:00
|
|
|
bool MathLib::isEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
2013-01-16 15:37:07 +01:00
|
|
|
// this conversion is needed for formatting
|
2013-06-12 22:13:05 +02:00
|
|
|
// e.g. if first=0.1 and second=1.0E-1, the direct comparison of the strings would fail
|
2013-09-21 18:48:48 +02:00
|
|
|
return toString(toDoubleNumber(first)) == toString(toDoubleNumber(second));
|
2011-07-17 05:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isNotEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
2011-07-24 21:35:21 +02:00
|
|
|
return !isEqual(first, second);
|
2011-07-17 05:05:35 +02:00
|
|
|
}
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
bool MathLib::isGreater(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) > toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
2011-07-17 05:05:35 +02:00
|
|
|
bool MathLib::isGreaterEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) >= toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isLess(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) < toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isLessEqual(const std::string &first, const std::string &second)
|
|
|
|
{
|
|
|
|
return toDoubleNumber(first) <= toDoubleNumber(second);
|
|
|
|
}
|
|
|
|
|
2014-04-14 05:27:38 +02:00
|
|
|
/*! \brief Does the string represent the numerical value of 0?
|
|
|
|
* In case leading or trailing white space is provided, the function
|
|
|
|
* returns false.
|
|
|
|
* Requirement for this function:
|
|
|
|
* - This code is allowed to be slow because of simplicity of the code.
|
|
|
|
*
|
|
|
|
* \param[in] str The string to check. In case the string is empty, the function returns false.
|
|
|
|
* \return Return true in case the string represents a numerical null value.
|
|
|
|
**/
|
|
|
|
bool MathLib::isNullValue(const std::string &str)
|
2010-04-02 22:41:54 +02:00
|
|
|
{
|
2014-04-14 05:27:38 +02:00
|
|
|
if (str.empty() || (!std::isdigit(static_cast<unsigned char>(str[0])) && (str.size() < 1 || (str[0] != '.' && str[0] != '-' && str[0] != '+'))))
|
|
|
|
return false; // Has to be a number
|
|
|
|
|
|
|
|
for (size_t i = 0; i < str.size(); i++) {
|
|
|
|
if (std::isdigit(static_cast<unsigned char>(str[i])) && str[i] != '0') // May not contain digits other than 0
|
2014-04-11 12:00:33 +02:00
|
|
|
return false;
|
2014-04-14 05:27:38 +02:00
|
|
|
if (str[i] == 'E' || str[i] == 'e')
|
|
|
|
return true;
|
2014-04-11 12:00:33 +02:00
|
|
|
}
|
2014-04-14 05:27:38 +02:00
|
|
|
return true;
|
2010-04-02 22:41:54 +02:00
|
|
|
}
|
|
|
|
|
2009-09-27 16:04:10 +02:00
|
|
|
bool MathLib::isOctalDigit(char c)
|
|
|
|
{
|
2014-04-11 12:00:33 +02:00
|
|
|
return (c >= '0' && c <= '7');
|
2009-09-27 16:04:10 +02:00
|
|
|
}
|