sc/source/core/inc/interpre.hxx  |    1 
 sc/source/core/tool/interpr4.cxx |   81 ++++++++++-----------------------------
 2 files changed, 23 insertions(+), 59 deletions(-)

New commits:
commit 168c17d4864922fac26cd8bfc7da4a119efb9fab
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Feb 5 11:49:02 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sat Feb 15 12:03:21 2025 +0100

    Implement GetUInt32 using double_to<sal_uInt32>
    
    This unifies the implementation with other conversions to integers.
    It is theoretically a behavior change, since it now treats numbers
    in the range of (-0.5;0) as 0, and succeeds, where it previously
    failed. But:
    
    (1) this shouldn't be a problem in practice;
    (2) this is consistent with the general spreadsheet convention of
        rounding when converting to integers.
    
    Change-Id: Ib2455d9b9a253dcf6e00cc7fafe61964aeb7ab33
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/181695
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Tested-by: Jenkins

diff --git a/sc/source/core/tool/interpr4.cxx b/sc/source/core/tool/interpr4.cxx
index 93dd5a7ac15f..814fe288d5bc 100644
--- a/sc/source/core/tool/interpr4.cxx
+++ b/sc/source/core/tool/interpr4.cxx
@@ -2260,18 +2260,7 @@ sal_Int16 ScInterpreter::GetInt16()
 
 sal_uInt32 ScInterpreter::GetUInt32()
 {
-    double fVal = rtl::math::approxFloor( GetDouble());
-    if (!std::isfinite(fVal))
-    {
-        SetError( GetDoubleErrorValue( fVal));
-        return SAL_MAX_UINT32;
-    }
-    if (fVal < 0.0 || fVal > SAL_MAX_UINT32)
-    {
-        SetError( FormulaError::IllegalArgument);
-        return SAL_MAX_UINT32;
-    }
-    return static_cast<sal_uInt32>(fVal);
+    return double_to<sal_uInt32>(GetDouble());
 }
 
 bool ScInterpreter::GetDoubleOrString( double& rDouble, svl::SharedString& 
rString )
commit 58a3233f5873fb40996682c35ef9c1e19e03ef4d
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Feb 5 11:49:00 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sat Feb 15 12:03:14 2025 +0100

    Deduplicate conversion to integers
    
    Change-Id: I225b304651048c6cc97397d8780e0463e3b5f984
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/181694
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sc/source/core/inc/interpre.hxx b/sc/source/core/inc/interpre.hxx
index c57d0e046df5..6fbbcfbe935b 100644
--- a/sc/source/core/inc/interpre.hxx
+++ b/sc/source/core/inc/interpre.hxx
@@ -488,6 +488,7 @@ private:
     double GetDouble();
     double GetDoubleWithDefault(double nDefault);
     bool IsMissing() const;
+    template <typename Int> requires std::is_integral_v<Int> Int 
double_to(double fVal);
     sal_Int32 double_to_int32(double fVal);
     /** if GetDouble() not within int32 limits sets nGlobalError and returns 
SAL_MAX_INT32 */
     sal_Int32 GetInt32();
diff --git a/sc/source/core/tool/interpr4.cxx b/sc/source/core/tool/interpr4.cxx
index a34c9e3b65bc..93dd5a7ac15f 100644
--- a/sc/source/core/tool/interpr4.cxx
+++ b/sc/source/core/tool/interpr4.cxx
@@ -2191,32 +2191,39 @@ bool ScInterpreter::GetBoolWithDefault(bool bDefault)
     return GetDoubleWithDefault(bDefault ? 1.0 : 0.0) != 0.0;
 }
 
-sal_Int32 ScInterpreter::double_to_int32(double fVal)
+template <typename Int>
+    requires std::is_integral_v<Int>
+Int ScInterpreter::double_to(double fVal)
 {
     if (!std::isfinite(fVal))
     {
         SetError( GetDoubleErrorValue( fVal));
-        return SAL_MAX_INT32;
+        return std::numeric_limits<Int>::max();
     }
     if (fVal > 0.0)
     {
         fVal = rtl::math::approxFloor( fVal);
-        if (fVal > SAL_MAX_INT32)
+        if (fVal > std::numeric_limits<Int>::max())
         {
             SetError( FormulaError::IllegalArgument);
-            return SAL_MAX_INT32;
+            return std::numeric_limits<Int>::max();
         }
     }
     else if (fVal < 0.0)
     {
         fVal = rtl::math::approxCeil( fVal);
-        if (fVal < SAL_MIN_INT32)
+        if (fVal < std::numeric_limits<Int>::min())
         {
             SetError( FormulaError::IllegalArgument);
-            return SAL_MAX_INT32;
+            return std::numeric_limits<Int>::max();
         }
     }
-    return static_cast<sal_Int32>(fVal);
+    return static_cast<Int>(fVal);
+}
+
+sal_Int32 ScInterpreter::double_to_int32(double fVal)
+{
+    return double_to<sal_Int32>(fVal);
 }
 
 sal_Int32 ScInterpreter::GetInt32()
@@ -2248,31 +2255,7 @@ sal_Int32 ScInterpreter::GetFloor32()
 
 sal_Int16 ScInterpreter::GetInt16()
 {
-    double fVal = GetDouble();
-    if (!std::isfinite(fVal))
-    {
-        SetError( GetDoubleErrorValue( fVal));
-        return SAL_MAX_INT16;
-    }
-    if (fVal > 0.0)
-    {
-        fVal = rtl::math::approxFloor( fVal);
-        if (fVal > SAL_MAX_INT16)
-        {
-            SetError( FormulaError::IllegalArgument);
-            return SAL_MAX_INT16;
-        }
-    }
-    else if (fVal < 0.0)
-    {
-        fVal = rtl::math::approxCeil( fVal);
-        if (fVal < SAL_MIN_INT16)
-        {
-            SetError( FormulaError::IllegalArgument);
-            return SAL_MAX_INT16;
-        }
-    }
-    return static_cast<sal_Int16>(fVal);
+    return double_to<sal_Int16>(GetDouble());
 }
 
 sal_uInt32 ScInterpreter::GetUInt32()
commit c38f9a4cd8537377337bd8558b197d937e30dcd1
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Feb 5 11:48:58 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sat Feb 15 12:03:07 2025 +0100

    Simplify Get*WithDefault to delegate to GetDoubleWithDefault
    
    Change-Id: I8be117954f58775e7b7ebbd441d252030f175f07
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/181693
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sc/source/core/tool/interpr4.cxx b/sc/source/core/tool/interpr4.cxx
index 28a36b0332df..a34c9e3b65bc 100644
--- a/sc/source/core/tool/interpr4.cxx
+++ b/sc/source/core/tool/interpr4.cxx
@@ -2180,20 +2180,15 @@ double ScInterpreter::GetDouble()
 
 double ScInterpreter::GetDoubleWithDefault(double nDefault)
 {
-    bool bMissing = IsMissing();
-    double nResultVal = GetDouble();
-    if ( bMissing )
-        nResultVal = nDefault;
-    return nResultVal;
+    if (!IsMissing())
+        return GetDouble();
+    Pop();
+    return nDefault;
 }
 
 bool ScInterpreter::GetBoolWithDefault(bool bDefault)
 {
-    bool bMissing = IsMissing();
-    bool bResultVal = (GetDouble() != 0.0);
-    if (bMissing)
-        bResultVal = bDefault;
-    return bResultVal;
+    return GetDoubleWithDefault(bDefault ? 1.0 : 0.0) != 0.0;
 }
 
 sal_Int32 ScInterpreter::double_to_int32(double fVal)
@@ -2231,11 +2226,7 @@ sal_Int32 ScInterpreter::GetInt32()
 
 sal_Int32 ScInterpreter::GetInt32WithDefault( sal_Int32 nDefault )
 {
-    bool bMissing = IsMissing();
-    double fVal = GetDouble();
-    if ( bMissing )
-        return nDefault;
-    return double_to_int32(fVal);
+    return double_to_int32(GetDoubleWithDefault(nDefault));
 }
 
 sal_Int32 ScInterpreter::GetFloor32()

Reply via email to