From aabc5b0f7c8e9cf6bb711815a67ff6b6e900b5aa Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Tue, 1 Feb 2022 16:03:38 +0100 Subject: Slightly optimize truncateToLength and padToLength Change-Id: I7a18ce1f1c25e9f7a3c230109c741d6642786a13 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129256 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- include/comphelper/string.hxx | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) 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 +#include #include #include #include @@ -159,7 +160,7 @@ namespace detail template 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 B& padToLength(B& rBuffer, sal_Int32 nLen, - U cFill = '\0') + template 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; } } -- cgit