mathlib: Refactor to use enum class (#2872)
This commit is contained in:
parent
26e6eed189
commit
bd4dc364a7
200
lib/mathlib.cpp
200
lib/mathlib.cpp
|
@ -44,7 +44,7 @@ MathLib::value::value(const std::string &s) :
|
|||
mIntValue(0), mDoubleValue(0), mIsUnsigned(false)
|
||||
{
|
||||
if (MathLib::isFloat(s)) {
|
||||
mType = MathLib::value::FLOAT;
|
||||
mType = MathLib::value::Type::FLOAT;
|
||||
mDoubleValue = MathLib::toDoubleNumber(s);
|
||||
return;
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ MathLib::value::value(const std::string &s) :
|
|||
if (!MathLib::isInt(s))
|
||||
throw InternalError(nullptr, "Invalid value: " + s);
|
||||
|
||||
mType = MathLib::value::INT;
|
||||
mType = MathLib::value::Type::INT;
|
||||
mIntValue = MathLib::toLongNumber(s);
|
||||
|
||||
if (isIntHex(s) && mIntValue < 0)
|
||||
|
@ -65,12 +65,12 @@ MathLib::value::value(const std::string &s) :
|
|||
if (c == 'u' || c == 'U')
|
||||
mIsUnsigned = true;
|
||||
else if (c == 'l' || c == 'L') {
|
||||
if (mType == MathLib::value::INT)
|
||||
mType = MathLib::value::LONG;
|
||||
else if (mType == MathLib::value::LONG)
|
||||
mType = MathLib::value::LONGLONG;
|
||||
if (mType == MathLib::value::Type::INT)
|
||||
mType = MathLib::value::Type::LONG;
|
||||
else if (mType == MathLib::value::Type::LONG)
|
||||
mType = MathLib::value::Type::LONGLONG;
|
||||
} else if (i > 2U && c == '4' && s[i-1] == '6' && s[i-2] == 'i')
|
||||
mType = MathLib::value::LONGLONG;
|
||||
mType = MathLib::value::Type::LONGLONG;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ MathLib::value::value(const std::string &s) :
|
|||
std::string MathLib::value::str() const
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
if (mType == MathLib::value::FLOAT) {
|
||||
if (mType == MathLib::value::Type::FLOAT) {
|
||||
if (ISNAN(mDoubleValue))
|
||||
return "nan.0";
|
||||
if (ISINF(mDoubleValue))
|
||||
|
@ -102,9 +102,9 @@ std::string MathLib::value::str() const
|
|||
ostr << static_cast<biguint>(mIntValue) << "U";
|
||||
else
|
||||
ostr << mIntValue;
|
||||
if (mType == MathLib::value::LONG)
|
||||
if (mType == MathLib::value::Type::LONG)
|
||||
ostr << "L";
|
||||
else if (mType == MathLib::value::LONGLONG)
|
||||
else if (mType == MathLib::value::Type::LONGLONG)
|
||||
ostr << "LL";
|
||||
return ostr.str();
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ void MathLib::value::promote(const MathLib::value &v)
|
|||
} else if (!isFloat()) {
|
||||
mIsUnsigned = false;
|
||||
mDoubleValue = mIntValue;
|
||||
mType = MathLib::value::FLOAT;
|
||||
mType = MathLib::value::Type::FLOAT;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -746,70 +746,70 @@ bool MathLib::isPositive(const std::string &str)
|
|||
|
||||
static bool isValidIntegerSuffixIt(std::string::const_iterator it, std::string::const_iterator end, bool supportMicrosoftExtensions=true)
|
||||
{
|
||||
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;
|
||||
enum class Status { 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 = Status::START;
|
||||
for (; it != end; ++it) {
|
||||
switch (state) {
|
||||
case START:
|
||||
case Status::START:
|
||||
if (*it == 'u' || *it == 'U')
|
||||
state = SUFFIX_U;
|
||||
state = Status::SUFFIX_U;
|
||||
else if (*it == 'l' || *it == 'L')
|
||||
state = SUFFIX_L;
|
||||
state = Status::SUFFIX_L;
|
||||
else if (supportMicrosoftExtensions && (*it == 'i' || *it == 'I'))
|
||||
state = SUFFIX_I;
|
||||
state = Status::SUFFIX_I;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_U:
|
||||
case Status::SUFFIX_U:
|
||||
if (*it == 'l' || *it == 'L')
|
||||
state = SUFFIX_UL; // UL
|
||||
state = Status::SUFFIX_UL; // UL
|
||||
else if (supportMicrosoftExtensions && (*it == 'i' || *it == 'I'))
|
||||
state = SUFFIX_UI;
|
||||
state = Status::SUFFIX_UI;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_UL:
|
||||
case Status::SUFFIX_UL:
|
||||
if (*it == 'l' || *it == 'L')
|
||||
state = SUFFIX_ULL; // ULL
|
||||
state = Status::SUFFIX_ULL; // ULL
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_L:
|
||||
case Status::SUFFIX_L:
|
||||
if (*it == 'u' || *it == 'U')
|
||||
state = SUFFIX_LU; // LU
|
||||
state = Status::SUFFIX_LU; // LU
|
||||
else if (*it == 'l' || *it == 'L')
|
||||
state = SUFFIX_LL; // LL
|
||||
state = Status::SUFFIX_LL; // LL
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_LU:
|
||||
case Status::SUFFIX_LU:
|
||||
return false;
|
||||
case SUFFIX_LL:
|
||||
case Status::SUFFIX_LL:
|
||||
if (*it == 'u' || *it == 'U')
|
||||
state = SUFFIX_LLU; // LLU
|
||||
state = Status::SUFFIX_LLU; // LLU
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_I:
|
||||
case Status::SUFFIX_I:
|
||||
if (*it == '6')
|
||||
state = SUFFIX_I6;
|
||||
state = Status::SUFFIX_I6;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_I6:
|
||||
case Status::SUFFIX_I6:
|
||||
if (*it == '4')
|
||||
state = SUFFIX_I64;
|
||||
state = Status::SUFFIX_I64;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_UI:
|
||||
case Status::SUFFIX_UI:
|
||||
if (*it == '6')
|
||||
state = SUFFIX_UI6;
|
||||
state = Status::SUFFIX_UI6;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case SUFFIX_UI6:
|
||||
case Status::SUFFIX_UI6:
|
||||
if (*it == '4')
|
||||
state = SUFFIX_UI64;
|
||||
state = Status::SUFFIX_UI64;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
|
@ -817,15 +817,15 @@ static bool isValidIntegerSuffixIt(std::string::const_iterator it, std::string::
|
|||
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));
|
||||
return ((state == Status::SUFFIX_U) ||
|
||||
(state == Status::SUFFIX_L) ||
|
||||
(state == Status::SUFFIX_UL) ||
|
||||
(state == Status::SUFFIX_LU) ||
|
||||
(state == Status::SUFFIX_LL) ||
|
||||
(state == Status::SUFFIX_ULL) ||
|
||||
(state == Status::SUFFIX_LLU) ||
|
||||
(state == Status::SUFFIX_I64) ||
|
||||
(state == Status::SUFFIX_UI64));
|
||||
}
|
||||
|
||||
bool MathLib::isValidIntegerSuffix(const std::string& str, bool supportMicrosoftExtensions)
|
||||
|
@ -881,9 +881,9 @@ bool MathLib::isOct(const std::string& str)
|
|||
|
||||
bool MathLib::isIntHex(const std::string& str)
|
||||
{
|
||||
enum Status {
|
||||
enum class Status {
|
||||
START, HEX_0, HEX_X, DIGIT
|
||||
} state = START;
|
||||
} state = Status::START;
|
||||
if (str.empty())
|
||||
return false;
|
||||
std::string::const_iterator it = str.begin();
|
||||
|
@ -891,40 +891,40 @@ bool MathLib::isIntHex(const std::string& str)
|
|||
++it;
|
||||
for (; it != str.end(); ++it) {
|
||||
switch (state) {
|
||||
case START:
|
||||
case Status::START:
|
||||
if (*it == '0')
|
||||
state = HEX_0;
|
||||
state = Status::HEX_0;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case HEX_0:
|
||||
case Status::HEX_0:
|
||||
if (*it == 'x' || *it == 'X')
|
||||
state = HEX_X;
|
||||
state = Status::HEX_X;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case HEX_X:
|
||||
case Status::HEX_X:
|
||||
if (isxdigit(static_cast<unsigned char>(*it)))
|
||||
state = DIGIT;
|
||||
state = Status::DIGIT;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case DIGIT:
|
||||
case Status::DIGIT:
|
||||
if (isxdigit(static_cast<unsigned char>(*it)))
|
||||
; // state = DIGIT;
|
||||
; // state = Status::DIGIT;
|
||||
else
|
||||
return isValidIntegerSuffixIt(it,str.end());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return DIGIT==state;
|
||||
return Status::DIGIT == state;
|
||||
}
|
||||
|
||||
bool MathLib::isFloatHex(const std::string& str)
|
||||
{
|
||||
enum Status {
|
||||
enum class Status {
|
||||
START, HEX_0, HEX_X, WHOLE_NUMBER_DIGIT, POINT, FRACTION, EXPONENT_P, EXPONENT_SIGN, EXPONENT_DIGITS, EXPONENT_SUFFIX
|
||||
} state = START;
|
||||
} state = Status::START;
|
||||
if (str.empty())
|
||||
return false;
|
||||
std::string::const_iterator it = str.begin();
|
||||
|
@ -932,72 +932,72 @@ bool MathLib::isFloatHex(const std::string& str)
|
|||
++it;
|
||||
for (; it != str.end(); ++it) {
|
||||
switch (state) {
|
||||
case START:
|
||||
case Status::START:
|
||||
if (*it == '0')
|
||||
state = HEX_0;
|
||||
state = Status::HEX_0;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case HEX_0:
|
||||
case Status::HEX_0:
|
||||
if (*it == 'x' || *it == 'X')
|
||||
state = HEX_X;
|
||||
state = Status::HEX_X;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case HEX_X:
|
||||
case Status::HEX_X:
|
||||
if (isxdigit(static_cast<unsigned char>(*it)))
|
||||
state = WHOLE_NUMBER_DIGIT;
|
||||
state = Status::WHOLE_NUMBER_DIGIT;
|
||||
else if (*it == '.')
|
||||
state = POINT;
|
||||
state = Status::POINT;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case WHOLE_NUMBER_DIGIT:
|
||||
case Status::WHOLE_NUMBER_DIGIT:
|
||||
if (isxdigit(static_cast<unsigned char>(*it)))
|
||||
; // state = WHOLE_NUMBER_DIGITS;
|
||||
; // state = Status::WHOLE_NUMBER_DIGITS;
|
||||
else if (*it=='.')
|
||||
state = FRACTION;
|
||||
state = Status::FRACTION;
|
||||
else if (*it=='p' || *it=='P')
|
||||
state = EXPONENT_P;
|
||||
state = Status::EXPONENT_P;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case POINT:
|
||||
case FRACTION:
|
||||
case Status::POINT:
|
||||
case Status::FRACTION:
|
||||
if (isxdigit(static_cast<unsigned char>(*it)))
|
||||
state=FRACTION;
|
||||
state = Status::FRACTION;
|
||||
else if (*it == 'p' || *it == 'P')
|
||||
state = EXPONENT_P;
|
||||
state = Status::EXPONENT_P;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case EXPONENT_P:
|
||||
case Status::EXPONENT_P:
|
||||
if (isdigit(static_cast<unsigned char>(*it)))
|
||||
state = EXPONENT_DIGITS;
|
||||
state = Status::EXPONENT_DIGITS;
|
||||
else if (*it == '+' || *it == '-')
|
||||
state = EXPONENT_SIGN;
|
||||
state = Status::EXPONENT_SIGN;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case EXPONENT_SIGN:
|
||||
case Status::EXPONENT_SIGN:
|
||||
if (isdigit(static_cast<unsigned char>(*it)))
|
||||
state = EXPONENT_DIGITS;
|
||||
state = Status::EXPONENT_DIGITS;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case EXPONENT_DIGITS:
|
||||
case Status::EXPONENT_DIGITS:
|
||||
if (isdigit(static_cast<unsigned char>(*it)))
|
||||
; // state = EXPONENT_DIGITS;
|
||||
; // state = Status::EXPONENT_DIGITS;
|
||||
else if (*it == 'f' || *it == 'F' || *it == 'l' || *it == 'L')
|
||||
state = EXPONENT_SUFFIX;
|
||||
state = Status::EXPONENT_SUFFIX;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case EXPONENT_SUFFIX:
|
||||
case Status::EXPONENT_SUFFIX:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (EXPONENT_DIGITS==state) || (EXPONENT_SUFFIX == state);
|
||||
return (Status::EXPONENT_DIGITS == state) || (Status::EXPONENT_SUFFIX == state);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1012,9 +1012,9 @@ bool MathLib::isFloatHex(const std::string& str)
|
|||
**/
|
||||
bool MathLib::isBin(const std::string& str)
|
||||
{
|
||||
enum Status {
|
||||
enum class Status {
|
||||
START, GNU_BIN_PREFIX_0, GNU_BIN_PREFIX_B, DIGIT
|
||||
} state = START;
|
||||
} state = Status::START;
|
||||
if (str.empty())
|
||||
return false;
|
||||
std::string::const_iterator it = str.begin();
|
||||
|
@ -1022,40 +1022,40 @@ bool MathLib::isBin(const std::string& str)
|
|||
++it;
|
||||
for (; it != str.end(); ++it) {
|
||||
switch (state) {
|
||||
case START:
|
||||
case Status::START:
|
||||
if (*it == '0')
|
||||
state = GNU_BIN_PREFIX_0;
|
||||
state = Status::GNU_BIN_PREFIX_0;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case GNU_BIN_PREFIX_0:
|
||||
case Status::GNU_BIN_PREFIX_0:
|
||||
if (*it == 'b' || *it == 'B')
|
||||
state = GNU_BIN_PREFIX_B;
|
||||
state = Status::GNU_BIN_PREFIX_B;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case GNU_BIN_PREFIX_B:
|
||||
case Status::GNU_BIN_PREFIX_B:
|
||||
if (*it == '0' || *it == '1')
|
||||
state = DIGIT;
|
||||
state = Status::DIGIT;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case DIGIT:
|
||||
case Status::DIGIT:
|
||||
if (*it == '0' || *it == '1')
|
||||
; // state = DIGIT;
|
||||
; // state = Status::DIGIT;
|
||||
else
|
||||
return isValidIntegerSuffixIt(it,str.end());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return state == DIGIT;
|
||||
return state == Status::DIGIT;
|
||||
}
|
||||
|
||||
bool MathLib::isDec(const std::string & str)
|
||||
{
|
||||
enum Status {
|
||||
enum class Status {
|
||||
START, DIGIT
|
||||
} state = START;
|
||||
} state = Status::START;
|
||||
if (str.empty())
|
||||
return false;
|
||||
std::string::const_iterator it = str.begin();
|
||||
|
@ -1063,21 +1063,21 @@ bool MathLib::isDec(const std::string & str)
|
|||
++it;
|
||||
for (; it != str.end(); ++it) {
|
||||
switch (state) {
|
||||
case START:
|
||||
case Status::START:
|
||||
if (isdigit(static_cast<unsigned char>(*it)))
|
||||
state = DIGIT;
|
||||
state = Status::DIGIT;
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
case DIGIT:
|
||||
case Status::DIGIT:
|
||||
if (isdigit(static_cast<unsigned char>(*it)))
|
||||
state = DIGIT;
|
||||
state = Status::DIGIT;
|
||||
else
|
||||
return isValidIntegerSuffixIt(it,str.end());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return state == DIGIT;
|
||||
return state == Status::DIGIT;
|
||||
}
|
||||
|
||||
bool MathLib::isInt(const std::string & str)
|
||||
|
|
|
@ -40,7 +40,7 @@ public:
|
|||
private:
|
||||
long long mIntValue;
|
||||
double mDoubleValue;
|
||||
enum { INT, LONG, LONGLONG, FLOAT } mType;
|
||||
enum class Type { INT, LONG, LONGLONG, FLOAT } mType;
|
||||
bool mIsUnsigned;
|
||||
|
||||
void promote(const value &v);
|
||||
|
@ -49,10 +49,10 @@ public:
|
|||
explicit value(const std::string &s);
|
||||
std::string str() const;
|
||||
bool isInt() const {
|
||||
return mType != FLOAT;
|
||||
return mType != Type::FLOAT;
|
||||
}
|
||||
bool isFloat() const {
|
||||
return mType == FLOAT;
|
||||
return mType == Type::FLOAT;
|
||||
}
|
||||
|
||||
double getDoubleValue() const {
|
||||
|
|
Loading…
Reference in New Issue