mathlib: Refactor to use enum class (#2872)

This commit is contained in:
Rikard Falkeborn 2020-11-01 11:47:34 +01:00 committed by GitHub
parent 26e6eed189
commit bd4dc364a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 103 deletions

View File

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

View File

@ -40,7 +40,7 @@ public:
private: private:
long long mIntValue; long long mIntValue;
double mDoubleValue; double mDoubleValue;
enum { INT, LONG, LONGLONG, FLOAT } mType; enum class Type { INT, LONG, LONGLONG, FLOAT } mType;
bool mIsUnsigned; bool mIsUnsigned;
void promote(const value &v); void promote(const value &v);
@ -49,10 +49,10 @@ public:
explicit value(const std::string &s); explicit value(const std::string &s);
std::string str() const; std::string str() const;
bool isInt() const { bool isInt() const {
return mType != FLOAT; return mType != Type::FLOAT;
} }
bool isFloat() const { bool isFloat() const {
return mType == FLOAT; return mType == Type::FLOAT;
} }
double getDoubleValue() const { double getDoubleValue() const {