pac_utils.h (pacparser-1.3.9) | : | pac_utils.h (pacparser-1.4.0) | ||
---|---|---|---|---|
skipping to change at line 338 | skipping to change at line 338 | |||
"function findProxyForURL(url, host) {\n" | "function findProxyForURL(url, host) {\n" | |||
" if (typeof FindProxyForURLEx == 'function') {\n" | " if (typeof FindProxyForURLEx == 'function') {\n" | |||
" return FindProxyForURLEx(url, host);\n" | " return FindProxyForURLEx(url, host);\n" | |||
" } else {\n" | " } else {\n" | |||
" return FindProxyForURL(url, host);\n" | " return FindProxyForURL(url, host);\n" | |||
" }\n" | " }\n" | |||
"}\n"; | "}\n"; | |||
// You must free the result if result is non-NULL. | // You must free the result if result is non-NULL. | |||
char *str_replace(const char *orig, char *rep, char *with) { | char *str_replace(const char *orig, char *rep, char *with) { | |||
char *tmporig = malloc(strlen(orig) + 1); // Copy of orig that we work with | ||||
tmporig = strcpy(tmporig, orig); | ||||
char *result; // the return string | char *result; // the return string | |||
char *ins; // the next insert point | char *ins; // the next insert point | |||
char *tmp; // varies | char *tmp; // varies | |||
int count; // number of replacements | int count; // number of replacements | |||
int len_front; // distance between rep and end of last rep | int len_front; // distance between rep and end of last rep | |||
int len_rep = strlen(rep); | int len_rep = strlen(rep); | |||
int len_with = strlen(with); | int len_with = strlen(with); | |||
// Get the count of replacements | // Get the count of replacements | |||
ins = tmporig; | ins = orig; | |||
for (count = 0; (tmp = strstr(ins, rep)); ++count) { | for (count = 0; (tmp = strstr(ins, rep)); ++count) { | |||
ins = tmp + len_rep; | ins = tmp + len_rep; | |||
} | } | |||
tmp = result = malloc(strlen(tmporig) + (len_with - len_rep) * count + 1); | tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); | |||
// first time through the loop, all the variable are set correctly | // first time through the loop, all the variable are set correctly | |||
// from here on, | // from here on, | |||
// tmp points to the end of the result string | // tmp points to the end of the result string | |||
// ins points to the next occurrence of rep in tmporig | // ins points to the next occurrence of rep in orig | |||
// tmporig points to the remainder of tmporig after "end of rep" | // orig points to the remainder of orig after "end of rep" | |||
while (count--) { | while (count--) { | |||
ins = strstr(tmporig, rep); | ins = strstr(orig, rep); | |||
len_front = ins - tmporig; | len_front = ins - orig; | |||
tmp = strncpy(tmp, tmporig, len_front) + len_front; | tmp = strncpy(tmp, orig, len_front) + len_front; | |||
tmp = strcpy(tmp, with) + len_with; | tmp = strcpy(tmp, with) + len_with; | |||
tmporig += len_front + len_rep; // move to next "end of rep" | orig += len_front + len_rep; // move to next "end of rep" | |||
} | } | |||
strcpy(tmp, tmporig); | ||||
// Copy the remaining string. | ||||
strcpy(tmp, orig); | ||||
return result; | return result; | |||
} | } | |||
End of changes. 7 change blocks. | ||||
12 lines changed or deleted | 11 lines changed or added |