include/comphelper/string.hxx |   16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

New commits:
commit aabc5b0f7c8e9cf6bb711815a67ff6b6e900b5aa
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Tue Feb 1 16:03:38 2022 +0100
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Tue Feb 1 20:39:16 2022 +0100

    Slightly optimize truncateToLength and padToLength
    
    Change-Id: I7a18ce1f1c25e9f7a3c230109c741d6642786a13
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129256
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/include/comphelper/string.hxx b/include/comphelper/string.hxx
index d4c9ccd1b947..5e5c4c4a28ab 100644
--- a/include/comphelper/string.hxx
+++ b/include/comphelper/string.hxx
@@ -22,6 +22,7 @@
 
 #include <sal/config.h>
 
+#include <algorithm>
 #include <vector>
 #include <comphelper/comphelperdllapi.h>
 #include <sal/types.h>
@@ -159,7 +160,7 @@ namespace detail
     template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
     {
         if (nLen < rBuffer.getLength())
-            rBuffer.remove(nLen, rBuffer.getLength()-nLen);
+            rBuffer.setLength(nLen);
         return rBuffer;
     }
 }
@@ -184,16 +185,11 @@ inline OUStringBuffer& truncateToLength(
 
 namespace detail
 {
-    template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
-        U cFill = '\0')
+    template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 
nLen, U cFill)
     {
-        sal_Int32 nOrigLen = rBuffer.getLength();
-        if (nLen > nOrigLen)
-        {
-            rBuffer.setLength(nLen);
-            for (sal_Int32 i = nOrigLen; i < nLen; ++i)
-                rBuffer[i] = cFill;
-        }
+        const sal_Int32 nPadLen = nLen - rBuffer.getLength();
+        if (nPadLen > 0)
+            std::fill_n(rBuffer.appendUninitialized(nPadLen), nPadLen, cFill);
         return rBuffer;
     }
 }

Reply via email to