What kind of bizarre spacing style is that?

And who decided to memcpy a single byte? I suspect it can be merged into the memcpy above, but I'll have to figure out what exactly it does first...
This commit is contained in:
Alcaro 2015-03-12 22:39:14 +01:00
parent 4d62a1883e
commit 48f8f02a2f
1 changed files with 12 additions and 13 deletions

View File

@ -268,7 +268,7 @@ static int xmb_entry_iterate(unsigned action)
static char *xmb_str_replace (const char *string, static char *xmb_str_replace (const char *string,
const char *substr, const char *replacement) const char *substr, const char *replacement)
{ {
char *tok, *newstr, *oldstr, *head; char *tok, *newstr, *head;
/* if either substr or replacement is NULL, /* if either substr or replacement is NULL,
* duplicate string a let caller handle it. */ * duplicate string a let caller handle it. */
@ -278,9 +278,9 @@ static char *xmb_str_replace (const char *string,
newstr = strdup(string); newstr = strdup(string);
head = newstr; head = newstr;
while ( (tok = strstr ( head, substr ))) while ((tok = strstr (head, substr)))
{ {
oldstr = newstr; char* oldstr = newstr;
newstr = (char*)malloc( newstr = (char*)malloc(
strlen(oldstr) - strlen(substr) + strlen(replacement) + 1); strlen(oldstr) - strlen(substr) + strlen(replacement) + 1);
@ -288,21 +288,20 @@ static char *xmb_str_replace (const char *string,
{ {
/* Failed to allocate memory, /* Failed to allocate memory,
* free old string and return NULL. */ * free old string and return NULL. */
free (oldstr); free(oldstr);
return NULL; return NULL;
} }
memcpy(newstr, oldstr, tok - oldstr ); memcpy(newstr, oldstr, tok - oldstr);
memcpy(newstr + (tok - oldstr), replacement, strlen ( replacement ) ); memcpy(newstr + (tok - oldstr), replacement, strlen(replacement));
memcpy(newstr + (tok - oldstr) + strlen( replacement ), tok + memcpy(newstr + (tok - oldstr) + strlen(replacement),
strlen ( substr ), strlen ( oldstr ) - tok + strlen(substr),
strlen ( substr ) - ( tok - oldstr ) ); strlen(oldstr) - strlen(substr) - (tok - oldstr));
memset(newstr + strlen ( oldstr ) - strlen ( substr ) + newstr[strlen(oldstr) - strlen(substr) + strlen(replacement)] = '\0';
strlen ( replacement ) , 0, 1 );
/* Move back head right after the last replacement. */ /* Move back head right after the last replacement. */
head = newstr + (tok - oldstr) + strlen( replacement ); head = newstr + (tok - oldstr) + strlen(replacement);
free (oldstr); free(oldstr);
} }
return newstr; return newstr;