summaryrefslogtreecommitdiff
path: root/svl/source/numbers/zforfind.cxx
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2020-02-23 19:19:10 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2020-02-27 11:02:03 +0100
commitf3e7004794eded346d98264d3061f4e4aa80ee0a (patch)
tree80a79880c0615114d05dd2a4025ada2684b01812 /svl/source/numbers/zforfind.cxx
parentb623684c666ccf07b59a76fe58e10f34afbb41a4 (diff)
tdf#130725: use strtod by David M. Gay to make sure we get the nearest
... representation of given decimal. Use dtoa.c from https://www.netlib.org/fp/dtoa.c to build a custom static library that doesn't use current locale (unlike strtod from stdlib.h). This is the implementation used by e.g. python and nss (search for "dtoa.c" under UnpackedTarball). To avoid name clash with the standard strtod, rename the function to strtod_nolocale. Size of buffer on stack in ImpSvNumberInputScan::StringToDouble is 256 characters. Logging function usage in make check, of ~124 600 invocations, the longest string was 14 characters, average being 2.1 characters. So heap allocation is unlikely in scenarios with intensive function usage. After std::from_chars is available in baseline compilers, external library can be dropped, and call to strtod_nolocale replaced with the standard function. The artifact at https://dev-www.libreoffice.org/src/dtoa-20180411.tgz is created with mkdir dtoa && mkdir dtoa/src && wget https://www.netlib.org/fp/dtoa.c -O dtoa/src/dtoa.c && \ printf 'd8bab255476f39ea495c8c8ed164f9077da926e6ca7afb9ad3c56d337c4484fe dtoa/src/dtoa.c' | sha256sum -c && \ tar -c --owner=0 --group=0 --mode=go=r,u=rw --mtime='Wed, 11 Apr 2018 15:59:39 GMT' dtoa/src/dtoa.c | gzip -n > dtoa-20180411.tgz && \ printf '0082d0684f7db6f62361b76c4b7faba19e0c7ce5cb8e36c4b65fea8281e711b4 dtoa-20180411.tgz' | sha256sum -c (where the date "Wed, 11 Apr 2018 15:59:39 GMT" is from `wget -S https://www.netlib.org/fp/dtoa.c` "Last-Modified: Wed, 11 Apr 2018 15:59:39 GMT" header). Change-Id: Ia61b7678e257c4bc1ff193f3f856d611aa5c1a21 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88854 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'svl/source/numbers/zforfind.cxx')
-rw-r--r--svl/source/numbers/zforfind.cxx48
1 files changed, 23 insertions, 25 deletions
diff --git a/svl/source/numbers/zforfind.cxx b/svl/source/numbers/zforfind.cxx
index e56a5cef8502..32b471a5df23 100644
--- a/svl/source/numbers/zforfind.cxx
+++ b/svl/source/numbers/zforfind.cxx
@@ -18,6 +18,7 @@
*/
#include <cstdlib>
+#include <dtoa.h>
#include <float.h>
#include <comphelper/string.hxx>
#include <sal/log.hxx>
@@ -36,6 +37,8 @@
#include "zforscan.hxx"
#include <svl/zformat.hxx>
+#include <memory>
+
#include "zforfind.hxx"
#ifndef DBG_UTIL
@@ -151,35 +154,30 @@ static void TransformInput( SvNumberFormatter const * pFormatter, OUString& rStr
*/
double ImpSvNumberInputScan::StringToDouble( const OUString& rStr, bool bForceFraction )
{
- double fNum = 0.0;
- double fFrac = 0.0;
- int nExp = 0;
- sal_Int32 nPos = 0;
- sal_Int32 nLen = rStr.getLength();
- bool bPreSep = !bForceFraction;
-
- while (nPos < nLen)
+ std::unique_ptr<char[]> bufInHeap;
+ constexpr int bufOnStackSize = 256;
+ char bufOnStack[bufOnStackSize];
+ char* buf = bufOnStack;
+ const sal_Int32 bufsize = rStr.getLength() + (bForceFraction ? 2 : 1);
+ if (bufsize > bufOnStackSize)
{
- if (rStr[nPos] == '.')
- {
- bPreSep = false;
- }
- else if (bPreSep)
- {
- fNum = fNum * 10.0 + static_cast<double>(rStr[nPos] - '0');
- }
- else
- {
- fFrac = fFrac * 10.0 + static_cast<double>(rStr[nPos] - '0');
- --nExp;
- }
- nPos++;
+ bufInHeap = std::make_unique<char[]>(bufsize);
+ buf = bufInHeap.get();
}
- if ( fFrac )
+ char* p = buf;
+ if (bForceFraction)
+ *p++ = '.';
+ for (sal_Int32 nPos = 0; nPos < rStr.getLength(); ++nPos)
{
- return fNum + ::rtl::math::pow10Exp( fFrac, nExp );
+ sal_Unicode c = rStr[nPos];
+ if (c == '.' || (c >= '0' && c <= '9'))
+ *p++ = static_cast<char>(c);
+ else
+ break;
}
- return fNum;
+ *p = '\0';
+
+ return strtod_nolocale(buf, nullptr);
}
namespace {