Fix some clang warnings about type conversions

This commit is contained in:
Alexander Mai 2014-04-11 23:35:53 +02:00
parent 9ac83d7624
commit 718e42f5ca
4 changed files with 51 additions and 54 deletions

View File

@ -205,7 +205,7 @@ static const Signaltype listofsignals[] = {
* \return size of array
* */
template<typename T, int size>
int GetArrayLength(const T(&)[size])
size_t GetArrayLength(const T(&)[size])
{
return size;
}

View File

@ -21,60 +21,10 @@
#include "mathlib.h"
#include "errorlogger.h"
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <limits>
MathLib::bigint MathLib::toLongNumber(const std::string &str)
{
// hexadecimal numbers:
if (isHex(str)) {
if (str[0] == '-') {
bigint ret = 0;
std::istringstream istr(str);
istr >> std::hex >> ret;
return ret;
} else {
unsigned long long ret = 0;
std::istringstream istr(str);
istr >> std::hex >> ret;
return (bigint)ret;
}
}
// octal numbers:
if (isOct(str)) {
bigint ret = 0;
std::istringstream istr(str);
istr >> std::oct >> ret;
return ret;
}
// 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))
return static_cast<bigint>(std::atof(str.c_str()));
bigint ret = 0;
std::istringstream istr(str);
istr >> ret;
return ret;
}
double MathLib::toDoubleNumber(const std::string &str)
{
if (isHex(str))

View File

@ -21,6 +21,7 @@
#define mathlibH
//---------------------------------------------------------------------------
#include <cstdlib>
#include <string>
#include <sstream>
#include "config.h"
@ -35,7 +36,53 @@ public:
typedef long long bigint;
typedef unsigned long long biguint;
static bigint toLongNumber(const std::string & str);
template < class T = bigint >
static T toLongNumber(const std::string & str) {
// hexadecimal numbers:
if (isHex(str)) {
if (str[0] == '-') {
T ret = 0;
std::istringstream istr(str);
istr >> std::hex >> ret;
return ret;
} else {
unsigned long long ret = 0;
std::istringstream istr(str);
istr >> std::hex >> ret;
return (T)ret;
}
}
// octal numbers:
if (isOct(str)) {
T ret = 0;
std::istringstream istr(str);
istr >> std::oct >> ret;
return ret;
}
// binary numbers:
if (isBin(str)) {
T 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))
return static_cast<T>(std::atof(str.c_str()));
T ret = 0;
std::istringstream istr(str);
istr >> ret;
return ret;
}
template<class T> static std::string toString(T value) {
std::ostringstream result;
result << value;

View File

@ -803,8 +803,8 @@ static bool isLowerEqualThanMulDiv(const Token* lower)
template <typename T>
std::string typeCorrectShift(const char cop, const Token* left, const Token* right)
{
const T leftInt=MathLib::toLongNumber(left->str());
const T rightInt=MathLib::toLongNumber(right->str());
const T leftInt=MathLib::toLongNumber<T>(left->str());
const T rightInt=MathLib::toLongNumber<T>(right->str());
if (cop == '&' || cop == '|' || cop == '^')
return MathLib::calculate(left->str(), right->str(), cop);