2009-05-30 07:48:12 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2019-02-09 07:24:06 +01:00
|
|
|
* Copyright (C) 2007-2019 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) :
|
2018-06-17 17:04:34 +02:00
|
|
|
mIntValue(0), mDoubleValue(0), mIsUnsigned(false)
|
2015-07-10 20:36:38 +02:00
|
|
|
{
|
|
|
|
if (MathLib::isFloat(s)) {
|
2018-06-17 17:04:34 +02:00
|
|
|
mType = MathLib::value::FLOAT;
|
|
|
|
mDoubleValue = MathLib::toDoubleNumber(s);
|
2015-07-10 20:36:38 +02:00
|
|
|
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
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
mType = MathLib::value::INT;
|
|
|
|
mIntValue = MathLib::toLongNumber(s);
|
2015-07-10 20:36:38 +02:00
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (isIntHex(s) && mIntValue < 0)
|
|
|
|
mIsUnsigned = true;
|
2016-03-18 10:30:57 +01:00
|
|
|
|
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) {
|
2018-04-04 21:51:31 +02:00
|
|
|
const char c = s[i];
|
2015-07-10 20:36:38 +02:00
|
|
|
if (c == 'u' || c == 'U')
|
2018-06-17 17:04:34 +02:00
|
|
|
mIsUnsigned = true;
|
2015-07-10 20:36:38 +02:00
|
|
|
else if (c == 'l' || c == 'L') {
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mType == MathLib::value::INT)
|
|
|
|
mType = MathLib::value::LONG;
|
|
|
|
else if (mType == MathLib::value::LONG)
|
|
|
|
mType = 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')
|
2018-06-17 17:04:34 +02:00
|
|
|
mType = MathLib::value::LONGLONG;
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MathLib::value::str() const
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mType == MathLib::value::FLOAT) {
|
|
|
|
if (ISNAN(mDoubleValue))
|
2015-07-10 20:36:38 +02:00
|
|
|
return "nan.0";
|
2018-06-17 17:04:34 +02:00
|
|
|
if (ISINF(mDoubleValue))
|
|
|
|
return (mDoubleValue > 0) ? "inf.0" : "-inf.0";
|
2015-07-14 20:48:08 +02:00
|
|
|
|
2015-07-10 20:36:38 +02:00
|
|
|
ostr.precision(9);
|
2018-06-17 17:04:34 +02:00
|
|
|
ostr << std::fixed << mDoubleValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mIsUnsigned)
|
|
|
|
ostr << static_cast<biguint>(mIntValue) << "U";
|
2016-03-18 10:30:57 +01:00
|
|
|
else
|
2018-06-17 17:04:34 +02:00
|
|
|
ostr << mIntValue;
|
|
|
|
if (mType == MathLib::value::LONG)
|
2015-07-10 20:36:38 +02:00
|
|
|
ostr << "L";
|
2018-06-17 17:04:34 +02:00
|
|
|
else if (mType == MathLib::value::LONGLONG)
|
2015-07-10 20:36:38 +02:00
|
|
|
ostr << "LL";
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathLib::value::promote(const MathLib::value &v)
|
|
|
|
{
|
|
|
|
if (isInt() && v.isInt()) {
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mType < v.mType) {
|
|
|
|
mType = v.mType;
|
|
|
|
mIsUnsigned = v.mIsUnsigned;
|
|
|
|
} else if (mType == v.mType) {
|
|
|
|
mIsUnsigned |= v.mIsUnsigned;
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
} else if (!isFloat()) {
|
2018-06-17 17:04:34 +02:00
|
|
|
mIsUnsigned = false;
|
|
|
|
mDoubleValue = mIntValue;
|
|
|
|
mType = MathLib::value::FLOAT;
|
2015-07-10 20:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 '+':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue += v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '-':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue -= v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '*':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue *= v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '/':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue /= v2.getDoubleValue();
|
2015-07-10 20:36:38 +02:00
|
|
|
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
|
|
|
}
|
2018-06-17 17:04:34 +02:00
|
|
|
} else if (temp.mIsUnsigned) {
|
2015-07-10 20:36:38 +02:00
|
|
|
switch (op) {
|
|
|
|
case '+':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue += (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '-':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue -= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '*':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue *= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '/':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v1.mIntValue == std::numeric_limits<bigint>::min() && std::abs(v2.mIntValue)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue /= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '%':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue %= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '&':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue &= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '|':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue |= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '^':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue ^= (unsigned long long)v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
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 '+':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue += v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '-':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue -= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '*':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue *= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '/':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v1.mIntValue == std::numeric_limits<bigint>::min() && std::abs(v2.mIntValue)<=1)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division overflow");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue /= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '%':
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v2.mIntValue == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
throw InternalError(nullptr, "Internal Error: Division by zero");
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue %= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '&':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue &= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '|':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue |= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
break;
|
|
|
|
case '^':
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue ^= v2.mIntValue;
|
2015-07-10 20:36:38 +02:00
|
|
|
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()) {
|
2018-06-17 17:04:34 +02:00
|
|
|
if (temp.mDoubleValue < v.getDoubleValue())
|
2015-07-10 20:36:38 +02:00
|
|
|
return -1;
|
2018-06-17 17:04:34 +02:00
|
|
|
if (temp.mDoubleValue > v.getDoubleValue())
|
2015-07-10 20:36:38 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (temp.mIsUnsigned) {
|
|
|
|
if ((unsigned long long)mIntValue < (unsigned long long)v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return -1;
|
2018-06-17 17:04:34 +02:00
|
|
|
if ((unsigned long long)mIntValue > (unsigned long long)v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mIntValue < v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return -1;
|
2018-06-17 17:04:34 +02:00
|
|
|
if (mIntValue > v.mIntValue)
|
2015-07-10 20:36:38 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MathLib::value MathLib::value::add(int v) const
|
|
|
|
{
|
|
|
|
MathLib::value temp(*this);
|
|
|
|
if (temp.isInt())
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mIntValue += v;
|
2015-07-10 20:36:38 +02:00
|
|
|
else
|
2018-06-17 17:04:34 +02:00
|
|
|
temp.mDoubleValue += v;
|
2015-07-10 20:36:38 +02:00
|
|
|
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);
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v.mIntValue >= MathLib::bigint_bits) {
|
2017-10-21 21:04:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2018-06-17 17:04:34 +02:00
|
|
|
ret.mIntValue <<= v.mIntValue;
|
2016-03-21 21:20:02 +01:00
|
|
|
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);
|
2018-06-17 17:04:34 +02:00
|
|
|
if (v.mIntValue >= MathLib::bigint_bits) {
|
2017-10-21 21:04:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2018-06-17 17:04:34 +02:00
|
|
|
ret.mIntValue >>= v.mIntValue;
|
2016-03-21 21:20:02 +01:00
|
|
|
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++) {
|
2020-01-12 08:11:12 +01:00
|
|
|
if (str[i] != '1' && str[i] != '0')
|
|
|
|
break;
|
2014-04-15 06:32:25 +02:00
|
|
|
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)
|
|
|
|
{
|
2018-03-30 20:57:25 +02:00
|
|
|
unsigned int retval = 0;
|
2019-09-19 20:29:33 +02:00
|
|
|
for (char it : str) {
|
|
|
|
retval = (retval << 8) | it;
|
2015-11-28 16:37:26 +01:00
|
|
|
}
|
|
|
|
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])) {
|
2019-06-30 21:39:22 +02:00
|
|
|
return (char)std::strtoul(str.substr(1).c_str(), nullptr, 8);
|
2016-10-16 13:42:20 +02:00
|
|
|
}
|
|
|
|
|
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++) {
|
2020-01-12 08:11:12 +01:00
|
|
|
if (str[i] != '1' && str[i] != '0')
|
|
|
|
break;
|
2014-04-15 06:32:25 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-16 11:41:33 +02:00
|
|
|
if (isCharLiteral(str)) {
|
|
|
|
return characterLiteralToLongNumber(getCharLiteral(str));
|
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
|
|
|
}
|
|
|
|
|
2018-12-06 22:16:16 +01:00
|
|
|
// in-place conversion of (sub)string to double. Requires no heap.
|
|
|
|
static double myStod(const std::string& str, std::string::const_iterator from, std::string::const_iterator to, int base)
|
|
|
|
{
|
|
|
|
double result = 0.;
|
|
|
|
bool positivesign = true;
|
|
|
|
std::string::const_iterator it;
|
|
|
|
if ('+' == *from) {
|
|
|
|
it = from + 1;
|
|
|
|
} else if ('-' == *from) {
|
|
|
|
it = from + 1;
|
|
|
|
positivesign = false;
|
|
|
|
} else
|
|
|
|
it = from;
|
|
|
|
const std::size_t decimalsep = str.find('.', it-str.begin());
|
|
|
|
int distance;
|
|
|
|
if (std::string::npos == decimalsep) {
|
|
|
|
distance = to - it;
|
|
|
|
} else if (decimalsep > (to - str.begin()))
|
|
|
|
return 0.; // error handling??
|
|
|
|
else
|
|
|
|
distance = int(decimalsep)-(from - str.begin());
|
|
|
|
auto digitval = [&](char c) {
|
|
|
|
if ((10 < base) && (c > '9'))
|
|
|
|
return 10 + std::tolower(c) - 'a';
|
|
|
|
else
|
|
|
|
return c - '0';
|
|
|
|
};
|
|
|
|
for (; it!=to; ++it) {
|
|
|
|
if ('.' == *it)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
--distance;
|
|
|
|
result += digitval(*it)* std::pow(base, distance);
|
|
|
|
}
|
|
|
|
return (positivesign)?result:-result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Assuming a limited support of built-in hexadecimal floats (see C99, C++17) that is a fall-back implementation.
|
|
|
|
// Performance has been optimized WRT to heap activity, however the calculation part is not optimized.
|
2019-11-20 15:37:09 +01:00
|
|
|
static double floatHexToDoubleNumber(const std::string& str)
|
2018-12-06 22:16:16 +01:00
|
|
|
{
|
|
|
|
const std::size_t p = str.find_first_of("pP",3);
|
|
|
|
const double factor1 = myStod(str, str.begin() + 2, str.begin()+p, 16);
|
|
|
|
const bool suffix = (str.back() == 'f') || (str.back() == 'F') || (str.back() == 'l') || (str.back() == 'L');
|
|
|
|
const double exponent = myStod(str, str.begin() + p + 1, (suffix)?str.end()-1:str.end(), 10);
|
|
|
|
const double factor2 = std::pow(2, exponent);
|
|
|
|
return factor1 * factor2;
|
|
|
|
}
|
2014-04-14 20:30:38 +02:00
|
|
|
|
2009-06-02 22:32:58 +02:00
|
|
|
double MathLib::toDoubleNumber(const std::string &str)
|
|
|
|
{
|
2019-10-16 11:41:33 +02:00
|
|
|
if (isCharLiteral(str))
|
|
|
|
return characterLiteralToLongNumber(getCharLiteral(str));
|
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
|
2018-12-06 22:16:16 +01:00
|
|
|
if (isFloatHex(str))
|
2019-11-20 15:37:09 +01:00
|
|
|
return floatHexToDoubleNumber(str);
|
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;
|
2019-07-17 10:43:18 +02:00
|
|
|
enum class State {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, BASE_DIGITS1, LEADING_DECIMAL, TRAILING_DECIMAL, BASE_DIGITS2, E, MANTISSA_PLUSMINUS, MANTISSA_DIGITS, SUFFIX_F, SUFFIX_L
|
2019-07-17 10:43:18 +02:00
|
|
|
} state = State::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-02-24 18:52:31 +01:00
|
|
|
switch (state) {
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::START:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='.')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::LEADING_DECIMAL;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::BASE_DIGITS1;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::LEADING_DECIMAL:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::BASE_DIGITS2;
|
2014-02-27 16:08:49 +01:00
|
|
|
else
|
|
|
|
return false;
|
2014-02-24 18:52:31 +01:00
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::BASE_DIGITS1:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::E;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='.')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = 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;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::TRAILING_DECIMAL:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::E;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='f' || *it=='F')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_L;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::BASE_DIGITS2;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::BASE_DIGITS2:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='e' || *it=='E')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::E;
|
2014-02-24 18:52:31 +01:00
|
|
|
else if (*it=='f' || *it=='F')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = 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;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::E:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='+' || *it=='-')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::MANTISSA_PLUSMINUS;
|
2015-11-02 21:30:28 +01:00
|
|
|
else if (std::isdigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::MANTISSA_DIGITS;
|
2014-02-24 18:52:31 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::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
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::MANTISSA_DIGITS;
|
2014-02-24 18:52:31 +01:00
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::MANTISSA_DIGITS:
|
2014-02-24 18:52:31 +01:00
|
|
|
if (*it=='f' || *it=='F')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = State::SUFFIX_F;
|
2014-11-17 06:53:32 +01:00
|
|
|
else if (*it=='l' || *it=='L')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = 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;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::SUFFIX_F:
|
2014-02-20 16:56:49 +01:00
|
|
|
return false;
|
2019-07-17 10:43:18 +02:00
|
|
|
case State::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
|
|
|
}
|
2019-07-17 10:43:18 +02:00
|
|
|
return (state==State::BASE_DIGITS2 || state==State::MANTISSA_DIGITS || state==State::TRAILING_DECIMAL || state==State::SUFFIX_F || state==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
|
|
|
}
|
|
|
|
|
2019-11-20 15:37:09 +01:00
|
|
|
static bool isValidIntegerSuffixIt(std::string::const_iterator it, std::string::const_iterator end, bool supportMicrosoftExtensions=true)
|
2018-12-20 12:20:31 +01: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;
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
switch (state) {
|
|
|
|
case START:
|
|
|
|
if (*it == 'u' || *it == 'U')
|
|
|
|
state = SUFFIX_U;
|
|
|
|
else if (*it == 'l' || *it == 'L')
|
|
|
|
state = SUFFIX_L;
|
|
|
|
else if (supportMicrosoftExtensions && (*it == 'i' || *it == 'I'))
|
|
|
|
state = SUFFIX_I;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SUFFIX_U:
|
|
|
|
if (*it == 'l' || *it == 'L')
|
|
|
|
state = SUFFIX_UL; // UL
|
|
|
|
else if (supportMicrosoftExtensions && (*it == 'i' || *it == 'I'))
|
|
|
|
state = SUFFIX_UI;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SUFFIX_UL:
|
|
|
|
if (*it == 'l' || *it == 'L')
|
|
|
|
state = SUFFIX_ULL; // ULL
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SUFFIX_L:
|
|
|
|
if (*it == 'u' || *it == 'U')
|
|
|
|
state = SUFFIX_LU; // LU
|
|
|
|
else if (*it == 'l' || *it == 'L')
|
|
|
|
state = SUFFIX_LL; // LL
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case SUFFIX_LU:
|
|
|
|
return false;
|
|
|
|
case SUFFIX_LL:
|
|
|
|
if (*it == 'u' || *it == 'U')
|
|
|
|
state = SUFFIX_LLU; // LLU
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) ||
|
|
|
|
(state == SUFFIX_UI64));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MathLib::isValidIntegerSuffix(const std::string& str, bool supportMicrosoftExtensions)
|
|
|
|
{
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(str.begin(), str.end(), supportMicrosoftExtensions);
|
2018-12-20 12:20:31 +01: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
|
|
|
{
|
2019-07-17 10:43:18 +02:00
|
|
|
enum class Status {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, OCTAL_PREFIX, DIGITS
|
2019-07-17 10:43:18 +02:00
|
|
|
} state = Status::START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-03-09 21:14:14 +01:00
|
|
|
switch (state) {
|
2019-07-17 10:43:18 +02:00
|
|
|
case Status::START:
|
2014-03-09 21:14:14 +01:00
|
|
|
if (*it == '0')
|
2019-07-17 10:43:18 +02:00
|
|
|
state = Status::OCTAL_PREFIX;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case Status::OCTAL_PREFIX:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isOctalDigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = Status::DIGITS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2019-07-17 10:43:18 +02:00
|
|
|
case Status::DIGITS:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isOctalDigit(static_cast<unsigned char>(*it)))
|
2019-07-17 10:43:18 +02:00
|
|
|
state = Status::DIGITS;
|
2014-03-09 21:14:14 +01:00
|
|
|
else
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(it,str.end());
|
2014-03-09 21:14:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 10:43:18 +02:00
|
|
|
return state == Status::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 {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, HEX_0, HEX_X, DIGIT
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; 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 == '0')
|
2018-12-10 12:10:26 +01:00
|
|
|
state = HEX_0;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-10 12:10:26 +01:00
|
|
|
case HEX_0:
|
2014-03-11 20:02:04 +01:00
|
|
|
if (*it == 'x' || *it == 'X')
|
2018-12-10 12:10:26 +01:00
|
|
|
state = HEX_X;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-10 12:10:26 +01:00
|
|
|
case HEX_X:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2018-12-10 12:10:26 +01:00
|
|
|
state = DIGIT;
|
2014-03-11 20:02:04 +01:00
|
|
|
else
|
|
|
|
return false;
|
2018-12-19 14:39:04 +01:00
|
|
|
break;
|
2018-12-10 12:10:26 +01:00
|
|
|
case DIGIT:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2018-12-19 14:39:04 +01:00
|
|
|
; // state = DIGIT;
|
2014-03-14 11:46:41 +01:00
|
|
|
else
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(it,str.end());
|
2014-03-14 11:46:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 12:10:26 +01:00
|
|
|
return DIGIT==state;
|
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 {
|
2018-12-06 22:16:16 +01:00
|
|
|
START, HEX_0, HEX_X, WHOLE_NUMBER_DIGIT, POINT, FRACTION, EXPONENT_P, EXPONENT_SIGN, EXPONENT_DIGITS, EXPONENT_SUFFIX
|
2015-07-26 08:06:56 +02:00
|
|
|
} state = START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2015-07-26 08:06:56 +02:00
|
|
|
switch (state) {
|
|
|
|
case START:
|
|
|
|
if (*it == '0')
|
2018-12-06 22:16:16 +01:00
|
|
|
state = HEX_0;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-06 22:16:16 +01:00
|
|
|
case HEX_0:
|
2015-07-26 08:06:56 +02:00
|
|
|
if (*it == 'x' || *it == 'X')
|
2018-12-06 22:16:16 +01:00
|
|
|
state = HEX_X;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-06 22:16:16 +01:00
|
|
|
case HEX_X:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2018-12-06 22:16:16 +01:00
|
|
|
state = WHOLE_NUMBER_DIGIT;
|
|
|
|
else if (*it == '.')
|
|
|
|
state = POINT;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-06 22:16:16 +01:00
|
|
|
case WHOLE_NUMBER_DIGIT:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2018-12-06 22:16:16 +01:00
|
|
|
; // state = WHOLE_NUMBER_DIGITS;
|
2015-07-26 08:06:56 +02:00
|
|
|
else if (*it=='.')
|
|
|
|
state = FRACTION;
|
|
|
|
else if (*it=='p' || *it=='P')
|
2018-12-06 22:16:16 +01:00
|
|
|
state = EXPONENT_P;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-06 22:16:16 +01:00
|
|
|
case POINT:
|
2015-07-26 08:06:56 +02:00
|
|
|
case FRACTION:
|
2015-11-02 21:30:28 +01:00
|
|
|
if (isxdigit(static_cast<unsigned char>(*it)))
|
2018-12-06 22:16:16 +01:00
|
|
|
state=FRACTION;
|
|
|
|
else if (*it == 'p' || *it == 'P')
|
|
|
|
state = EXPONENT_P;
|
|
|
|
else
|
|
|
|
return false;
|
2015-07-26 08:06:56 +02:00
|
|
|
break;
|
2018-12-06 22:16:16 +01:00
|
|
|
case EXPONENT_P:
|
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2015-07-26 08:06:56 +02:00
|
|
|
state = EXPONENT_DIGITS;
|
2018-12-06 22:16:16 +01:00
|
|
|
else if (*it == '+' || *it == '-')
|
|
|
|
state = EXPONENT_SIGN;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case EXPONENT_SIGN:
|
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
2015-07-26 08:06:56 +02:00
|
|
|
state = EXPONENT_DIGITS;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case EXPONENT_DIGITS:
|
2018-12-06 22:16:16 +01:00
|
|
|
if (isdigit(static_cast<unsigned char>(*it)))
|
|
|
|
; // state = EXPONENT_DIGITS;
|
|
|
|
else if (*it == 'f' || *it == 'F' || *it == 'l' || *it == 'L')
|
|
|
|
state = EXPONENT_SUFFIX;
|
2015-07-26 08:06:56 +02:00
|
|
|
else
|
2018-12-06 22:16:16 +01:00
|
|
|
return false;
|
2015-07-26 08:06:56 +02:00
|
|
|
break;
|
2018-12-06 22:16:16 +01:00
|
|
|
case EXPONENT_SUFFIX:
|
|
|
|
return false;
|
2015-07-26 08:06:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 22:16:16 +01:00
|
|
|
return (EXPONENT_DIGITS==state) || (EXPONENT_SUFFIX == state);
|
2015-07-26 08:06:56 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, GNU_BIN_PREFIX_0, GNU_BIN_PREFIX_B, DIGIT
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; 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 == '0')
|
2018-12-10 12:10:26 +01:00
|
|
|
state = GNU_BIN_PREFIX_0;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-10 12:10:26 +01:00
|
|
|
case GNU_BIN_PREFIX_0:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == 'b' || *it == 'B')
|
2018-12-10 12:10:26 +01:00
|
|
|
state = GNU_BIN_PREFIX_B;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-10 12:10:26 +01:00
|
|
|
case GNU_BIN_PREFIX_B:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0' || *it == '1')
|
2018-12-10 12:10:26 +01:00
|
|
|
state = DIGIT;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
break;
|
2018-12-10 12:10:26 +01:00
|
|
|
case DIGIT:
|
2014-03-10 17:23:07 +01:00
|
|
|
if (*it == '0' || *it == '1')
|
2018-12-10 12:10:26 +01:00
|
|
|
; // state = DIGIT;
|
2014-03-10 17:23:07 +01:00
|
|
|
else
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(it,str.end());
|
2014-03-10 17:23:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 12:10:26 +01:00
|
|
|
return state == DIGIT;
|
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 {
|
2018-12-10 12:10:26 +01:00
|
|
|
START, DIGIT
|
2014-04-30 21:01:47 +02:00
|
|
|
} state = START;
|
2018-12-10 12:10:26 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
std::string::const_iterator it = str.begin();
|
|
|
|
if ('+' == *it || '-' == *it)
|
|
|
|
++it;
|
|
|
|
for (; it != str.end(); ++it) {
|
2014-04-10 22:46:55 +02:00
|
|
|
switch (state) {
|
2014-04-30 21:01:47 +02:00
|
|
|
case START:
|
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
|
2019-11-20 15:37:09 +01:00
|
|
|
return isValidIntegerSuffixIt(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) {
|
2018-05-29 13:24:48 +02:00
|
|
|
const char c = value[value.size() - i];
|
2016-07-26 13:19:28 +02:00
|
|
|
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
|
|
|
{
|
2019-05-28 08:49:59 +02:00
|
|
|
if (str.empty() || (!std::isdigit(static_cast<unsigned char>(str[0])) && (str[0] != '.' && str[0] != '-' && str[0] != '+')))
|
2014-04-14 05:27:38 +02:00
|
|
|
return false; // Has to be a number
|
|
|
|
|
2020-02-09 11:16:08 +01:00
|
|
|
if (!isInt(str) && !isFloat(str))
|
|
|
|
return false;
|
2020-02-02 16:40:36 +01:00
|
|
|
bool isHex = isIntHex(str) || isFloatHex(str);
|
2019-09-19 20:29:33 +02:00
|
|
|
for (char i : str) {
|
|
|
|
if (std::isdigit(static_cast<unsigned char>(i)) && i != '0') // May not contain digits other than 0
|
2014-04-11 12:00:33 +02:00
|
|
|
return false;
|
2020-02-02 16:40:36 +01:00
|
|
|
if (i == 'p' || i == 'P' || (!isHex && (i == 'E' || i == 'e')))
|
2014-04-14 05:27:38 +02:00
|
|
|
return true;
|
2020-02-02 16:40:36 +01:00
|
|
|
if (isHex && isxdigit(i) && i != '0')
|
|
|
|
return false;
|
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);
|
|
|
|
}
|