Skip to content

Commit

Permalink
Fixed strreplace. Again.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpaterakis committed Aug 22, 2022
1 parent d35c676 commit 6e63e28
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions string-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,14 @@ char* strreplace(char* origString, const char* searchString, const char* replace
strcpy(tmpString, origString);

while (strstr(searchIndex, searchString) != NULL) {

searchIndex = strstr(searchIndex, searchString);
*searchIndex = '\0';

*(tmpString + strlen(origString)) = '\0';
*(tmpString + (searchIndex - origString)) = '\0';

tmpString = realloc(tmpString, strlen(tmpString) + strlen(replaceString) + 1);
strcat(tmpString, replaceString);
int offset = strlen(searchString);
if (offset < strlen(replaceString)) {
offset = strlen(replaceString);
}
searchIndex += offset;
searchIndex += strlen(searchString);

tmpString = realloc(tmpString, strlen(tmpString) + strlen(searchIndex) + 1);
strcat(tmpString, searchIndex);
Expand Down

0 comments on commit 6e63e28

Please sign in to comment.