sal/rtl/string.cxx  |    4 -
 sal/rtl/strtmpl.hxx |  168 +++++++++++++---------------------------------------
 sal/rtl/ustring.cxx |    4 -
 3 files changed, 47 insertions(+), 129 deletions(-)

New commits:
commit 7b8671d7b58a2a70c641f8315dc506542e8266cc
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Feb 27 09:44:23 2022 +0100
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sun Feb 27 10:42:51 2022 +0100

    Deduplicate newToAscii*Case
    
    Change-Id: I55f4f43375037ad4247f592bc981d8718c12de68
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130631
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 4b08b5e2c39f..92564b494c61 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -650,13 +650,13 @@ void SAL_CALL rtl_string_newReplace(rtl_String** ppThis, 
rtl_String* pStr, char
 void SAL_CALL rtl_string_newToAsciiLowerCase(rtl_String** ppThis, rtl_String* 
pStr)
     SAL_THROW_EXTERN_C()
 {
-    rtl::str::newToAsciiLowerCase(ppThis, pStr);
+    rtl::str::newReplaceChars<rtl::str::ToAsciiLower>(ppThis, pStr);
 }
 
 void SAL_CALL rtl_string_newToAsciiUpperCase(rtl_String** ppThis, rtl_String* 
pStr)
     SAL_THROW_EXTERN_C()
 {
-    rtl::str::newToAsciiUpperCase(ppThis, pStr);
+    rtl::str::newReplaceChars<rtl::str::ToAsciiUpper>(ppThis, pStr);
 }
 
 void SAL_CALL rtl_string_newTrim(rtl_String** ppThis, rtl_String* pStr) 
SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 9cbc8148044a..d800dd3c7b84 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -918,13 +918,7 @@ sal_uInt64 toUInt64                             ( const 
IMPL_RTL_STRCODE* pStr,
 /* Internal String-Class help functions                                    */
 /* ======================================================================= */
 
-template <typename STRINGDATA> struct STRCODE_DATA
-{
-    using type = std::remove_extent_t<decltype(STRINGDATA::buffer)>;
-    using unsigned_type = std::make_unsigned_t<type>;
-};
-template <class STRINGDATA> using STRCODE = typename 
STRCODE_DATA<STRINGDATA>::type;
-template <class STRINGDATA> using USTRCODE = typename 
STRCODE_DATA<STRINGDATA>::unsigned_type;
+template <class STRINGDATA> using STRCODE = 
std::remove_extent_t<decltype(STRINGDATA::buffer)>;
 
 template <typename IMPL_RTL_STRINGDATA> IMPL_RTL_STRINGDATA* Alloc( sal_Int32 
nLen )
 {
@@ -943,27 +937,6 @@ template <typename IMPL_RTL_STRINGDATA> 
IMPL_RTL_STRINGDATA* Alloc( sal_Int32 nL
     return pData;
 }
 
-/* ----------------------------------------------------------------------- */
-
-template <typename IMPL_RTL_STRINGDATA>
-auto* NewCopy                                              ( 
IMPL_RTL_STRINGDATA** ppThis,
-                                                             
IMPL_RTL_STRINGDATA* pStr,
-                                                             sal_Int32 nCount )
-{
-    assert(ppThis);
-    assert(pStr);
-    assert(nCount >= 0 && nCount <= pStr->length);
-    *ppThis = Alloc<IMPL_RTL_STRINGDATA>( pStr->length );
-    OSL_ASSERT(*ppThis != nullptr);
-
-    auto* pDest   = (*ppThis)->buffer;
-
-    Copy(pDest, pStr->buffer, nCount);
-
-    RTL_LOG_STRING_NEW( *ppThis );
-    return pDest + nCount;
-}
-
 /* ======================================================================= */
 /* String-Class functions                                                  */
 /* ======================================================================= */
@@ -1456,118 +1429,63 @@ void newReplace                                ( 
IMPL_RTL_STRINGDATA** ppThis,
 
 /* ----------------------------------------------------------------------- */
 
-template <typename IMPL_RTL_STRINGDATA>
-void newToAsciiLowerCase                                ( 
IMPL_RTL_STRINGDATA** ppThis,
-                                                          IMPL_RTL_STRINGDATA* 
pStr )
+struct ToAsciiLower
 {
-    assert(ppThis);
-    assert(pStr);
-    IMPL_RTL_STRINGDATA*    pOrg        = *ppThis;
-    bool                    bChanged    = false;
-    sal_Int32               nLen        = pStr->length;
-    const auto*             pCharStr    = pStr->buffer;
-
-    while ( nLen > 0 )
+    template <typename C> static bool Applicable(C c)
     {
-        if ( rtl::isAsciiUpperCase(USTRCODE<IMPL_RTL_STRINGDATA>(*pCharStr)) )
-        {
-            /* Copy String */
-            auto* pNewCharStr = NewCopy( ppThis, pStr, pCharStr-pStr->buffer );
-
-            /* replace/copy rest of the string */
-            if ( pNewCharStr )
-            {
-                *pNewCharStr = 
rtl::toAsciiLowerCase(USTRCODE<IMPL_RTL_STRINGDATA>(*pCharStr));
-                pNewCharStr++;
-                pCharStr++;
-                nLen--;
-
-                while ( nLen > 0 )
-                {
-                    *pNewCharStr = 
rtl::toAsciiLowerCase(USTRCODE<IMPL_RTL_STRINGDATA>(*pCharStr));
-
-                    pNewCharStr++;
-                    pCharStr++;
-                    nLen--;
-                }
-            }
-
-            bChanged = true;
-            break;
-        }
-
-        pCharStr++;
-        nLen--;
+        return rtl::isAsciiUpperCase(IMPL_RTL_USTRCODE(c));
     }
-
-    if ( !bChanged )
+    template <typename C> static C Replace(C c)
     {
-        *ppThis = pStr;
-        acquire( pStr );
+        return rtl::toAsciiLowerCase(IMPL_RTL_USTRCODE(c));
     }
+};
 
-    RTL_LOG_STRING_NEW( *ppThis );
-    /* must be done last, if pStr == *ppThis */
-    if ( pOrg )
-        release( pOrg );
-}
-
-/* ----------------------------------------------------------------------- */
+struct ToAsciiUpper
+{
+    template <typename C> static bool Applicable(C c)
+    {
+        return rtl::isAsciiLowerCase(IMPL_RTL_USTRCODE(c));
+    }
+    template <typename C> static C Replace(C c)
+    {
+        return rtl::toAsciiUpperCase(IMPL_RTL_USTRCODE(c));
+    }
+};
 
-template <typename IMPL_RTL_STRINGDATA>
-void newToAsciiUpperCase                                ( 
IMPL_RTL_STRINGDATA** ppThis,
-                                                          IMPL_RTL_STRINGDATA* 
pStr )
+template <class Traits, typename IMPL_RTL_STRINGDATA>
+void newReplaceChars(IMPL_RTL_STRINGDATA** ppThis, IMPL_RTL_STRINGDATA* pStr)
 {
     assert(ppThis);
     assert(pStr);
-    IMPL_RTL_STRINGDATA*    pOrg        = *ppThis;
-    bool                    bChanged    = false;
-    sal_Int32               nLen        = pStr->length;
-    const auto*             pCharStr    = pStr->buffer;
 
-    while ( nLen > 0 )
+    const auto pEnd = pStr->buffer + pStr->length;
+    auto pCharStr = std::find_if(pStr->buffer, pEnd, [](auto c) { return 
Traits::Applicable(c); });
+    if (pCharStr != pEnd)
     {
-        if ( rtl::isAsciiLowerCase(USTRCODE<IMPL_RTL_STRINGDATA>(*pCharStr)) )
+        IMPL_RTL_STRINGDATA* pOrg = *ppThis;
+        *ppThis = Alloc<IMPL_RTL_STRINGDATA>(pStr->length);
+        OSL_ASSERT(*ppThis != nullptr);
+        auto* pNewCharStr = (*ppThis)->buffer;
+        /* Copy String */
+        const sal_Int32 nCount = pCharStr - pStr->buffer;
+        Copy(pNewCharStr, pStr->buffer, nCount);
+        pNewCharStr += nCount;
+        /* replace/copy rest of the string */
+        do
         {
-            /* Copy String */
-            auto* pNewCharStr = NewCopy( ppThis, pStr, pCharStr-pStr->buffer );
-
-            /* replace/copy rest of the string */
-            if ( pNewCharStr )
-            {
-                *pNewCharStr = 
rtl::toAsciiUpperCase(USTRCODE<IMPL_RTL_STRINGDATA>(*pCharStr));
-                pNewCharStr++;
-                pCharStr++;
-                nLen--;
-
-                while ( nLen > 0 )
-                {
-                    *pNewCharStr = 
rtl::toAsciiUpperCase(USTRCODE<IMPL_RTL_STRINGDATA>(*pCharStr));
-
-                    pNewCharStr++;
-                    pCharStr++;
-                    nLen--;
-                }
-            }
-
-            bChanged = true;
-            break;
-        }
-
-        pCharStr++;
-        nLen--;
-    }
+            *pNewCharStr = Traits::Replace(*pCharStr);
+            pNewCharStr++;
+            pCharStr++;
+        } while (pCharStr != pEnd);
 
-    if ( !bChanged )
-    {
-        *ppThis = pStr;
-        acquire( pStr );
+        RTL_LOG_STRING_NEW(*ppThis);
+        /* must be done last, if pStr == *ppThis */
+        if (pOrg)
+            release(pOrg);
     }
-
-    RTL_LOG_STRING_NEW( *ppThis );
-    /* must be done last, if pStr == *ppThis */
-    if ( pOrg )
-        release( pOrg );
+    else
+        assign(ppThis, pStr);
 }
 
 /* ----------------------------------------------------------------------- */
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index f2503f5c2d06..04070b5588a9 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1321,13 +1321,13 @@ void SAL_CALL rtl_uString_newReplace(rtl_uString** 
ppThis, rtl_uString* pStr, sa
 void SAL_CALL rtl_uString_newToAsciiLowerCase(rtl_uString** ppThis, 
rtl_uString* pStr)
     SAL_THROW_EXTERN_C()
 {
-    rtl::str::newToAsciiLowerCase(ppThis, pStr);
+    rtl::str::newReplaceChars<rtl::str::ToAsciiLower>(ppThis, pStr);
 }
 
 void SAL_CALL rtl_uString_newToAsciiUpperCase(rtl_uString** ppThis, 
rtl_uString* pStr)
     SAL_THROW_EXTERN_C()
 {
-    rtl::str::newToAsciiUpperCase(ppThis, pStr);
+    rtl::str::newReplaceChars<rtl::str::ToAsciiUpper>(ppThis, pStr);
 }
 
 void SAL_CALL rtl_uString_newTrim(rtl_uString** ppThis, rtl_uString* pStr) 
SAL_THROW_EXTERN_C()

Reply via email to