sal/rtl/strtmpl.hxx |  132 ++++++++++++++++++----------------------------------
 1 file changed, 46 insertions(+), 86 deletions(-)

New commits:
commit a59e62abbd48a9e69dcfea574a84e70bd6983015
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Feb 27 12:02:20 2022 +0100
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sun Feb 27 13:21:16 2022 +0100

    More replacement functions deduplication
    
    Change-Id: I700074cddf1be937f5fdaf90e6cc0b533a3c57ad
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130633
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 5a468b8c9d90..f294c9c8b910 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1218,76 +1218,53 @@ void ensureCapacity                                ( 
IMPL_RTL_STRINGDATA** ppThi
 
 /* ----------------------------------------------------------------------- */
 
-template <typename IMPL_RTL_STRINGDATA>
+template <typename IMPL_RTL_STRINGDATA, typename IMPL_RTL_STRCODE>
 void newReplaceStrAt                                ( IMPL_RTL_STRINGDATA** 
ppThis,
                                                       IMPL_RTL_STRINGDATA* 
pStr,
                                                       sal_Int32 nIndex,
                                                       sal_Int32 nCount,
-                                                      IMPL_RTL_STRINGDATA* 
pNewSubStr )
+                                                      const IMPL_RTL_STRCODE* 
pNewSubStr,
+                                                      sal_Int32 nNewSubStrLen )
 {
     assert(ppThis);
     assert(nIndex >= 0 && nIndex <= pStr->length);
     assert(nCount >= 0);
     assert(nCount <= pStr->length - nIndex);
+    assert(pNewSubStr != nullptr || nNewSubStrLen == 0);
+    assert(nNewSubStrLen >= 0);
     /* Append? */
     if ( nIndex >= pStr->length )
-    {
-        /* newConcat test, if pNewSubStr is 0 */
-        newConcat( ppThis, pStr, pNewSubStr );
-        return;
-    }
-
-    /* negative index? */
-    if ( nIndex < 0 )
-    {
-        nCount -= nIndex;
-        nIndex = 0;
-    }
+        return newConcat(ppThis, pStr, pNewSubStr, nNewSubStrLen);
 
     /* not more than the String length could be deleted */
     if ( nCount >= pStr->length-nIndex )
     {
-        nCount = pStr->length-nIndex;
-
         /* Assign of NewSubStr? */
-        if ( !nIndex && (nCount >= pStr->length) )
-        {
-            if ( !pNewSubStr )
-                new_( ppThis );
-            else
-                assign( ppThis, pNewSubStr );
-            return;
-        }
+        if (nIndex == 0)
+            return newFromStr_WithLength( ppThis, pNewSubStr, nNewSubStrLen );
+
+        nCount = pStr->length - nIndex;
     }
 
     /* Assign of Str? */
-    if ( !nCount && (!pNewSubStr || !pNewSubStr->length) )
-    {
-        assign( ppThis, pStr );
-        return;
-    }
+    if ( !nCount && !nNewSubStrLen )
+        return assign(ppThis, pStr);
 
     IMPL_RTL_STRINGDATA*    pOrg = *ppThis;
-    sal_Int32               nNewLen;
-
-    /* Calculate length of the new string */
-    nNewLen = pStr->length-nCount;
-    if ( pNewSubStr )
-        nNewLen += pNewSubStr->length;
 
     /* Alloc New Buffer */
-    *ppThis = Alloc<IMPL_RTL_STRINGDATA>( nNewLen );
-    OSL_ASSERT(*ppThis != nullptr);
+    *ppThis = Alloc<IMPL_RTL_STRINGDATA>(pStr->length - nCount + 
nNewSubStrLen);
+    assert(*ppThis != nullptr);
     auto* pBuffer = (*ppThis)->buffer;
     if ( nIndex )
     {
         Copy( pBuffer, pStr->buffer, nIndex );
         pBuffer += nIndex;
     }
-    if ( pNewSubStr && pNewSubStr->length )
+    if ( nNewSubStrLen )
     {
-        Copy( pBuffer, pNewSubStr->buffer, pNewSubStr->length );
-        pBuffer += pNewSubStr->length;
+        Copy( pBuffer, pNewSubStr, nNewSubStrLen );
+        pBuffer += nNewSubStrLen;
     }
     Copy( pBuffer, pStr->buffer+nIndex+nCount, pStr->length-nIndex-nCount );
 
@@ -1299,64 +1276,56 @@ void newReplaceStrAt                                ( 
IMPL_RTL_STRINGDATA** ppTh
 
 /* ----------------------------------------------------------------------- */
 
-template <typename IMPL_RTL_STRINGDATA, typename IMPL_RTL_STRCODE>
+template <typename IMPL_RTL_STRINGDATA>
 void newReplaceStrAt                                ( IMPL_RTL_STRINGDATA** 
ppThis,
                                                       IMPL_RTL_STRINGDATA* 
pStr,
                                                       sal_Int32 nIndex,
                                                       sal_Int32 nCount,
-                                                      const IMPL_RTL_STRCODE* 
pNewSubStr,
-                                                      sal_Int32 nNewSubStrLen )
+                                                      IMPL_RTL_STRINGDATA* 
pNewSubStr )
 {
     assert(ppThis);
     assert(nIndex >= 0 && nIndex <= pStr->length);
     assert(nCount >= 0);
     assert(nCount <= pStr->length - nIndex);
-    assert(pNewSubStr != nullptr || nNewSubStrLen == 0);
-    assert(nNewSubStrLen >= 0);
     /* Append? */
-    if ( nIndex >= pStr->length )
-        return newConcat(ppThis, pStr, pNewSubStr, nNewSubStrLen);
+    if (nIndex >= pStr->length)
+    {
+        /* newConcat test, if pNewSubStr is 0 */
+        newConcat(ppThis, pStr, pNewSubStr);
+        return;
+    }
+
+    /* negative index? */
+    if (nIndex < 0)
+    {
+        nCount -= nIndex;
+        nIndex = 0;
+    }
 
     /* not more than the String length could be deleted */
-    if ( nCount >= pStr->length-nIndex )
+    if (nCount >= pStr->length-nIndex)
     {
         /* Assign of NewSubStr? */
         if (nIndex == 0)
-            return newFromStr_WithLength( ppThis, pNewSubStr, nNewSubStrLen );
-
+        {
+            if (!pNewSubStr)
+                return new_(ppThis);
+            else
+                return assign(ppThis, pNewSubStr);
+        }
         nCount = pStr->length - nIndex;
     }
 
     /* Assign of Str? */
-    if ( !nCount && !nNewSubStrLen )
-        return assign(ppThis, pStr);
-
-    IMPL_RTL_STRINGDATA*    pOrg = *ppThis;
-    sal_Int32               nNewLen;
-
-    /* Calculate length of the new string */
-    nNewLen = pStr->length-nCount + nNewSubStrLen;
-
-    /* Alloc New Buffer */
-    *ppThis = Alloc<IMPL_RTL_STRINGDATA>( nNewLen );
-    OSL_ASSERT(*ppThis != nullptr);
-    auto* pBuffer = (*ppThis)->buffer;
-    if ( nIndex )
+    if (!nCount && (!pNewSubStr || !pNewSubStr->length))
     {
-        Copy( pBuffer, pStr->buffer, nIndex );
-        pBuffer += nIndex;
-    }
-    if ( nNewSubStrLen )
-    {
-        Copy( pBuffer, pNewSubStr, nNewSubStrLen );
-        pBuffer += nNewSubStrLen;
+        assign(ppThis, pStr);
+        return;
     }
-    Copy( pBuffer, pStr->buffer+nIndex+nCount, pStr->length-nIndex-nCount );
 
-    RTL_LOG_STRING_NEW( *ppThis );
-    /* must be done last, if pStr or pNewSubStr == *ppThis */
-    if ( pOrg )
-        release( pOrg );
+    const auto* pNewSubStrBuf = pNewSubStr ? pNewSubStr->buffer : nullptr;
+    const sal_Int32 nNewSubStrLength = pNewSubStr ? pNewSubStr->length : 0;
+    newReplaceStrAt(ppThis, pStr, nIndex, nCount, pNewSubStrBuf, 
nNewSubStrLength);
 }
 
 /* ----------------------------------------------------------------------- */
@@ -1600,16 +1569,7 @@ void newReplaceFirst(S** s, S* s1, CharTypeFrom const* 
from, sal_Int32 fromLengt
         if (s1->length - fromLength > SAL_MAX_INT32 - toLength)
             std::abort();
         i += fromIndex;
-        const sal_Int32 n = s1->length + (toLength - fromLength);
-        const auto pOld = *s;
-        *s = Alloc<S>(n);
-        if (i)
-            Copy((*s)->buffer, s1->buffer, i);
-        if (toLength)
-            Copy((*s)->buffer + i, to, toLength);
-        Copy((*s)->buffer + i + toLength, s1->buffer + i + fromLength, 
s1->length - i - fromLength);
-        if (pOld)
-            release(pOld); // Must be last in case *s == s1
+        newReplaceStrAt(s, s1, i, fromLength, to, toLength);
     }
     else
         assign(s, s1);

Reply via email to