From fa2c482a45394623388652c77fd95e86944aa7d4 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 13 Mar 2015 16:11:58 +0100 Subject: [PATCH] (stdstring) Use old function --- libretro-common/string/stdstring.c | 61 +++++++++++++++--------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/libretro-common/string/stdstring.c b/libretro-common/string/stdstring.c index 5ef73b283e..5c74467e29 100644 --- a/libretro-common/string/stdstring.c +++ b/libretro-common/string/stdstring.c @@ -22,44 +22,45 @@ #include -char *string_replace_substring(const char *in, const char *pattern, const char *by) +char *string_replace_substring(const char *in, const char *pattern, const char *replacement) { - char *needle = NULL, *res = NULL; - size_t outsize = 0; - size_t resoffset = 0; + char *needle = NULL; + char *newstr = NULL; + char *head = NULL; - if (!in) - return NULL; + /* if either pattern or replacement is NULL, + * duplicate in and let caller handle it. */ + if (!pattern || !replacement) + return strdup(in); - outsize = strlen(in) + 1; + newstr = strdup(in); + head = newstr; - /* TODO maybe avoid reallocating by counting the - * non-overlapping occurences of pattern */ - res = (char*)malloc(outsize); - - if (!res) - return NULL; - - while ((needle = strstr(in, pattern))) + while ((needle = strstr (head, pattern))) { - /* copy everything up to the pattern */ - memcpy(res + resoffset, in, needle - in); - resoffset += needle - in; + char* oldstr = newstr; + newstr = (char*)malloc( + strlen(oldstr) - strlen(pattern) + strlen(replacement) + 1); - /* skip the pattern in the input-string */ - in = needle + strlen(pattern); + if (!newstr) + { + /* Failed to allocate memory, + * free old string and return NULL. */ + free(oldstr); + return NULL; + } - /* adjust space for replacement */ - outsize = outsize - strlen(pattern) + strlen(by); - res = realloc(res, outsize); + memcpy(newstr, oldstr, needle - oldstr); + memcpy(newstr + (needle - oldstr), replacement, strlen(replacement)); + memcpy(newstr + (needle - oldstr) + strlen(replacement), + needle + strlen(pattern), + strlen(oldstr) - strlen(pattern) - (needle - oldstr)); + newstr[strlen(oldstr) - strlen(pattern) + strlen(replacement)] = '\0'; - /* copy the pattern */ - memcpy(res + resoffset, by, strlen(by)); - resoffset += strlen(by); + /* Move back head right after the last replacement. */ + head = newstr + (needle - oldstr) + strlen(replacement); + free(oldstr); } - /* copy the remaining input */ - strcpy(res + resoffset, in); - - return res; + return newstr; }