computerwhiz
Programmer
Is there a faster way to insert a particular string into a string at a certain position? Here is the code I am using now. <pszText> is a static global variable accessed in several functions. My program is running to slow, would reallocation be faster than what I am doing now?
Code:
void Insert(int iLen, LPSTR String)
{
if (iLen < 0 || iLen > lstrlen(pszText) || String == 0X00)
{
return;
}
LPSTR NewText = (LPSTR)GlobalAlloc(GPTR,
lstrlen(String)+ lstrlen(pszText) + 2);
if (iLen > 0)
{
CopyMemory(NewText, pszText, iLen);
}
CopyMemory(NewText+iLen, String, lstrlen(String));
if (lstrlen(pszText)-iLen > 0)
{
CopyMemory(NewText+iLen+lstrlen(String), pszText+iLen,
lstrlen(pszText)-iLen);
}
GlobalFree(pszText);
pszText = (LPSTR)GlobalAlloc(GPTR, lstrlen(NewText) + 2);
lstrcpy(pszText, NewText);
pszText[lstrlen(NewText)+1] = 0x00;
GlobalFree(NewText);
}