From 622663b8dbbb83d04e1493a03ce2200b491b855a Mon Sep 17 00:00:00 2001 From: Martin Ettl Date: Fri, 2 Apr 2010 22:41:54 +0200 Subject: [PATCH] added a helper function to mathlib::isNullValue(), it checks wheter a string has a null representation or not and returns a boolian result; extended nullchecks in testmathlib.cpp --- lib/mathlib.cpp | 16 ++++++++++++++-- lib/mathlib.h | 2 +- test/testmathlib.cpp | 8 ++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 18f055262..157f9e0bb 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -56,8 +56,7 @@ double MathLib::toDoubleNumber(const std::string &str) return std::strtoul(str.c_str(), '\0', 16); } // nullcheck - else if (str == "-0" || str == "-0.0" || str == "-0." - || str == "+0" || str == "+0.0" || str == "+0.") + else if (isNullValue(str)) return 0.0; // otherwise, convert to double std::istringstream istr(str.c_str()); @@ -298,6 +297,19 @@ bool MathLib::isGreater(const std::string &first, const std::string &second) return toDoubleNumber(first) > toDoubleNumber(second); } +bool MathLib::isNullValue(const std::string &str) +{ + return (str == "-0" || str == "-0.0" + || str == "-0." || str == "-0E-00" + || str == "-0E+00" || str == "+0E+00" + || str == "+0E-00" || str == "+0" + || str == "+0.0" || str == "+0." + || str == "0.0" || str == "-0e-00" + || str == "+0e+00" || str == "-0e+00" + || str == "+0e-00" || str == "-0e-00" + || str == "-0E-0" || str == "+0E-00"); +} + bool MathLib::isOctalDigit(char c) { if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7') diff --git a/lib/mathlib.h b/lib/mathlib.h index 6019f3e0f..2fd6a6deb 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -51,7 +51,7 @@ public: static std::string tan(const std::string & tok); static std::string abs(const std::string & tok); static bool isGreater(const std::string & first, const std::string & second); - + static bool isNullValue(const std::string &tok); /** * Return true if given character is 0,1,2,3,4,5,6 or 7. * @param c The character to check diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 866d242b6..dd4b66d52 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -128,7 +128,15 @@ private: ASSERT_EQUALS(1e+10 , MathLib::toDoubleNumber("+1.0E+10")); ASSERT_EQUALS(100.0 , MathLib::toDoubleNumber("1.0E+2")); ASSERT_EQUALS(1e+10 , MathLib::toDoubleNumber("1.0E+10")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E+0")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E-0")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E+00")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E-00")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0E+00")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("+0E-00")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0.")); + ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0.0")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("+0")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0."));