Skip to content
Permalink
Browse files
Add a msStringBuffer API to offer more efficient string concatenation
  • Loading branch information
rouault committed Oct 24, 2019
1 parent d89ac1f commit 9bb379794e63e654de408cc416db692424da20e4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
@@ -2275,6 +2275,13 @@ void msPopulateTextSymbolForLabelAndString(textSymbolObj *ts, labelObj *l, char
MS_DLL_EXPORT char* msStringEscape( const char * pszString );
MS_DLL_EXPORT int msStringInArray( const char * pszString, char **array, int numelements);

typedef struct msStringBuffer msStringBuffer;
MS_DLL_EXPORT msStringBuffer* msStringBufferAlloc(void);
MS_DLL_EXPORT void msStringBufferFree(msStringBuffer* sb);
MS_DLL_EXPORT const char* msStringBufferGetString(msStringBuffer* sb);
MS_DLL_EXPORT char* msStringBufferReleaseStringAndFree(msStringBuffer* sb);
MS_DLL_EXPORT int msStringBufferAppend(msStringBuffer* sb, const char* pszAppendedString);

#ifndef HAVE_STRRSTR
MS_DLL_EXPORT char *strrstr(const char *string, const char *find);
#endif /* NEED_STRRSTR */
@@ -2232,3 +2232,82 @@ int msLayerEncodeShapeAttributes( layerObj *layer, shapeObj *shape) {
return MS_FAILURE;
#endif
}

/************************************************************************/
/* msStringBuffer */
/************************************************************************/

struct msStringBuffer
{
size_t alloc_size;
size_t length;
char *str;
};

/************************************************************************/
/* msStringBufferAlloc() */
/************************************************************************/

msStringBuffer* msStringBufferAlloc(void)
{
return (msStringBuffer*)msSmallCalloc(sizeof(msStringBuffer), 1);
}

/************************************************************************/
/* msStringBufferFree() */
/************************************************************************/

void msStringBufferFree(msStringBuffer* sb)
{
if( sb )
msFree(sb->str);
msFree(sb);
}

/************************************************************************/
/* msStringBufferGetString() */
/************************************************************************/

const char* msStringBufferGetString(msStringBuffer* sb)
{
return sb->str;
}

/************************************************************************/
/* msStringBufferReleaseStringAndFree() */
/************************************************************************/

char* msStringBufferReleaseStringAndFree(msStringBuffer* sb)
{
char* str = sb->str;
sb->str = NULL;
sb->alloc_size = 0;
sb->length = 0;
msStringBufferFree(sb);
return str;
}

/************************************************************************/
/* msStringBufferAppend() */
/************************************************************************/

int msStringBufferAppend(msStringBuffer* sb, const char* pszAppendedString)
{
size_t nAppendLen = strlen(pszAppendedString);
if( sb->length + nAppendLen >= sb->alloc_size )
{
size_t newAllocSize1 = sb->alloc_size + sb->alloc_size / 3;
size_t newAllocSize2 = sb->length + nAppendLen + 1;
size_t newAllocSize = MAX(newAllocSize1, newAllocSize2);
void* newStr = realloc(sb->str, newAllocSize);
if( newStr == NULL ) {
msSetError(MS_MEMERR, "Not enough memory", "msStringBufferAppend()");
return MS_FAILURE;
}
sb->alloc_size = newAllocSize;
sb->str = (char*) newStr;
}
memcpy(sb->str + sb->length, pszAppendedString, nAppendLen + 1);
sb->length += nAppendLen;
return MS_SUCCESS;
}

0 comments on commit 9bb3797

Please sign in to comment.