diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2022-02-24 11:37:11 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2022-02-24 15:24:33 +0100 |
commit | b927f636a0992b750c6a8de8bb8e9cc5e98a0e33 (patch) | |
tree | 654de031ab7c91606857302234792bc86dbf434b | |
parent | 14b219547dff8ed1c998a045f14cb1d9eed485c5 (diff) |
Deduplicate valueOf*Int*
Change-Id: Ib9da148c371f52ee8da2848581bb8a024f97de96
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130479
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
-rw-r--r-- | sal/rtl/string.cxx | 6 | ||||
-rw-r--r-- | sal/rtl/strtmpl.hxx | 129 | ||||
-rw-r--r-- | sal/rtl/ustring.cxx | 6 |
3 files changed, 22 insertions, 119 deletions
diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx index 3c8c01e98b52..86cfdb1672d9 100644 --- a/sal/rtl/string.cxx +++ b/sal/rtl/string.cxx @@ -514,19 +514,19 @@ sal_Int32 SAL_CALL rtl_str_valueOfChar(char* pStr, char c) SAL_THROW_EXTERN_C() sal_Int32 SAL_CALL rtl_str_valueOfInt32(char* pStr, sal_Int32 n, sal_Int16 nRadix) SAL_THROW_EXTERN_C() { - return rtl::str::valueOfInt32(pStr, n, nRadix); + return rtl::str::valueOfInt<RTL_STR_MAX_VALUEOFINT32>(pStr, n, nRadix); } sal_Int32 SAL_CALL rtl_str_valueOfInt64(char* pStr, sal_Int64 n, sal_Int16 nRadix) SAL_THROW_EXTERN_C() { - return rtl::str::valueOfInt64(pStr, n, nRadix); + return rtl::str::valueOfInt<RTL_STR_MAX_VALUEOFINT64>(pStr, n, nRadix); } sal_Int32 SAL_CALL rtl_str_valueOfUInt64(char* pStr, sal_uInt64 n, sal_Int16 nRadix) SAL_THROW_EXTERN_C() { - return rtl::str::valueOfUInt64(pStr, n, nRadix); + return rtl::str::valueOfInt<RTL_STR_MAX_VALUEOFUINT64>(pStr, n, nRadix); } sal_Bool SAL_CALL rtl_str_toBoolean(const char* pStr) SAL_THROW_EXTERN_C() diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx index d0f8fbeb6d83..0cee38057074 100644 --- a/sal/rtl/strtmpl.hxx +++ b/sal/rtl/strtmpl.hxx @@ -623,85 +623,35 @@ sal_Int32 valueOfChar ( IMPL_RTL_STRCODE* pStr, /* ----------------------------------------------------------------------- */ -template <typename IMPL_RTL_STRCODE> -sal_Int32 valueOfInt32 ( IMPL_RTL_STRCODE* pStr, - sal_Int32 n, +template <sal_Int32 maxLen, typename IMPL_RTL_STRCODE, typename T> +sal_Int32 valueOfInt ( IMPL_RTL_STRCODE* pStr, + T n, sal_Int16 nRadix ) { assert(pStr); assert( nRadix >= RTL_STR_MIN_RADIX && nRadix <= RTL_STR_MAX_RADIX ); - char aBuf[RTL_STR_MAX_VALUEOFINT32]; + char aBuf[maxLen]; char* pBuf = aBuf; sal_Int32 nLen = 0; - sal_uInt32 nValue; + using uT = std::make_unsigned_t<T>; + uT nValue; /* Radix must be valid */ if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) nRadix = 10; - /* is value negative */ - if ( n < 0 ) - { - *pStr = '-'; - pStr++; - nLen++; - nValue = n == SAL_MIN_INT32 ? static_cast<sal_uInt32>(n) : -n; - } - else - nValue = n; - - /* create a recursive buffer with all values, except the last one */ - do + if constexpr (std::is_signed_v<T>) { - char nDigit = static_cast<char>(nValue % nRadix); - nValue /= nRadix; - if ( nDigit > 9 ) - *pBuf = (nDigit-10) + 'a'; + /* is value negative */ + if ( n < 0 ) + { + *pStr = '-'; + pStr++; + nLen++; + nValue = n == std::numeric_limits<T>::min() ? static_cast<uT>(n) : -n; + } else - *pBuf = (nDigit + '0' ); - pBuf++; - } - while ( nValue > 0 ); - - /* copy the values in the right direction into the destination buffer */ - do - { - pBuf--; - *pStr = *pBuf; - pStr++; - nLen++; - } - while ( pBuf != aBuf ); - *pStr = 0; - - return nLen; -} - -/* ----------------------------------------------------------------------- */ - -template <typename IMPL_RTL_STRCODE> -sal_Int32 valueOfInt64 ( IMPL_RTL_STRCODE* pStr, - sal_Int64 n, - sal_Int16 nRadix ) -{ - assert(pStr); - assert( nRadix >= RTL_STR_MIN_RADIX && nRadix <= RTL_STR_MAX_RADIX ); - char aBuf[RTL_STR_MAX_VALUEOFINT64]; - char* pBuf = aBuf; - sal_Int32 nLen = 0; - sal_uInt64 nValue; - - /* Radix must be valid */ - if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) - nRadix = 10; - - /* is value negative */ - if ( n < 0 ) - { - *pStr = '-'; - pStr++; - nLen++; - nValue = n == SAL_MIN_INT64 ? static_cast<sal_uInt64>(n) : -n; + nValue = n; } else nValue = n; @@ -735,53 +685,6 @@ sal_Int32 valueOfInt64 ( IMPL_RTL_STRCODE* pStr, /* ----------------------------------------------------------------------- */ -template <typename IMPL_RTL_STRCODE> -sal_Int32 valueOfUInt64 ( IMPL_RTL_STRCODE* pStr, - sal_uInt64 n, - sal_Int16 nRadix ) -{ - assert(pStr); - assert( nRadix >= RTL_STR_MIN_RADIX && nRadix <= RTL_STR_MAX_RADIX ); - char aBuf[RTL_STR_MAX_VALUEOFUINT64]; - char* pBuf = aBuf; - sal_Int32 nLen = 0; - sal_uInt64 nValue; - - /* Radix must be valid */ - if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) - nRadix = 10; - - nValue = n; - - /* create a recursive buffer with all values, except the last one */ - do - { - char nDigit = static_cast<char>(nValue % nRadix); - nValue /= nRadix; - if ( nDigit > 9 ) - *pBuf = (nDigit-10) + 'a'; - else - *pBuf = (nDigit + '0' ); - pBuf++; - } - while ( nValue > 0 ); - - /* copy the values in the right direction into the destination buffer */ - do - { - pBuf--; - *pStr = *pBuf; - pStr++; - nLen++; - } - while ( pBuf != aBuf ); - *pStr = 0; - - return nLen; -} - -/* ----------------------------------------------------------------------- */ - template <typename IMPL_RTL_STRCODE> sal_Bool toBoolean( const IMPL_RTL_STRCODE* pStr ) { assert(pStr); diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx index 227894742b4e..2ea2eb142d7f 100644 --- a/sal/rtl/ustring.cxx +++ b/sal/rtl/ustring.cxx @@ -1540,19 +1540,19 @@ sal_Int32 SAL_CALL rtl_ustr_valueOfChar(sal_Unicode* pStr, sal_Unicode c) SAL_TH sal_Int32 SAL_CALL rtl_ustr_valueOfInt32(sal_Unicode* pStr, sal_Int32 n, sal_Int16 nRadix) SAL_THROW_EXTERN_C() { - return rtl::str::valueOfInt32(pStr, n, nRadix); + return rtl::str::valueOfInt<RTL_USTR_MAX_VALUEOFINT32>(pStr, n, nRadix); } sal_Int32 SAL_CALL rtl_ustr_valueOfInt64(sal_Unicode* pStr, sal_Int64 n, sal_Int16 nRadix) SAL_THROW_EXTERN_C() { - return rtl::str::valueOfInt64(pStr, n, nRadix); + return rtl::str::valueOfInt<RTL_USTR_MAX_VALUEOFINT64>(pStr, n, nRadix); } sal_Int32 SAL_CALL rtl_ustr_valueOfUInt64(sal_Unicode* pStr, sal_uInt64 n, sal_Int16 nRadix) SAL_THROW_EXTERN_C() { - return rtl::str::valueOfUInt64(pStr, n, nRadix); + return rtl::str::valueOfInt<RTL_USTR_MAX_VALUEOFUINT64>(pStr, n, nRadix); } sal_Bool SAL_CALL rtl_ustr_toBoolean(const sal_Unicode* pStr) SAL_THROW_EXTERN_C() |