diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2022-02-25 20:38:23 +0100 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2022-02-25 22:56:17 +0100 |
commit | cbb1241ebb7488aa584825645d399b7ee83272fb (patch) | |
tree | 9b6e5264494af24c8ffcea38fb4e42f9cf554f76 /sal | |
parent | ffb2e5520eff008d4e784fa6b15f4e2baa534e7a (diff) |
Slightly optimize newReplaceAllFromIndex
Change-Id: Ie73a5e8c44458123ef03096e3e8f6c9e41c48814
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130466
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/rtl/strtmpl.hxx | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx index 598d214878d6..348b84fab246 100644 --- a/sal/rtl/strtmpl.hxx +++ b/sal/rtl/strtmpl.hxx @@ -1695,19 +1695,20 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom const* from, sal_Int32 fr { if (s1->length - fromLength > SAL_MAX_INT32 - toLength) std::abort(); - acquire(s1); // in case *s == s1 + i += fromIndex; sal_Int32 nCapacity = s1->length + (toLength - fromLength); if (fromLength < toLength) { // Pre-allocate up to 16 replacements more - const sal_Int32 nMaxMoreFinds = (s1->length - fromIndex - i - fromLength) / fromLength; + const sal_Int32 nMaxMoreFinds = (s1->length - i - fromLength) / fromLength; const sal_Int32 nIncrease = toLength - fromLength; const sal_Int32 nMoreReplacements = std::min( { nMaxMoreFinds, (SAL_MAX_INT32 - nCapacity) / nIncrease, sal_Int32(16) }); nCapacity += nMoreReplacements * nIncrease; } - new_WithLength(s, nCapacity); - i += fromIndex; + const auto pOld = *s; + *s = Alloc<S>(nCapacity); + (*s)->length = 0; fromIndex = 0; do { @@ -1718,7 +1719,8 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom const* from, sal_Int32 fr } while (i >= 0); // the rest detail::append(s, &nCapacity, s1->buffer + fromIndex, s1->length - fromIndex); - release(s1); + if (pOld) + release(pOld); // Must be last in case *s == s1 } else assign(s, s1); |