Kaydet (Commit) dead246b authored tarafından Noel Grandin's avatar Noel Grandin Kaydeden (comit) Noel Grandin

avoid writing StringBuffer twice

Change-Id: Ib0a8914cc1967f89a2cd3b062e7a0b52de7a972c
Reviewed-on: https://gerrit.libreoffice.org/58281
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst bdb0775a
......@@ -21,6 +21,7 @@
#include <osl/interlck.h>
#include <rtl/strbuf.hxx>
#include "strimp.hxx"
/*************************************************************************
* rtl_stringbuffer_newFromStr_WithLength
......@@ -37,9 +38,13 @@ void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
return;
}
rtl_string_new_WithLength( newStr, count + 16 );
// use raw alloc to avoid overwriting the buffer twice
if ( *newStr)
rtl_string_release( *newStr );
*newStr = rtl_string_ImplAlloc( count + 16 );
(*newStr)->length = count;
memcpy( (*newStr)->buffer, value, count );
memset( (*newStr)->buffer + count, 0, 16 );
}
/*************************************************************************
......@@ -78,16 +83,19 @@ void SAL_CALL rtl_stringbuffer_ensureCapacity
{
rtl_String * pTmp = *This;
rtl_String * pNew = nullptr;
*capacity = ((*This)->length + 1) * 2;
auto nLength = (*This)->length;
*capacity = (nLength + 1) * 2;
if (minimumCapacity > *capacity)
/* still lower, set to the minimum capacity */
*capacity = minimumCapacity;
rtl_string_new_WithLength(&pNew, *capacity);
pNew->length = (*This)->length;
// use raw alloc to avoid overwriting the buffer twice
pNew = rtl_string_ImplAlloc( *capacity );
pNew->length = nLength;
*This = pNew;
memcpy( (*This)->buffer, pTmp->buffer, pTmp->length );
memcpy( (*This)->buffer, pTmp->buffer, nLength );
memset( (*This)->buffer + nLength, 0, *capacity - nLength );
rtl_string_release( pTmp );
}
}
......
......@@ -25,8 +25,9 @@
#include <sys/sdt.h>
#endif
#include <sal/types.h>
#include <rtl/string.hxx>
#include <rtl/ustring.hxx>
/* ======================================================================= */
/* Help functions for String and UString */
......@@ -49,6 +50,10 @@ sal_Int16 rtl_ImplGetDigit( sal_Unicode ch, sal_Int16 nRadix );
bool rtl_ImplIsWhitespace( sal_Unicode c );
rtl_uString* rtl_uString_ImplAlloc( sal_Int32 nLen );
rtl_String* rtl_string_ImplAlloc( sal_Int32 nLen );
extern "C" {
typedef void *(*rtl_allocateStringFn)(sal_Size size);
......
......@@ -1134,7 +1134,7 @@ sal_uInt64 SAL_CALL IMPL_RTL_STRNAME( toUInt64 )( const IMPL_RTL_STRCODE* pStr,
/* Internal String-Class help functions */
/* ======================================================================= */
static IMPL_RTL_STRINGDATA* IMPL_RTL_STRINGNAME( ImplAlloc )( sal_Int32 nLen )
IMPL_RTL_STRINGDATA* IMPL_RTL_STRINGNAME( ImplAlloc )( sal_Int32 nLen )
{
IMPL_RTL_STRINGDATA * pData
= (sal::static_int_cast< sal_uInt32 >(nLen)
......
......@@ -41,9 +41,13 @@ void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
return;
}
rtl_uString_new_WithLength( newStr, count + 16 );
// use raw alloc to avoid overwriting the buffer twice
if ( *newStr)
rtl_uString_release( *newStr );
*newStr = rtl_uString_ImplAlloc( count + 16 );
(*newStr)->length = count;
memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode) );
memset( (*newStr)->buffer + count, 0, 16 * sizeof(sal_Unicode) );
RTL_LOG_STRING_NEW( *newStr );
}
......@@ -103,16 +107,19 @@ void SAL_CALL rtl_uStringbuffer_ensureCapacity
{
rtl_uString * pTmp = *This;
rtl_uString * pNew = nullptr;
*capacity = ((*This)->length + 1) * 2;
auto nLength = (*This)->length;
*capacity = (nLength + 1) * 2;
if (minimumCapacity > *capacity)
/* still lower, set to the minimum capacity */
*capacity = minimumCapacity;
rtl_uString_new_WithLength(&pNew, *capacity);
pNew->length = (*This)->length;
// use raw alloc to avoid overwriting the buffer twice
pNew = rtl_uString_ImplAlloc( *capacity );
pNew->length = nLength;
*This = pNew;
memcpy( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
memcpy( (*This)->buffer, pTmp->buffer, nLength * sizeof(sal_Unicode) );
memset( (*This)->buffer + nLength, 0, (*capacity - nLength) * sizeof(sal_Unicode) );
RTL_LOG_STRING_NEW( pTmp ); // with accurate contents
rtl_uString_release( pTmp );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment