sal/qa/rtl/math/test-rtl-math.cxx |   10 ++++++++++
 sal/rtl/math.cxx                  |   15 +++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)

New commits:
commit 81321560f2151ee12be26135aed65968287d1c94
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Sep 17 18:16:27 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu Sep 18 06:05:07 2025 +0200

    Simplify the check for "the dreaded rounded to 15 digits max value"
    
    The code that prepares the buffer for strtod_nolocale creates an
    invariant result; we will have 'E', not 'e'; we will not have '+'
    in the exponent.
    
    Having two checks is enough; with the strings of different length,
    this is as fast as possible.
    
    Change-Id: Id87f677a1ebf1004439d5975aacda5c85e78ceb2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191090
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Tested-by: Jenkins

diff --git a/sal/qa/rtl/math/test-rtl-math.cxx 
b/sal/qa/rtl/math/test-rtl-math.cxx
index 22714fdd4592..5d2488f2c712 100644
--- a/sal/qa/rtl/math/test-rtl-math.cxx
+++ b/sal/qa/rtl/math/test-rtl-math.cxx
@@ -240,6 +240,16 @@ public:
         CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
         CPPUNIT_ASSERT_EQUAL(sal_Int32(21), end);
         CPPUNIT_ASSERT_EQUAL(0x1p-1074, res);
+
+        res = rtl::math::stringToDouble("1.79769313486232E+308", '.', ',', 
&status, &end);
+        CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
+        CPPUNIT_ASSERT_EQUAL(sal_Int32(21), end);
+        CPPUNIT_ASSERT_EQUAL(DBL_MAX, res);
+
+        res = rtl::math::stringToDouble("-1.79769313486232e308", '.', ',', 
&status, &end);
+        CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
+        CPPUNIT_ASSERT_EQUAL(sal_Int32(21), end);
+        CPPUNIT_ASSERT_EQUAL(-DBL_MAX, res);
     }
 
     void test_stringToDouble_bad() {
diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index dbeef855e9c5..bc8aa7d32b85 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -378,15 +378,14 @@ double stringToDouble(CharT const* pBegin, CharT const* 
pEnd, CharT cDecSeparato
                     // Check for the dreaded rounded to 15 digits max value
                     // 1.79769313486232e+308 for 1.7976931348623157e+308 we 
wrote
                     // everywhere, accept with or without plus sign in 
exponent.
-                    const char* b = buf;
-                    if (b[0] == '-')
-                        ++b;
-                    if (((pCharParseEnd - b == 21) || (pCharParseEnd - b == 
20))
-                        && !strncmp(b, "1.79769313486232", 16) && (b[16] == 
'e' || b[16] == 'E')
-                        && (((pCharParseEnd - b == 21) && !strncmp(b + 17, 
"+308", 4))
-                            || ((pCharParseEnd - b == 20) && !strncmp(b + 17, 
"308", 3))))
+                    std::string_view num_view(buf, pCharParseEnd);
+                    if (num_view == "1.79769313486232E308")
                     {
-                        fVal = (buf < b) ? -DBL_MAX : DBL_MAX;
+                        fVal = DBL_MAX;
+                    }
+                    else if (num_view == "-1.79769313486232E308")
+                    {
+                        fVal = -DBL_MAX;
                     }
                     else
                     {

Reply via email to