MCPcopy Create free account
hub / github.com/TASEmulators/fceux / str_replace

Function str_replace

src/utils/xstring.cpp:160–181  ·  view source on GitHub ↗

Replaces all instances of 'search' with 'replace' Returns number of sub-strings modified, or -1 on error

Source from the content-addressed store, hash-verified

158///Replaces all instances of 'search' with 'replace'
159///Returns number of sub-strings modified, or -1 on error
160int str_replace(char *str, const char *search, const char *replace) {
161 unsigned int i=0,j=0; //mbg merge 7/17/06 changed to unsigned int
162 int searchlen,replacelen;
163 char *astr;
164
165 searchlen = strlen(search);
166 replacelen = strlen(replace);
167 if ((!strlen(str)) || (!searchlen)) return -1; //note: allow *replace to have a length of zero!
168 if (!(astr = (char*)malloc(strlen(str)+1))) return -1;
169 while (i < strlen(str)) {
170 if (!strncmp(str+i,search,searchlen)) {
171 if (replacelen) memcpy(astr+j,replace,replacelen);
172 i += searchlen;
173 j += replacelen;
174 }
175 else astr[j++] = str[i++];
176 }
177 astr[j] = 0;
178 strcpy(str,astr);
179 free(astr);
180 return j;
181}
182
183static const struct Base64Table
184{

Callers 1

AssembleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected