sal/qa/rtl/math/test-rtl-math.cxx | 35 +++++++++++++++++++++++++++++++++++ sal/rtl/math.cxx | 13 +++++++++++++ 2 files changed, 48 insertions(+)
New commits: commit 2d6a88135ef107e5927c7a6cd2542ad1bc8bbe09 Author: Eike Rathke <[email protected]> Date: Mon Jun 27 21:59:06 2016 +0200 unit tests for stringToDouble() separator without digits Change-Id: I85cfe8123a6ef1c3a1aa1b1085e2961055dfa907 diff --git a/sal/qa/rtl/math/test-rtl-math.cxx b/sal/qa/rtl/math/test-rtl-math.cxx index 58b27b0..7dd492d 100644 --- a/sal/qa/rtl/math/test-rtl-math.cxx +++ b/sal/qa/rtl/math/test-rtl-math.cxx @@ -86,6 +86,20 @@ public: CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_OutOfRange, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); CPPUNIT_ASSERT_EQUAL(rtl::math::isInf(res), true); + + res = rtl::math::stringToDouble( + rtl::OUString(".5"), + '.', ',', &status, &end); + CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); + CPPUNIT_ASSERT_EQUAL(0.5, res); + + res = rtl::math::stringToDouble( + rtl::OUString("5."), + '.', ',', &status, &end); + CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); + CPPUNIT_ASSERT_EQUAL(5.0, res); } void test_stringToDouble_bad() { @@ -97,6 +111,27 @@ public: CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); CPPUNIT_ASSERT_EQUAL(0.0, res); + + res = rtl::math::stringToDouble( + rtl::OUString("."), + '.', ',', &status, &end); + CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); + CPPUNIT_ASSERT_EQUAL(0.0, res); + + res = rtl::math::stringToDouble( + rtl::OUString(" +.Efoo"), + '.', ',', &status, &end); + CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); + CPPUNIT_ASSERT_EQUAL(0.0, res); + + res = rtl::math::stringToDouble( + rtl::OUString(" +,.Efoo"), + '.', ',', &status, &end); + CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); + CPPUNIT_ASSERT_EQUAL(0.0, res); } void test_stringToDouble_exponent_without_digit() { commit 9a6527a98fb968b3fe6bc293ff7520a9480d43d0 Author: Eike Rathke <[email protected]> Date: Mon Jun 27 21:54:59 2016 +0200 stringToDouble() do not parse separator without digit as 0.0 Occurred in CSV import without "detect special numbers" activated for data like ,., where the . dot resulted in a numeric cell value 0 Change-Id: Ie715d7a8ed02196b59968a92919ad286b3bedf64 diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx index 92de8b6..c694e58 100644 --- a/sal/rtl/math.cxx +++ b/sal/rtl/math.cxx @@ -694,6 +694,7 @@ inline double stringToDouble(CharT const * pBegin, CharT const * pEnd, while (p != pEnd && (*p == CharT('0') || *p == cGroupSeparator)) ++p; + CharT const * pFirstSignificant = p; long nValExp = 0; // carry along exponent of mantissa // integer part of mantissa @@ -741,7 +742,19 @@ inline double stringToDouble(CharT const * pBegin, CharT const * pEnd, if ( fFrac != 0.0 ) fVal += rtl::math::pow10Exp( fFrac, nFracExp ); else if ( nValExp < 0 ) + { + if (pFirstSignificant + 1 == p) + { + // No digit at all, only separator(s) without integer or + // fraction part. Bail out. No number. No error. + if (pStatus != nullptr) + *pStatus = eStatus; + if (pParsedEnd != nullptr) + *pParsedEnd = pBegin; + return fVal; + } nValExp = 0; // no digit other than 0 after decimal point + } } if ( nValExp > 0 ) _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
