Reuse code in isXNumber() to avoid duplication and unneeded computations

This commit is contained in:
Dmitry-Me 2014-10-21 16:02:35 +04:00
parent d5908f03b7
commit c3fa85b282
2 changed files with 39 additions and 32 deletions

View File

@ -8328,51 +8328,58 @@ void Tokenizer::cppcheckError(const Token *tok) const
printDebugOutput();
throw InternalError(tok, "Analysis failed. If the code is valid then please report this failure.", InternalError::INTERNAL);
}
/**
* Helper function to check whether number is equal to integer constant X
* or floating point pattern X.0
* @param s the string to check
* @param intConstant the integer constant to check against
* @param floatConstant the string with stringified float constant to check against
* @return true in case s is equal to X or X.0 and false otherwise.
*/
static bool isNumberOneOf(const std::string &s, const MathLib::bigint& intConstant, const char* floatConstant)
{
if (MathLib::isInt(s)) {
if (MathLib::toLongNumber(s) == intConstant)
return true;
} else if (MathLib::isFloat(s)) {
if (MathLib::toString(MathLib::toDoubleNumber(s)) == floatConstant)
return true;
}
return false;
}
// ------------------------------------------------------------------------
// Helper function to check wether number is zero (0 or 0.0 or 0E+0) or not?
// @param s --> a string to check
// Helper function to check whether number is zero (0 or 0.0 or 0E+0) or not?
// @param s the string to check
// @return true in case s is zero and false otherwise.
// ------------------------------------------------------------------------
bool Tokenizer::isZeroNumber(const std::string &s)
{
const bool isInteger = MathLib::isInt(s);
const bool isFloat = MathLib::isFloat(s);
const bool isZeroValue = ((isInteger && (MathLib::toLongNumber(s) == 0L)) // case: integer number
|| (isFloat && MathLib::toString(MathLib::toDoubleNumber(s)) == "0.0")); // case: float number
return isZeroValue;
return isNumberOneOf(s, 0L, "0.0");
}
// ------------------------------------------------------------------------
// Helper function to check wether number is one (1 or 0.1E+1 or 1E+0) or not?
// @param s --> a string to check
// Helper function to check whether number is one (1 or 0.1E+1 or 1E+0) or not?
// @param s the string to check
// @return true in case s is one and false otherwise.
// ------------------------------------------------------------------------
bool Tokenizer::isOneNumber(const std::string &s)
{
const bool isPositive = MathLib::isPositive(s);
const bool isInteger = MathLib::isInt(s);
const bool isFloat = MathLib::isFloat(s);
const bool isZeroValue = ((isPositive && isInteger && (MathLib::toLongNumber(s) == 1L)) // case: integer number
|| (isPositive && isFloat && MathLib::toString(MathLib::toDoubleNumber(s)) == "1.0")); // case: float number
return isZeroValue;
if (!MathLib::isPositive(s))
return false;
return isNumberOneOf(s, 1L, "1.0");
}
// ------------------------------------------------------------------------
// Helper function to check wether number is one (2 or 0.2E+1 or 2E+0) or not?
// @param s --> a string to check
// Helper function to check whether number is two (2 or 0.2E+1 or 2E+0) or not?
// @param s the string to check
// @return true in case s is two and false otherwise.
// ------------------------------------------------------------------------
bool Tokenizer::isTwoNumber(const std::string &s)
{
const bool isPositive = MathLib::isPositive(s);
const bool isInteger = MathLib::isInt(s);
const bool isFloat = MathLib::isFloat(s);
const bool isZeroValue = ((isPositive && isInteger && (MathLib::toLongNumber(s) == 2L)) // case: integer number
|| (isPositive && isFloat && MathLib::toString(MathLib::toDoubleNumber(s)) == "2.0")); // case: float number
return isZeroValue;
if (!MathLib::isPositive(s))
return false;
return isNumberOneOf(s, 2L, "2.0");
}
// ------------------------------------------------------

View File

@ -763,22 +763,22 @@ public:
static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
/**
* Helper function to check wether number is zero (0 or 0.0 or 0E+0) or not?
* @param s --> a string to check
* Helper function to check whether number is zero (0 or 0.0 or 0E+0) or not?
* @param s the string to check
* @return true in case is is zero and false otherwise.
*/
static bool isZeroNumber(const std::string &s);
/**
* Helper function to check wether number is one (1 or 0.1E+1 or 1E+0) or not?
* @param s --> a string to check
* Helper function to check whether number is one (1 or 0.1E+1 or 1E+0) or not?
* @param s the string to check
* @return true in case is is one and false otherwise.
*/
static bool isOneNumber(const std::string &s);
/**
* Helper function to check wether number is one (2 or 0.2E+1 or 2E+0) or not?
* @param s --> a string to check
* Helper function to check whether number is two (2 or 0.2E+1 or 2E+0) or not?
* @param s the string to check
* @return true in case is is two and false otherwise.
*/
static bool isTwoNumber(const std::string &s);