summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2022-02-01 16:03:38 +0100
committerMike Kaganski <mike.kaganski@collabora.com>2022-02-01 20:39:16 +0100
commitaabc5b0f7c8e9cf6bb711815a67ff6b6e900b5aa (patch)
tree022ebd172cf54092e1c5a5ce9b4a71348db2a125
parent5e8ceac64b66d1298037b939350d3adb86b37752 (diff)
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.kaganski@collabora.com>
-rw-r--r--include/comphelper/string.hxx16
1 files 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 <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;
}
}