Introduce OUStringBuffer::insert taking OUStringConcat

Avoids some (re)allocations, and aligns with already existing append

Change-Id: I536ba50f56fc560c0f6e8c0a8b65bd4248896a8e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148777
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski
2023-03-13 13:05:41 +00:00
parent cd50669485
commit 2cb2e2b9c1
2 changed files with 40 additions and 12 deletions
+33
View File
@@ -43,6 +43,7 @@
#include "sal/types.h"
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
#include "o3tl/safeint.hxx"
#include "rtl/stringconcat.hxx"
#endif
@@ -1015,6 +1016,38 @@ public:
}
#endif
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
/**
@overload
@internal
*/
template <typename T1, typename T2>
OUStringBuffer& insert(sal_Int32 offset, OUStringConcat<T1, T2>&& c)
{
const size_t l = c.length();
if (l == 0)
return *this;
if (l > o3tl::make_unsigned(std::numeric_limits<sal_Int32>::max() - pData->length))
throw std::bad_alloc();
rtl_uStringbuffer_insert(&pData, &nCapacity, offset, nullptr, l);
/* insert the new characters */
c.addData(pData->buffer + offset);
return *this;
}
/**
@overload
@internal
*/
template <typename T, std::size_t N>
OUStringBuffer& insert(sal_Int32 offset, StringNumberBase<sal_Unicode, T, N>&& c)
{
return insert(offset, c.buf, c.length);
}
#endif
/**
Inserts the string representation of the <code>char</code> array
argument into this string buffer.
+7 -12
View File
@@ -709,7 +709,7 @@ OUString SvNumberformat::ImpObtainCalendarAndNumerals( OUStringBuffer& rString,
if ( nNumeralID >= 0x02 && nNumeralID <= 0x13 )
nNatNum = 1;
if ( nNatNum )
rString.insert( nPos, Concat2View("[NatNum"+OUString::number(nNatNum)+"]"));
rString.insert(nPos, "[NatNum" + OUString::number(nNatNum) + "]");
return sCalendar;
}
@@ -1101,11 +1101,8 @@ SvNumberformat::SvNumberformat(OUString& rString,
sBuff.remove(nPosOld, nPos - nPosOld);
if (!sStr.isEmpty())
{
sBuff.insert(nPosOld, sStr);
nPos = nPosOld + sStr.getLength();
sBuff.insert(nPos, "]");
sBuff.insert(nPosOld, "[");
nPos += 2;
sBuff.insert(nPosOld, "[" + sStr + "]");
nPos = nPosOld + sStr.getLength() + 2;
nPosOld = nPos; // position before string
}
else
@@ -5210,18 +5207,16 @@ static void lcl_insertLCID( OUStringBuffer& rFormatStr, sal_uInt32 nLCID, sal_In
// No format code, no locale.
return;
OUStringBuffer aLCIDString = OUString::number( nLCID , 16 ).toAsciiUpperCase();
auto aLCIDString = OUString::number( nLCID , 16 ).toAsciiUpperCase();
// Search for only last DBNum which is the last element before insertion position
if ( bDBNumInserted && nPosInsertLCID >= 8
&& aLCIDString.getLength() > 4
&& rFormatStr.indexOf( "[DBNum", nPosInsertLCID-8) == nPosInsertLCID-8 )
&& aLCIDString.length > 4
&& OUString::unacquired(rFormatStr).match( "[DBNum", nPosInsertLCID-8) )
{ // remove DBNumX code if long LCID
nPosInsertLCID -= 8;
rFormatStr.remove( nPosInsertLCID, 8 );
}
aLCIDString.insert( 0, "[$-" );
aLCIDString.append( "]" );
rFormatStr.insert( nPosInsertLCID, aLCIDString );
rFormatStr.insert( nPosInsertLCID, "[$-" + aLCIDString + "]" );
}
/** Increment nAlphabetID for CJK numerals