summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2022-02-24 12:31:56 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2022-02-24 15:25:14 +0100
commited5503ba4ef2952b5fbc1195692076e858597baa (patch)
tree58cec6fb93e8891885e45c89bf9d3199426d80c1 /sal
parentc6cbf970943e17da5d95fc730fa1c15637caee05 (diff)
Deduplicate rtl_*str_valueOf{Float,Double}
Change-Id: Ie3254fbf6178f4e623fd8209b58e9f19d2c46d2c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130480 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sal')
-rw-r--r--sal/rtl/string.cxx26
-rw-r--r--sal/rtl/strtmpl.hxx24
-rw-r--r--sal/rtl/ustring.cxx26
3 files changed, 28 insertions, 48 deletions
diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 86cfdb1672d9..ab59eab78c77 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -64,35 +64,13 @@ rtl_String rtl::str::EmptyStringImpl<rtl_String>::data =
sal_Int32 SAL_CALL rtl_str_valueOfFloat(char * pStr, float f)
SAL_THROW_EXTERN_C()
{
- assert(pStr);
- rtl_String * pResult = nullptr;
- sal_Int32 nLen;
- rtl_math_doubleToString(
- &pResult, nullptr, 0, f, rtl_math_StringFormat_G,
- RTL_STR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr, 0,
- true);
- nLen = pResult->length;
- OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFFLOAT);
- memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(char));
- rtl_string_release(pResult);
- return nLen;
+ return rtl::str::valueOfFP<RTL_STR_MAX_VALUEOFFLOAT>(pStr, f, &rtl_math_doubleToString);
}
sal_Int32 SAL_CALL rtl_str_valueOfDouble(char * pStr, double d)
SAL_THROW_EXTERN_C()
{
- assert(pStr);
- rtl_String * pResult = nullptr;
- sal_Int32 nLen;
- rtl_math_doubleToString(
- &pResult, nullptr, 0, d, rtl_math_StringFormat_G,
- RTL_STR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr,
- 0, true);
- nLen = pResult->length;
- OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFDOUBLE);
- memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(char));
- rtl_string_release(pResult);
- return nLen;
+ return rtl::str::valueOfFP<RTL_STR_MAX_VALUEOFDOUBLE>(pStr, d, &rtl_math_doubleToString);
}
float SAL_CALL rtl_str_toFloat(char const * pStr) SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 2e11ffd0161f..8901f3ace2b2 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1663,6 +1663,30 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom const* from, sal_Int32 fr
RTL_LOG_STRING_NEW(*s);
}
+template <class IMPL_RTL_STRINGDATA>
+using doubleToString_t
+ = void(SAL_CALL*)(IMPL_RTL_STRINGDATA** pResult, sal_Int32* pResultCapacity,
+ sal_Int32 nResultOffset, double fValue, rtl_math_StringFormat eFormat,
+ sal_Int32 nDecPlaces, STRCODE<IMPL_RTL_STRINGDATA> cDecSeparator,
+ sal_Int32 const* pGroups, STRCODE<IMPL_RTL_STRINGDATA> cGroupSeparator,
+ sal_Bool bEraseTrailingDecZeros) SAL_THROW_EXTERN_C();
+
+template <sal_Int32 maxLen, typename T, typename IMPL_RTL_STRINGDATA>
+sal_Int32 SAL_CALL valueOfFP(STRCODE<IMPL_RTL_STRINGDATA>* pStr, T f,
+ doubleToString_t<IMPL_RTL_STRINGDATA> doubleToString)
+{
+ assert(pStr);
+ IMPL_RTL_STRINGDATA* pResult = nullptr;
+ sal_Int32 nLen;
+ doubleToString(&pResult, nullptr, 0, f, rtl_math_StringFormat_G,
+ maxLen - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr, 0, true);
+ nLen = pResult->length;
+ OSL_ASSERT(nLen < maxLen);
+ Copy(pStr, pResult->buffer, nLen + 1);
+ release(pResult);
+ return nLen;
+}
+
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 2ea2eb142d7f..b5fc3685c48b 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -121,35 +121,13 @@ sal_Int32 rtl_ustr_lastIndexOfAscii_WithLength(
sal_Int32 SAL_CALL rtl_ustr_valueOfFloat(sal_Unicode * pStr, float f)
SAL_THROW_EXTERN_C()
{
- assert(pStr);
- rtl_uString * pResult = nullptr;
- sal_Int32 nLen;
- rtl_math_doubleToUString(
- &pResult, nullptr, 0, f, rtl_math_StringFormat_G,
- RTL_USTR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr,
- 0, true);
- nLen = pResult->length;
- OSL_ASSERT(nLen < RTL_USTR_MAX_VALUEOFFLOAT);
- memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Unicode));
- rtl_uString_release(pResult);
- return nLen;
+ return rtl::str::valueOfFP<RTL_USTR_MAX_VALUEOFFLOAT>(pStr, f, &rtl_math_doubleToUString);
}
sal_Int32 SAL_CALL rtl_ustr_valueOfDouble(sal_Unicode * pStr, double d)
SAL_THROW_EXTERN_C()
{
- assert(pStr);
- rtl_uString * pResult = nullptr;
- sal_Int32 nLen;
- rtl_math_doubleToUString(
- &pResult, nullptr, 0, d, rtl_math_StringFormat_G,
- RTL_USTR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr,
- 0, true);
- nLen = pResult->length;
- OSL_ASSERT(nLen < RTL_USTR_MAX_VALUEOFDOUBLE);
- memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Unicode));
- rtl_uString_release(pResult);
- return nLen;
+ return rtl::str::valueOfFP<RTL_USTR_MAX_VALUEOFDOUBLE>(pStr, d, &rtl_math_doubleToUString);
}
namespace {