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/rtl/strtmpl.hxx | |
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/rtl/strtmpl.hxx')
-rw-r--r-- | sal/rtl/strtmpl.hxx | 65 |
1 files changed, 42 insertions, 23 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 ) |