diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2023-04-10 11:15:02 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2023-04-10 16:36:09 +0200 |
commit | 8b6d167608d74986e97d462fde8efcae4c28b564 (patch) | |
tree | 4d9f6aaa6566f9ae14df90718739e710a586012a /include/rtl/ustring.hxx | |
parent | eb372cde829a667acb509ab9d55887ed934f7639 (diff) |
Use of O(U)StringNumber for float/double is actually a pessimisation
They use rtl_(u)str_valueOf(Float|Double), to fill the buffer, and
the latter use doubleToString, which creates an rtl_(u)String, copies
to the buffer, and releases the rtl_(u)String.
So instead just use the rtl_(u)String from rtl_math_doubleTo(U)String
directly. Even when the end result is not needed as O(U)String, this
would avoid an extra copy step. Also, this avoids separate
LIBO_INTERNAL_ONLY implementations.
Change-Id: Ib1d9ecebd7876dfff7dc758f89ee4c1536647a50
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150150
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/rtl/ustring.hxx')
-rw-r--r-- | include/rtl/ustring.hxx | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx index a87082aacfff..263cb71d74c9 100644 --- a/include/rtl/ustring.hxx +++ b/include/rtl/ustring.hxx @@ -39,6 +39,7 @@ #include <type_traits> #endif +#include "rtl/math.h" #include "rtl/ustring.h" #include "rtl/string.hxx" #include "rtl/stringutils.hxx" @@ -3082,14 +3083,6 @@ public: { return number( static_cast< unsigned long long >( i ), radix ); } - static auto number( float f ) - { - return OUStringNumber<RTL_USTR_MAX_VALUEOFFLOAT>(rtl_ustr_valueOfFloat, f); - } - static auto number( double d ) - { - return OUStringNumber<RTL_USTR_MAX_VALUEOFDOUBLE>(rtl_ustr_valueOfDouble, d); - } #else /** Returns the string representation of the integer argument. @@ -3138,6 +3131,7 @@ public: sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFUINT64]; return OUString(aBuf, rtl_ustr_valueOfUInt64(aBuf, ll, radix)); } +#endif /** Returns the string representation of the float argument. @@ -3150,8 +3144,15 @@ public: */ static OUString number( float f ) { - sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFFLOAT]; - return OUString(aBuf, rtl_ustr_valueOfFloat(aBuf, f)); + rtl_uString* pNew = NULL; + // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfFloat + rtl_math_doubleToUString(&pNew, NULL, 0, f, rtl_math_StringFormat_G, + RTL_USTR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.', + NULL, 0, true); + if (pNew == NULL) + throw std::bad_alloc(); + + return OUString(pNew, SAL_NO_ACQUIRE); } /** @@ -3165,10 +3166,16 @@ public: */ static OUString number( double d ) { - sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFDOUBLE]; - return OUString(aBuf, rtl_ustr_valueOfDouble(aBuf, d)); + rtl_uString* pNew = NULL; + // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfDouble + rtl_math_doubleToUString(&pNew, NULL, 0, d, rtl_math_StringFormat_G, + RTL_USTR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.', + NULL, 0, true); + if (pNew == NULL) + throw std::bad_alloc(); + + return OUString(pNew, SAL_NO_ACQUIRE); } -#endif #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING" static auto boolean(bool b) |