- make dynstring independed from NUL terminating in strings where possible

This commit is contained in:
zolliker
2009-11-10 10:35:49 +00:00
parent 5e9d28037c
commit 9d8f3d89f0

View File

@ -149,7 +149,7 @@ int DynStringConcat(pDynString self, char *pText)
} }
} }
strcat(self->pBuffer, pText); strcpy(self->pBuffer + self->iTextLen, pText);
self->iTextLen = iRequested; self->iTextLen = iRequested;
return 1; return 1;
} }
@ -179,7 +179,6 @@ int DynStringConcatChar(pDynString self, char c)
int DynStringInsert(pDynString self, char *pText, int iPos) int DynStringInsert(pDynString self, char *pText, int iPos)
{ {
int iRequested, iRet, iPush, iRest; int iRequested, iRet, iPush, iRest;
char *pPtr;
assert(self); assert(self);
assert(self->iMAGIC == DYNMAGIC); 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 */ if (iPos >= self->iTextLen) {
iRest = self->iTextLen - iPos; iPos = self->iTextLen;
pPtr = (char *) malloc((iRest + 10) * sizeof(char)); } else {
if (!pPtr) { if (iPos < 0) {
return 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 */ memcpy(&self->pBuffer[iPos], pText, iPush);
memset(&self->pBuffer[iPos], 0, iRest * sizeof(char));
strcat(self->pBuffer, pText);
strcat(self->pBuffer, pPtr);
free(pPtr);
self->iTextLen = iRequested; self->iTextLen = iRequested;
return 1; return 1;
} }
@ -229,7 +224,9 @@ int DynStringReplace(pDynString self, char *pText, int iPos)
} }
memcpy(&self->pBuffer[iPos], pText, strlen(pText) * sizeof(char)); memcpy(&self->pBuffer[iPos], pText, strlen(pText) * sizeof(char));
self->iTextLen = strlen(self->pBuffer); if (iRequested > self->iTextLen) {
self->iTextLen = iRequested;
}
return 1; return 1;
} }