2009-05-30 07:48:12 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2016-01-01 14:34:45 +01:00
|
|
|
* Copyright (C) 2007-2016 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
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-10-14 16:30:10 +02:00
|
|
|
#include "mathlib.h"
|
2012-01-08 21:19:44 +01:00
|
|
|
#include "errorlogger.h"
|
2017-04-01 18:14:18 +02:00
|
|
|
#include "utils.h"
|
2009-06-02 22:32:58 +02:00
|
|
|
|
2009-08-17 21:24:17 +02:00
|
|
|
#include <cctype>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cmath>
|
2015-11-29 10:49:10 +01:00
|
|
|
#include <cstdlib>
|
2013-05-09 18:50:24 +02:00
|
|
|
#include <limits>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <locale>
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2015-07-14 20:48:08 +02:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER <= 1700 // VS2012 doesn't have std::isinf and std::isnan
|
|
|
|
#define ISINF(x) (!_finite(x))
|
|
|
|
#define ISNAN(x) (_isnan(x))
|
|
|
|
#elif defined(__INTEL_COMPILER)
|
|
|
|
#define ISINF(x) (isinf(x))
|
|
|
|
#define ISNAN(x) (isnan(x))
|
|
|
|
#else // Use C++11 functions
|
|
|
|
#define ISINF(x) (std::isinf(x))
|
|
|
|
#define ISNAN(x) (std::isnan(x))
|
|
|
|
#endif
|
|
|
|
|
2017-10-20 17:38:15 +02:00
|
|
|
const int MathLib::bigint_bits = 64;
|
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
MathLib::value::value(const std::string &s) :
|
|
|
|
intValue(0), doubleValue(0), isUnsigned(false)
|
|
|
|
{
|
|
|
|
if (MathLib::isFloat(s)) {
|
|
|
|
type = MathLib::value::FLOAT;
|
|
|
|
doubleValue = MathLib::toDoubleNumber(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MathLib::isInt(s))
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Invalid value: " + s);
|
2015-07-10 20:36:38 +02:00
|
|
|
|
|
|
|
type = MathLib::value::INT;
|
|
|
|
intValue = MathLib::toLongNumber(s);
|
|
|
|
|
2016-03-18 10:30:57 +01:00
|
|
|
if (isIntHex(s) && intValue < 0)
|
|
|
|
isUnsigned = true;
|
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
// read suffix
|
|
|
|
if (s.size() >= 2U) {
|
|
|
|
for (std::size_t i = s.size() - 1U; i > 0U; --i) {
|
|
|
|
char c = s[i];
|
|
|
|
if (c == 'u' || c == 'U')
|
|
|
|
isUnsigned = true;
|
|
|
|
else if (c == 'l' || c == 'L') {
|
|
|
|
if (type == MathLib::value::INT)
|
|
|
|
type = MathLib::value::LONG;
|
|
|
|
else if (type == MathLib::value::LONG)
|
|
|
|
type = MathLib::value::LONGLONG;
|
2016-07-26 13:19:28 +02:00
|
|
|
} else if (i > 2U && c == '4' && s[i-1] == '6' && s[i-2] == 'i')
|
|
|
|
type = MathLib::value::LONGLONG;
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::value::str() const
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
if (type == MathLib::value::FLOAT) {
|
2015-07-14 20:48:08 +02:00
|
|
|
if (ISNAN(doubleValue))
|
2015-07-10 20:36:38 +02:00
|
|
|
return "nan.0";
|
2015-07-14 20:48:08 +02:00
|
|
|
if (ISINF(doubleValue))
|
2015-07-10 20:36:38 +02:00
|
|
|
return (doubleValue > 0) ? "inf.0" : "-inf.0";
|
2015-07-14 20:48:08 +02:00
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
ostr.precision(9);
|
|
|
|
ostr << std::fixed << doubleValue;
|
|
|
|
|
|
|
|
// remove trailing zeros
|
|
|
|
std::string ret(ostr.str());
|
|
|
|
std::string::size_type pos = ret.size() - 1U;
|
|
|
|
while (ret[pos] == '0')
|
|
|
|
pos--;
|
|
|
|
if (ret[pos] == '.')
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
return ret.substr(0, pos+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUnsigned)
|
2016-03-18 10:30:57 +01:00
|
|
|
ostr << static_cast<biguint>(intValue) << "U";
|
|
|
|
else
|
|
|
|
ostr << intValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
if (type == MathLib::value::LONG)
|
|
|
|
ostr << "L";
|
|
|
|
else if (type == MathLib::value::LONGLONG)
|
|
|
|
ostr << "LL";
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathLib::value::promote(const MathLib::value &v)
|
|
|
|
{
|
|
|
|
if (isInt() && v.isInt()) {
|
|
|
|
if (type < v.type) {
|
|
|
|
type = v.type;
|
|
|
|
isUnsigned = v.isUnsigned;
|
|
|
|
} else if (type == v.type) {
|
|
|
|
isUnsigned |= v.isUnsigned;
|
|
|
|
}
|
|
|
|
} else if (!isFloat()) {
|
|
|
|
isUnsigned = false;
|
|
|
|
doubleValue = intValue;
|
|
|
|
type = MathLib::value::FLOAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
value temp(v1);
|
|
|
|
temp.promote(v2);
|
|
|
|
if (temp.isFloat()) {
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
temp.doubleValue += v2.getDoubleValue();
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
temp.doubleValue -= v2.getDoubleValue();
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
temp.doubleValue *= v2.getDoubleValue();
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
temp.doubleValue /= v2.getDoubleValue();
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '^':
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Invalid calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Unhandled calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
} else if (temp.isUnsigned) {
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
temp.intValue += (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
temp.intValue -= (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
temp.intValue *= (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (v2.intValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2015-07-10 20:36:38 +02:00
|
|
|
if (v1.intValue == std::numeric_limits<bigint>::min() && std::abs(v2.intValue)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
2015-07-10 20:36:38 +02:00
|
|
|
temp.intValue /= (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if (v2.intValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2015-07-10 20:36:38 +02:00
|
|
|
temp.intValue %= (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
temp.intValue &= (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
temp.intValue |= (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
temp.intValue ^= (unsigned long long)v2.intValue;
|
|
|
|
break;
|
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Unhandled calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
temp.intValue += v2.intValue;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
temp.intValue -= v2.intValue;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
temp.intValue *= v2.intValue;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (v2.intValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2015-07-10 20:36:38 +02:00
|
|
|
if (v1.intValue == std::numeric_limits<bigint>::min() && std::abs(v2.intValue)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
2015-07-10 20:36:38 +02:00
|
|
|
temp.intValue /= v2.intValue;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if (v2.intValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2015-07-10 20:36:38 +02:00
|
|
|
temp.intValue %= v2.intValue;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
temp.intValue &= v2.intValue;
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
temp.intValue |= v2.intValue;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
temp.intValue ^= v2.intValue;
|
|
|
|
break;
|
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Unhandled calculation");
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int MathLib::value::compare(const MathLib::value &v) const
|
|
|
|
{
|
|
|
|
value temp(*this);
|
|
|
|
temp.promote(v);
|
|
|
|
|
|
|
|
if (temp.isFloat()) {
|
|
|
|
if (temp.doubleValue < v.getDoubleValue())
|
|
|
|
return -1;
|
|
|
|
if (temp.doubleValue > v.getDoubleValue())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp.isUnsigned) {
|
|
|
|
if ((unsigned long long)intValue < (unsigned long long)v.intValue)
|
|
|
|
return -1;
|
|
|
|
if ((unsigned long long)intValue > (unsigned long long)v.intValue)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intValue < v.intValue)
|
|
|
|
return -1;
|
|
|
|
if (intValue > v.intValue)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value MathLib::value::add(int v) const
|
|
|
|
{
|
|
|
|
MathLib::value temp(*this);
|
|
|
|
if (temp.isInt())
|
|
|
|
temp.intValue += v;
|
|
|
|
else
|
|
|
|
temp.doubleValue += v;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2016-03-21 21:20:02 +01:00
|
|
|
MathLib::value MathLib::value::shiftLeft(const MathLib::value &v) const
|
|
|
|
{
|
|
|
|
if (!isInt() || !v.isInt())
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Shift operand is not integer");
|
2016-03-21 21:20:02 +01:00
|
|
|
MathLib::value ret(*this);
|
2017-10-21 21:04:14 +02:00
|
|
|
if (v.intValue >= MathLib::bigint_bits) {
|
|
|
|
return ret;
|
|
|
|
}
|
2016-03-21 21:20:02 +01:00
|
|
|
ret.intValue <<= v.intValue;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value MathLib::value::shiftRight(const MathLib::value &v) const
|
|
|
|
{
|
|
|
|
if (!isInt() || !v.isInt())
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Shift operand is not integer");
|
2016-03-21 21:20:02 +01:00
|
|
|
MathLib::value ret(*this);
|
2017-10-21 21:04:14 +02:00
|
|
|
if (v.intValue >= MathLib::bigint_bits) {
|
|
|
|
return ret;
|
|
|
|
}
|
2016-03-21 21:20:02 +01:00
|
|
|
ret.intValue >>= v.intValue;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
MathLib::biguint MathLib::toULongNumber(const std::string & str)
|
|
|
|
{
|
|
|
|
// hexadecimal numbers:
|
2015-07-26 08:06:56 +02:00
|
|
|
if (isIntHex(str)) {
|
2014-04-15 06:32:25 +02:00
|
|
|
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
|
|
|
|
2015-11-28 16:37:26 +01:00
|
|
|
static unsigned int encodeMultiChar(const std::string& str)
|
|
|
|
{
|
|
|
|
unsigned int retval(str.front());
|
|
|
|
for (std::string::const_iterator it=str.begin()+1; it!=str.end(); ++it) {
|
|
|
|
retval = retval<<8 | *it;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-10-16 19:03:14 +02:00
|
|
|
static bool isoctal(int c)
|
|
|
|
{
|
2016-10-16 13:42:20 +02:00
|
|
|
return c>='0' && c<='7';
|
|
|
|
}
|
|
|
|
|
2015-11-21 22:35:58 +01:00
|
|
|
MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str)
|
|
|
|
{
|
2015-11-22 07:38:38 +01:00
|
|
|
if (str.empty())
|
2016-08-28 11:37:05 +02:00
|
|
|
return 0; // <- only possible in unit testing
|
|
|
|
|
|
|
|
// '\xF6'
|
|
|
|
if (str.size() == 4 && str.compare(0,2,"\\x")==0 && std::isxdigit(str[2]) && std::isxdigit(str[3])) {
|
2017-08-09 20:00:26 +02:00
|
|
|
return std::strtoul(str.substr(2).c_str(), nullptr, 16);
|
2016-08-28 11:37:05 +02:00
|
|
|
}
|
2016-05-15 15:48:24 +02:00
|
|
|
|
2016-10-16 13:42:20 +02:00
|
|
|
// '\123'
|
|
|
|
if (str.size() == 4 && str[0] == '\\' && isoctal(str[1]) && isoctal(str[2]) && isoctal(str[3])) {
|
|
|
|
return (char)std::strtoul(str.substr(1).c_str(), NULL, 8);
|
|
|
|
}
|
|
|
|
|
2016-05-15 15:48:24 +02:00
|
|
|
// C99 6.4.4.4
|
|
|
|
// The value of an integer character constant containing more than one character (e.g., 'ab'),
|
|
|
|
// or containing a character or escape sequence that does not map to a single-byte execution character,
|
|
|
|
// is implementation-defined.
|
|
|
|
// clang and gcc seem to use the following encoding: 'AB' as (('A' << 8) | 'B')
|
|
|
|
const std::string& normStr = normalizeCharacterLiteral(str);
|
|
|
|
return encodeMultiChar(normStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::normalizeCharacterLiteral(const std::string& iLiteral)
|
|
|
|
{
|
|
|
|
std::string normalizedLiteral;
|
|
|
|
const std::string::size_type iLiteralLen = iLiteral.size();
|
|
|
|
for (std::string::size_type idx = 0; idx < iLiteralLen ; ++idx) {
|
|
|
|
if (iLiteral[idx] != '\\') {
|
|
|
|
normalizedLiteral.push_back(iLiteral[idx]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
if (idx == iLiteralLen) {
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
|
2016-05-15 15:48:24 +02:00
|
|
|
}
|
|
|
|
switch (iLiteral[idx]) {
|
|
|
|
case 'x':
|
|
|
|
// Hexa-decimal number: skip \x and interpret the next two characters
|
2016-05-16 09:36:26 +02:00
|
|
|
{
|
|
|
|
if (++idx == iLiteralLen)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
|
2016-05-16 09:36:26 +02:00
|
|
|
std::string tempBuf;
|
|
|
|
tempBuf.push_back(iLiteral[idx]);
|
|
|
|
if (++idx != iLiteralLen)
|
2016-05-15 15:48:24 +02:00
|
|
|
tempBuf.push_back(iLiteral[idx]);
|
2016-05-16 09:36:26 +02:00
|
|
|
normalizedLiteral.push_back(static_cast<char>(MathLib::toULongNumber("0x" + tempBuf)));
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-15 15:48:24 +02:00
|
|
|
case 'u':
|
|
|
|
case 'U':
|
|
|
|
// Unicode string; just skip the \u or \U
|
2016-05-20 22:52:43 +02:00
|
|
|
if (idx + 1 == iLiteralLen)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::characterLiteralToLongNumber: Unhandled char constant '" + iLiteral + "'.");
|
2016-05-15 15:48:24 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Single digit octal number
|
2016-05-22 22:59:32 +02:00
|
|
|
if (1 == iLiteralLen - idx) {
|
2016-05-15 15:48:24 +02:00
|
|
|
switch (iLiteral[idx]) {
|
2015-11-22 07:38:38 +01:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back(iLiteral[idx]-'0');
|
|
|
|
break;
|
2015-11-22 07:38:38 +01:00
|
|
|
case 'a':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back('\a');
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
|
|
|
case 'b':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back('\b');
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
2015-12-05 18:53:03 +01:00
|
|
|
case 'e':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back(0x1B); // clang, gcc, tcc interpnormalizedLiteral this as 0x1B - escape character
|
2015-12-05 18:53:03 +01:00
|
|
|
break;
|
2015-11-22 07:38:38 +01:00
|
|
|
case 'f':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back('\f');
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
|
|
|
case 'n':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back('\n');
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
|
|
|
case 'r':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back('\r');
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
|
|
|
case 't':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back('\t');
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
|
|
|
case 'v':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back('\v');
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
case '\?':
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
2016-05-15 15:48:24 +02:00
|
|
|
normalizedLiteral.push_back(iLiteral[idx]);
|
2015-11-22 07:38:38 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
|
2015-11-22 07:38:38 +01:00
|
|
|
}
|
2016-05-15 15:48:24 +02:00
|
|
|
continue;
|
2015-11-23 20:41:21 +01:00
|
|
|
}
|
2016-05-15 15:48:24 +02:00
|
|
|
// 2-3 digit octal number
|
|
|
|
if (!MathLib::isOctalDigit(iLiteral[idx]))
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
|
2016-05-15 15:48:24 +02:00
|
|
|
std::string tempBuf;
|
|
|
|
tempBuf.push_back(iLiteral[idx]);
|
|
|
|
++idx;
|
|
|
|
if (MathLib::isOctalDigit(iLiteral[idx])) {
|
|
|
|
tempBuf.push_back(iLiteral[idx]);
|
|
|
|
++idx;
|
|
|
|
if (MathLib::isOctalDigit(iLiteral[idx])) {
|
|
|
|
tempBuf.push_back(iLiteral[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
normalizedLiteral.push_back(static_cast<char>(MathLib::toLongNumber("0" + tempBuf)));
|
2015-11-22 07:38:38 +01:00
|
|
|
}
|
2016-05-15 15:48:24 +02:00
|
|
|
return normalizedLiteral;
|
2015-11-21 22:35:58 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 06:32:25 +02:00
|
|
|
MathLib::bigint MathLib::toLongNumber(const std::string & str)
|
|
|
|
{
|
|
|
|
// hexadecimal numbers:
|
2015-07-26 08:06:56 +02:00
|
|
|
if (isIntHex(str)) {
|
2014-04-15 06:32:25 +02:00
|
|
|
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
|
2015-07-10 18:34:28 +02:00
|
|
|
const double doubleval = toDoubleNumber(str);
|
2014-05-21 21:36:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-01 18:14:18 +02:00
|
|
|
if (str[0] == '\'' && str.size() >= 3U && endsWith(str,'\'')) {
|
2015-11-21 22:35:58 +01:00
|
|
|
return characterLiteralToLongNumber(str.substr(1,str.size()-2));
|
2015-11-20 10:10:38 +01:00
|
|
|
}
|
|
|
|
|
2017-07-22 12:19:46 +02:00
|
|
|
if (str[0] == '-') {
|
|
|
|
bigint ret = 0;
|
|
|
|
std::istringstream istr(str);
|
|
|
|
istr >> ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
biguint ret = 0;
|
2014-04-15 06:32:25 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-04-01 18:14:18 +02:00
|
|
|
if (str[0] == '\'' && str.size() >= 3U && endsWith(str,'\''))
|
2017-02-25 21:58:09 +01:00
|
|
|
return characterLiteralToLongNumber(str.substr(1,str.size()-2));
|
2015-07-26 08:06:56 +02:00
|
|
|
if (isIntHex(str))
|
2012-02-17 19:54:53 +01:00
|
|
|
return static_cast<double>(toLongNumber(str));
|
2010-04-02 20:42:06 +02:00
|
|
|
// nullcheck
|
2017-02-25 21:58:09 +01:00
|
|
|
if (isNullValue(str))
|
2010-04-02 20:42:06 +02:00
|
|
|
return 0.0;
|
2015-07-10 18:34:28 +02:00
|
|
|
#ifdef __clang__
|
2017-02-25 21:58:09 +01:00
|
|
|
if (isFloat(str)) // Workaround libc++ bug at http://llvm.org/bugs/show_bug.cgi?id=17782
|
2015-07-10 18:34:28 +02:00
|
|
|
// TODO : handle locale
|
2017-08-09 20:00:26 +02:00
|
|
|
return std::strtod(str.c_str(), nullptr);
|
2015-07-10 18:34:28 +02:00
|
|
|
#endif
|
2010-04-02 20:42:06 +02:00
|
|
|
// otherwise, convert to double
|
2011-12-08 21:28:34 +01:00
|
|
|
std::istringstream istr(str);
|
2015-07-10 18:34:28 +02:00
|
|
|
istr.imbue(std::locale::classic());
|
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";
|
2015-08-15 20:24:26 +02:00
|
|
|
if (result.str().find('.') == std::string::npos)
|
2012-09-11 16:50:42 +02:00
|
|
|
return result.str() + ".0";
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isFloat(const std::string &str)
|
2015-07-26 19:21:53 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
return isDecimalFloat(str) || isFloatHex(str);
|
2015-07-26 19:21:53 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isDecimalFloat(const std::string &str)
|
2010-04-02 02:19:38 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
if (str.empty())
|
2014-02-20 16:56:49 +01:00
|
|
|
return false;
|
2014-04-30 21:01:47 +02:00
|
|
|
enum State {
|
2015-07-26 19:21:53 +02:00
|
|
|
START, BASE_PLUSMINUS, BASE_DIGITS1, LEADING_DECIMAL, TRAILING_DECIMAL, BASE_DIGITS2, E, MANTISSA_PLUSMINUS, MANTISSA_DIGITS, SUFFIX_F, SUFFIX_L
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2017-04-01 09:31:27 +02:00
|
|
|
for (std::string::const_iterator it = str.begin(); it != str.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;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*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;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (std::isdigit(static_cast<unsigned char>(*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;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
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')
|
2015-07-26 19:21:53 +02:00
|
|
|
state=SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2015-07-26 19:21:53 +02:00
|
|
|
state=SUFFIX_L;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*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')
|
2015-07-26 19:21:53 +02:00
|
|
|
state=SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2015-07-26 19:21:53 +02:00
|
|
|
state=SUFFIX_L;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
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;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
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')
|
2015-07-26 19:21:53 +02:00
|
|
|
state=SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2015-07-26 19:21:53 +02:00
|
|
|
state=SUFFIX_L;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (!std::isdigit(static_cast<unsigned char>(*it)))
|
2014-02-24 18:52:31 +01:00
|
|
|
return false;
|
|
|
|
break;
|
2015-07-26 19:21:53 +02:00
|
|
|
case SUFFIX_F:
|
2014-02-20 16:56:49 +01:00
|
|
|
return false;
|
2015-07-26 19:21:53 +02:00
|
|
|
case SUFFIX_L:
|
2014-11-17 06:53:32 +01:00
|
|
|
return false;
|
2014-02-24 18:52:31 +01:00
|
|
|
}
|
2014-02-20 16:56:49 +01:00
|
|
|
}
|
2015-07-26 19:21:53 +02:00
|
|
|
return (state==BASE_DIGITS2 || state==MANTISSA_DIGITS || state==TRAILING_DECIMAL || state==SUFFIX_F || state==SUFFIX_L);
|
2010-04-02 02:19:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isNegative(const std::string &str)
|
2010-04-02 02:19:38 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
if (str.empty())
|
2015-07-26 19:21:53 +02:00
|
|
|
return false;
|
2017-04-01 09:31:27 +02:00
|
|
|
return (str[0] == '-');
|
2012-02-17 19:54:53 +01:00
|
|
|
}
|
2010-04-02 07:32:03 +02:00
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isPositive(const std::string &str)
|
2013-09-29 15:11:09 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
if (str.empty())
|
2015-07-26 19:21:53 +02:00
|
|
|
return false;
|
2017-04-01 09:31:27 +02:00
|
|
|
return !MathLib::isNegative(str);
|
2013-09-29 15:11:09 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
2017-04-01 09:31:27 +02:00
|
|
|
* \param str The string to check. In case the string is empty, the function returns false.
|
2014-03-09 21:14:14 +01:00
|
|
|
* \return Return true in case a octal number is provided and false otherwise.
|
|
|
|
**/
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isOct(const std::string& str)
|
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;
|
2017-04-01 09:31:27 +02:00
|
|
|
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
|
2014-03-09 21:14:14 +01:00
|
|
|
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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isOctalDigit(static_cast<unsigned char>(*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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isOctalDigit(static_cast<unsigned char>(*it)))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
2017-04-01 09:31:27 +02:00
|
|
|
return isValidIntegerSuffix(it,str.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
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isIntHex(const std::string& str)
|
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;
|
2017-04-01 09:31:27 +02:00
|
|
|
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
|
2014-03-11 20:02:04 +01:00
|
|
|
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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGITS;
|
2014-03-14 11:46:41 +01:00
|
|
|
else
|
2017-04-01 09:31:27 +02:00
|
|
|
return isValidIntegerSuffix(it,str.end());
|
2014-03-14 11:46:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 21:01:47 +02:00
|
|
|
return state == DIGITS;
|
2014-03-14 11:46:41 +01:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isFloatHex(const std::string& str)
|
2015-07-26 08:06:56 +02:00
|
|
|
{
|
|
|
|
enum Status {
|
|
|
|
START, PLUSMINUS, HEX_PREFIX, WHOLE_NUMBER_DIGIT, WHOLE_NUMBER_DIGITS, FRACTION, EXPONENT_DIGIT, EXPONENT_DIGITS
|
|
|
|
} state = START;
|
2017-04-01 09:31:27 +02:00
|
|
|
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
|
2015-07-26 08:06:56 +02:00
|
|
|
switch (state) {
|
|
|
|
case START:
|
|
|
|
if (*it == '+' || *it == '-')
|
|
|
|
state = PLUSMINUS;
|
|
|
|
else if (*it == '0')
|
|
|
|
state = HEX_PREFIX;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case PLUSMINUS:
|
|
|
|
if (*it == '0')
|
|
|
|
state = HEX_PREFIX;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case HEX_PREFIX:
|
|
|
|
if (*it == 'x' || *it == 'X')
|
|
|
|
state = WHOLE_NUMBER_DIGIT;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case WHOLE_NUMBER_DIGIT:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2015-07-26 08:06:56 +02:00
|
|
|
state = WHOLE_NUMBER_DIGITS;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case WHOLE_NUMBER_DIGITS:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2015-07-26 08:06:56 +02:00
|
|
|
state = WHOLE_NUMBER_DIGITS;
|
|
|
|
else if (*it=='.')
|
|
|
|
state = FRACTION;
|
|
|
|
else if (*it=='p' || *it=='P')
|
|
|
|
state = EXPONENT_DIGIT;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case FRACTION:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2015-07-26 08:06:56 +02:00
|
|
|
state = FRACTION;
|
|
|
|
else if (*it=='p' || *it=='P')
|
|
|
|
state = EXPONENT_DIGIT;
|
|
|
|
break;
|
|
|
|
case EXPONENT_DIGIT:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2015-07-26 08:06:56 +02:00
|
|
|
state = EXPONENT_DIGITS;
|
|
|
|
else if (*it=='+' || *it=='-')
|
|
|
|
state = EXPONENT_DIGITS;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case EXPONENT_DIGITS:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2015-07-26 08:06:56 +02:00
|
|
|
state = EXPONENT_DIGITS;
|
|
|
|
else
|
|
|
|
return *it=='f'||*it=='F'||*it=='l'||*it=='L';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state==EXPONENT_DIGITS;
|
|
|
|
}
|
|
|
|
|
2017-05-05 14:47:58 +02:00
|
|
|
bool MathLib::isValidIntegerSuffix(const std::string& str)
|
|
|
|
{
|
2017-05-06 11:57:02 +02:00
|
|
|
return isValidIntegerSuffix(str.begin(), str.end());
|
2017-05-05 14:47:58 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 08:06:56 +02:00
|
|
|
bool MathLib::isValidIntegerSuffix(std::string::const_iterator it, std::string::const_iterator end)
|
2014-03-14 11:46:41 +01:00
|
|
|
{
|
2016-07-26 13:19:28 +02:00
|
|
|
enum {START, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU, SUFFIX_I, SUFFIX_I6, SUFFIX_I64, SUFFIX_UI, SUFFIX_UI6, SUFFIX_UI64} 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
|
2016-07-26 13:19:28 +02:00
|
|
|
else if (*it == 'i')
|
|
|
|
state = SUFFIX_UI;
|
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;
|
2016-07-26 13:19:28 +02:00
|
|
|
case SUFFIX_UI:
|
|
|
|
if (*it == '6')
|
|
|
|
state = SUFFIX_UI6;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SUFFIX_UI6:
|
|
|
|
if (*it == '4')
|
|
|
|
state = SUFFIX_UI64;
|
|
|
|
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) ||
|
2016-07-26 13:19:28 +02:00
|
|
|
(state == SUFFIX_I64) ||
|
|
|
|
(state == SUFFIX_UI64));
|
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
|
|
|
|
*
|
2017-04-01 09:31:27 +02:00
|
|
|
* \param str The string to check. In case the string is empty, the function returns false.
|
2014-03-10 17:23:07 +01:00
|
|
|
* \return Return true in case a binary number is provided and false otherwise.
|
|
|
|
**/
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isBin(const std::string& str)
|
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;
|
2017-04-01 09:31:27 +02:00
|
|
|
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
|
2014-03-10 17:23:07 +01:00
|
|
|
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
|
2017-04-01 09:31:27 +02:00
|
|
|
return isValidIntegerSuffix(it,str.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
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isDec(const std::string & str)
|
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;
|
2017-04-01 09:31:27 +02:00
|
|
|
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
|
2014-04-10 22:46:55 +02:00
|
|
|
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;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (isdigit(static_cast<unsigned char>(*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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*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:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2014-04-30 21:01:47 +02:00
|
|
|
state = DIGIT;
|
2014-04-10 22:46:55 +02:00
|
|
|
else
|
2017-04-01 09:31:27 +02:00
|
|
|
return isValidIntegerSuffix(it,str.end());
|
2014-04-10 22:46:55 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-01 09:31:27 +02:00
|
|
|
bool MathLib::isInt(const std::string & str)
|
2014-04-11 22:15:25 +02:00
|
|
|
{
|
2017-04-01 09:31:27 +02:00
|
|
|
return isDec(str) || isIntHex(str) || isOct(str) || isBin(str);
|
2014-04-11 22:15:25 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 14:47:58 +02:00
|
|
|
std::string MathLib::getSuffix(const std::string& value)
|
2015-06-15 19:01:56 +02:00
|
|
|
{
|
2016-07-26 13:19:28 +02:00
|
|
|
if (value.size() > 3 && value[value.size() - 3] == 'i' && value[value.size() - 2] == '6' && value[value.size() - 1] == '4') {
|
|
|
|
if (value[value.size() - 4] == 'u')
|
|
|
|
return "ULL";
|
|
|
|
return "LL";
|
2015-06-15 19:01:56 +02:00
|
|
|
}
|
2016-07-26 13:19:28 +02:00
|
|
|
bool isUnsigned = false;
|
|
|
|
unsigned int longState = 0;
|
|
|
|
for (std::size_t i = 1U; i < value.size(); ++i) {
|
|
|
|
char c = value[value.size() - i];
|
|
|
|
if (c == 'u' || c == 'U')
|
|
|
|
isUnsigned = true;
|
|
|
|
else if (c == 'L' || c == 'l')
|
|
|
|
longState++;
|
|
|
|
else break;
|
2015-06-15 19:01:56 +02:00
|
|
|
}
|
2016-07-26 13:19:28 +02:00
|
|
|
if (longState == 0)
|
|
|
|
return isUnsigned ? "U" : "";
|
|
|
|
if (longState == 1)
|
|
|
|
return isUnsigned ? "UL" : "L";
|
|
|
|
if (longState == 2)
|
|
|
|
return isUnsigned ? "ULL" : "LL";
|
|
|
|
else return "";
|
|
|
|
}
|
2015-06-15 19:01:56 +02:00
|
|
|
|
2016-07-26 13:19:28 +02:00
|
|
|
static std::string intsuffix(const std::string & first, const std::string & second)
|
|
|
|
{
|
2017-05-05 14:47:58 +02:00
|
|
|
const std::string suffix1 = MathLib::getSuffix(first);
|
|
|
|
const std::string suffix2 = MathLib::getSuffix(second);
|
2016-07-26 13:19:28 +02:00
|
|
|
if (suffix1 == "ULL" || suffix2 == "ULL")
|
2015-06-15 19:01:56 +02:00
|
|
|
return "ULL";
|
|
|
|
if (suffix1 == "LL" || suffix2 == "LL")
|
|
|
|
return "LL";
|
2016-07-26 13:19:28 +02:00
|
|
|
if (suffix1 == "UL" || suffix2 == "UL")
|
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)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) + value(second)).str();
|
|
|
|
#else
|
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);
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::subtract(const std::string &first, const std::string &second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) - value(second)).str();
|
|
|
|
#else
|
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);
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
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)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
if (op == "++")
|
|
|
|
return value(var).add(1).str();
|
|
|
|
else if (op == "--")
|
|
|
|
return value(var).add(-1).str();
|
|
|
|
#else
|
2014-05-29 18:25:01 +02:00
|
|
|
if (op == "++")
|
|
|
|
return MathLib::add(var, "1");
|
|
|
|
else if (op == "--")
|
|
|
|
return MathLib::subtract(var, "1");
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2014-05-29 18:25:01 +02:00
|
|
|
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, std::string("Unexpected operation '") + op + "' in MathLib::incdec(). Please report this to Cppcheck developers.");
|
2014-05-29 18:25:01 +02:00
|
|
|
}
|
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
std::string MathLib::divide(const std::string &first, const std::string &second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) / value(second)).str();
|
|
|
|
#else
|
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)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2015-06-20 23:15:35 +02:00
|
|
|
if (a == std::numeric_limits<bigint>::min() && std::abs(b)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "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";
|
2015-07-15 17:36:41 +02:00
|
|
|
return isPositive(first) ? "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));
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
2009-06-02 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::multiply(const std::string &first, const std::string &second)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) * value(second)).str();
|
|
|
|
#else
|
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));
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
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)
|
|
|
|
{
|
2015-07-15 17:36:41 +02:00
|
|
|
#ifdef TEST_MATHLIB_VALUE
|
|
|
|
return (value(first) % value(second)).str();
|
|
|
|
#else
|
2012-04-12 14:02:09 +02:00
|
|
|
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)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "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)));
|
2015-07-15 17:36:41 +02:00
|
|
|
#endif
|
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:
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, 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
|
|
|
}
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2016-06-05 14:14:28 +02:00
|
|
|
bool MathLib::isDigitSeparator(const std::string& iCode, std::string::size_type iPos)
|
|
|
|
{
|
2016-06-04 22:55:55 +02:00
|
|
|
if (iPos == 0 || iPos >= iCode.size() || iCode[iPos] != '\'')
|
|
|
|
return false;
|
|
|
|
std::string::size_type i = iPos - 1;
|
|
|
|
while (std::isxdigit(iCode[i])) {
|
|
|
|
if (i == 0)
|
|
|
|
return true; // Only xdigits before '
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
if (i == iPos - 1) { // No xdigit before '
|
|
|
|
return false;
|
|
|
|
} else {
|
2016-06-05 14:14:28 +02:00
|
|
|
switch (iCode[i]) {
|
2016-06-04 22:55:55 +02:00
|
|
|
case ' ':
|
|
|
|
case '.':
|
|
|
|
case ',':
|
|
|
|
case 'x':
|
|
|
|
case '(':
|
|
|
|
case '{':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '*':
|
|
|
|
case '%':
|
|
|
|
case '/':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '^':
|
|
|
|
case '~':
|
|
|
|
case '=':
|
|
|
|
return true;
|
2016-07-01 23:22:16 +02:00
|
|
|
case '\'':
|
|
|
|
return isDigitSeparator(iCode, i);
|
2016-06-04 22:55:55 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('+',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator-(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('-',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator*(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('*',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator/(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('/',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator%(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('%',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator&(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('&',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator|(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('|',v1,v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator^(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return MathLib::value::calc('^',v1,v2);
|
|
|
|
}
|
2016-03-21 21:20:02 +01:00
|
|
|
|
|
|
|
MathLib::value operator<<(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return v1.shiftLeft(v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value operator>>(const MathLib::value &v1, const MathLib::value &v2)
|
|
|
|
{
|
|
|
|
return v1.shiftRight(v2);
|
|
|
|
}
|