From ed5503ba4ef2952b5fbc1195692076e858597baa Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Thu, 24 Feb 2022 12:31:56 +0300 Subject: 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 --- sal/rtl/string.cxx | 26 ++------------------------ sal/rtl/strtmpl.hxx | 24 ++++++++++++++++++++++++ sal/rtl/ustring.cxx | 26 ++------------------------ 3 files changed, 28 insertions(+), 48 deletions(-) (limited to 'sal') 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::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(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(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 +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 cDecSeparator, + sal_Int32 const* pGroups, STRCODE cGroupSeparator, + sal_Bool bEraseTrailingDecZeros) SAL_THROW_EXTERN_C(); + +template +sal_Int32 SAL_CALL valueOfFP(STRCODE* pStr, T f, + doubleToString_t 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(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(pStr, d, &rtl_math_doubleToUString); } namespace { -- cgit