diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2022-02-24 21:43:50 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2022-02-25 08:29:35 +0100 |
commit | 8d7516a0e690fb34b894b8f064a47d705100a56f (patch) | |
tree | 01ad2e6da9625340d9403febc6221f4d3849fdfd /sal | |
parent | c2cf1b01d5bed68c572ca73b32c6f92d6723562c (diff) |
Deduplicate some comparison functions
Change-Id: Iffeb4323c99649d45387981ec583fdcff207ec4e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130512
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/rtl/strtmpl.hxx | 65 | ||||
-rw-r--r-- | sal/rtl/ustring.cxx | 87 |
2 files changed, 46 insertions, 106 deletions
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx index 90d9547170ec..287e6987e36e 100644 --- a/sal/rtl/strtmpl.hxx +++ b/sal/rtl/strtmpl.hxx @@ -82,27 +82,45 @@ template <typename T> sal_Int32 getLength( const T* pStr ) /* ----------------------------------------------------------------------- */ -template <typename IMPL_RTL_STRCODE> -sal_Int32 compare ( const IMPL_RTL_STRCODE* pStr1, - const IMPL_RTL_STRCODE* pStr2 ) +template <typename C> void warnIfCharAndNotAscii(C c) +{ + if constexpr (sizeof(c) == sizeof(char)) + SAL_WARN_IF(!rtl::isAscii(static_cast<unsigned char>(c)), "rtl.string", + "Found non-ASCII char"); +} + +template <typename C1, typename C2> void warnIfOneIsCharAndNotAscii(C1 c1, C2 c2) +{ + if constexpr (sizeof(c1) != sizeof(c2)) + { + warnIfCharAndNotAscii(c1); + warnIfCharAndNotAscii(c2); + } +} + +/* ----------------------------------------------------------------------- */ + +template <typename C1, typename C2> sal_Int32 compare(const C1* pStr1, const C2* pStr2) { assert(pStr1); assert(pStr2); - if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char)) + if constexpr (sizeof(C1) == sizeof(char) && sizeof(C2) == sizeof(char)) { // take advantage of builtin optimisations return strcmp( pStr1, pStr2); } - else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(wchar_t)) + else if constexpr (sizeof(C1) == sizeof(wchar_t) && sizeof(C2) == sizeof(wchar_t)) { // take advantage of builtin optimisations return wcscmp(reinterpret_cast<wchar_t const *>(pStr1), reinterpret_cast<wchar_t const *>(pStr2)); } - else + else // including C1 != C2 { sal_Int32 nRet; for (;;) { + warnIfOneIsCharAndNotAscii(*pStr1, *pStr2); + nRet = static_cast<sal_Int32>(IMPL_RTL_USTRCODE(*pStr1)) - static_cast<sal_Int32>(IMPL_RTL_USTRCODE(*pStr2)); if (!(nRet == 0 && *pStr2 )) @@ -146,19 +164,19 @@ sal_Int32 shortenedCompare_WithLength ( const IMPL_R /* ----------------------------------------------------------------------- */ -template <typename IMPL_RTL_STRCODE> -sal_Int32 reverseCompare_WithLength ( const IMPL_RTL_STRCODE* pStr1, - sal_Int32 nStr1Len, - const IMPL_RTL_STRCODE* pStr2, - sal_Int32 nStr2Len ) +template <typename C1, typename C2> +sal_Int32 reverseCompare_WithLength(const C1* pStr1, sal_Int32 nStr1Len, + const C2* pStr2, sal_Int32 nStr2Len) { assert(nStr1Len >= 0); assert(nStr2Len >= 0); - const IMPL_RTL_STRCODE* pStr1Run = pStr1+nStr1Len; - const IMPL_RTL_STRCODE* pStr2Run = pStr2+nStr2Len; + const C1* pStr1Run = pStr1+nStr1Len; + const C2* pStr2Run = pStr2+nStr2Len; sal_Int32 nRet; while ( (pStr1 < pStr1Run) && (pStr2 < pStr2Run) ) { + warnIfOneIsCharAndNotAscii(*pStr1, *pStr2); + pStr1Run--; pStr2Run--; nRet = static_cast<sal_Int32>(IMPL_RTL_USTRCODE( *pStr1Run ))- @@ -172,15 +190,16 @@ sal_Int32 reverseCompare_WithLength ( const IMPL_RTL /* ----------------------------------------------------------------------- */ -template <typename IMPL_RTL_STRCODE> -sal_Int32 compareIgnoreAsciiCase ( const IMPL_RTL_STRCODE* pStr1, - const IMPL_RTL_STRCODE* pStr2 ) +template <typename C1, typename C2> +sal_Int32 compareIgnoreAsciiCase(const C1* pStr1, const C2* pStr2) { assert(pStr1); assert(pStr2); sal_uInt32 c1; do { + warnIfOneIsCharAndNotAscii(*pStr1, *pStr2); + c1 = IMPL_RTL_USTRCODE(*pStr1); sal_Int32 nRet = rtl::compareIgnoreAsciiCase( c1, IMPL_RTL_USTRCODE(*pStr2)); @@ -197,18 +216,18 @@ sal_Int32 compareIgnoreAsciiCase ( const IMPL_RTL_ST /* ----------------------------------------------------------------------- */ -template <typename IMPL_RTL_STRCODE> -sal_Int32 compareIgnoreAsciiCase_WithLength ( const IMPL_RTL_STRCODE* pStr1, - sal_Int32 nStr1Len, - const IMPL_RTL_STRCODE* pStr2, - sal_Int32 nStr2Len ) +template <typename C1, typename C2> +sal_Int32 compareIgnoreAsciiCase_WithLength(const C1* pStr1, sal_Int32 nStr1Len, + const C2* pStr2, sal_Int32 nStr2Len) { assert(nStr1Len >= 0); assert(nStr2Len >= 0); - const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len; - const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len; + const C1* pStr1End = pStr1 + nStr1Len; + const C2* pStr2End = pStr2 + nStr2Len; while ( (pStr1 < pStr1End) && (pStr2 < pStr2End) ) { + warnIfOneIsCharAndNotAscii(*pStr1, *pStr2); + sal_Int32 nRet = rtl::compareIgnoreAsciiCase( IMPL_RTL_USTRCODE(*pStr1), IMPL_RTL_USTRCODE(*pStr2)); if ( nRet != 0 ) diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx index 3aa772f491e6..ff51a53f61fd 100644 --- a/sal/rtl/ustring.cxx +++ b/sal/rtl/ustring.cxx @@ -166,23 +166,7 @@ sal_Int32 SAL_CALL rtl_ustr_ascii_compare( const sal_Unicode* pStr1, const char* pStr2 ) SAL_THROW_EXTERN_C() { - assert(pStr1); - assert(pStr2); - sal_Int32 nRet; - for (;;) - { - nRet = static_cast<sal_Int32>(*pStr1)- - static_cast<sal_Int32>(static_cast<unsigned char>(*pStr2)); - if (!(nRet == 0 && *pStr2 )) - break; - /* Check ASCII range */ - SAL_WARN_IF( (static_cast<unsigned char>(*pStr2)) > 127, "rtl.string", - "rtl_ustr_ascii_compare - Found char > 127" ); - pStr1++; - pStr2++; - } - - return nRet; + return rtl::str::compare(pStr1, pStr2); } /* ----------------------------------------------------------------------- */ @@ -268,23 +252,7 @@ sal_Int32 SAL_CALL rtl_ustr_asciil_reverseCompare_WithLength( const sal_Unicode* sal_Int32 nStr2Len ) SAL_THROW_EXTERN_C() { - assert(nStr1Len >= 0 && nStr2Len >= 0); - const sal_Unicode* pStr1Run = pStr1+nStr1Len; - const char* pStr2Run = pStr2+nStr2Len; - sal_Int32 nRet; - while ( (pStr1 < pStr1Run) && (pStr2 < pStr2Run) ) - { - /* Check ASCII range */ - SAL_WARN_IF( (static_cast<unsigned char>(*pStr2)) > 127, "rtl.string", - "rtl_ustr_asciil_reverseCompare_WithLength - Found char > 127" ); - pStr1Run--; - pStr2Run--; - nRet = static_cast<sal_Int32>(*pStr1Run)- static_cast<sal_Int32>(*pStr2Run); - if ( nRet ) - return nRet; - } - - return nStr1Len - nStr2Len; + return rtl::str::reverseCompare_WithLength(pStr1, nStr1Len, pStr2, nStr2Len); } /* ----------------------------------------------------------------------- */ @@ -317,33 +285,7 @@ sal_Int32 SAL_CALL rtl_ustr_ascii_compareIgnoreAsciiCase( const sal_Unicode* pSt const char* pStr2 ) SAL_THROW_EXTERN_C() { - assert(pStr1); - assert(pStr2); - sal_Int32 nRet; - sal_Int32 c1; - sal_Int32 c2; - do - { - /* Check ASCII range */ - SAL_WARN_IF( (static_cast<unsigned char>(*pStr2)) > 127, "rtl.string", - "rtl_ustr_ascii_compareIgnoreAsciiCase - Found char > 127" ); - /* If character between 'A' and 'Z', then convert it to lowercase */ - c1 = static_cast<sal_Int32>(*pStr1); - c2 = static_cast<sal_Int32>(static_cast<unsigned char>(*pStr2)); - if ( (c1 >= 65) && (c1 <= 90) ) - c1 += 32; - if ( (c2 >= 65) && (c2 <= 90) ) - c2 += 32; - nRet = c1-c2; - if ( nRet != 0 ) - return nRet; - - pStr1++; - pStr2++; - } - while ( c2 ); - - return 0; + return rtl::str::compareIgnoreAsciiCase(pStr1, pStr2); } /* ----------------------------------------------------------------------- */ @@ -390,28 +332,7 @@ sal_Int32 rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( sal_Unicode const * first, sal_Int32 firstLen, char const * second, sal_Int32 secondLen) SAL_THROW_EXTERN_C() { - assert(firstLen >= 0 && secondLen >= 0); - sal_Int32 i; - sal_Int32 len = std::min(firstLen, secondLen); - for (i = 0; i < len; ++i) { - /* Check ASCII range */ - SAL_WARN_IF( (static_cast<unsigned char>(*second)) > 127, "rtl.string", - "rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths - Found char > 127" ); - sal_Int32 c1 = *first++; - sal_Int32 c2 = static_cast<unsigned char>(*second++); - sal_Int32 d; - if (c1 >= 65 && c1 <= 90) { - c1 += 32; - } - if (c2 >= 65 && c2 <= 90) { - c2 += 32; - } - d = c1 - c2; - if (d != 0) { - return d; - } - } - return firstLen - secondLen; + return rtl::str::compareIgnoreAsciiCase_WithLength(first, firstLen, second, secondLen); } /* ----------------------------------------------------------------------- */ |