diff --git a/dynstring.c b/dynstring.c index 7269eb2b..a24136d4 100644 --- a/dynstring.c +++ b/dynstring.c @@ -149,7 +149,7 @@ int DynStringConcat(pDynString self, char *pText) } } - strcat(self->pBuffer, pText); + strcpy(self->pBuffer + self->iTextLen, pText); self->iTextLen = iRequested; return 1; } @@ -179,7 +179,6 @@ int DynStringConcatChar(pDynString self, char c) int DynStringInsert(pDynString self, char *pText, int iPos) { int iRequested, iRet, iPush, iRest; - char *pPtr; assert(self); assert(self->iMAGIC == DYNMAGIC); @@ -194,20 +193,16 @@ int DynStringInsert(pDynString self, char *pText, int iPos) } } - /* we need a temporary buffer to hold the backend of the string */ - iRest = self->iTextLen - iPos; - pPtr = (char *) malloc((iRest + 10) * sizeof(char)); - if (!pPtr) { - return 0; + if (iPos >= self->iTextLen) { + iPos = self->iTextLen; + } else { + if (iPos < 0) { + iPos = 0; + } + memmove(&self->pBuffer[iPos + iPush], &self->pBuffer[iPos], self->iTextLen + 1 - iPos); } - memset(pPtr, 0, (iRest + 10) * sizeof(char)); - strcpy(pPtr, &self->pBuffer[iPos]); - - /* OK build the result string */ - memset(&self->pBuffer[iPos], 0, iRest * sizeof(char)); - strcat(self->pBuffer, pText); - strcat(self->pBuffer, pPtr); - free(pPtr); + + memcpy(&self->pBuffer[iPos], pText, iPush); self->iTextLen = iRequested; return 1; } @@ -229,7 +224,9 @@ int DynStringReplace(pDynString self, char *pText, int iPos) } memcpy(&self->pBuffer[iPos], pText, strlen(pText) * sizeof(char)); - self->iTextLen = strlen(self->pBuffer); + if (iRequested > self->iTextLen) { + self->iTextLen = iRequested; + } return 1; }