From: Sascha Schumann Date: Wed, 21 Apr 1999 16:31:27 +0000 (+0000) Subject: str_replace fix X-Git-Tag: BEFORE_PHP4_APACHE_MODULE_CHANGE~251 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a6e4ef3e31629a4060dffb1fcc3153b1854d4f7b;p=php str_replace fix --- diff --git a/ChangeLog.TODO b/ChangeLog.TODO index db7230a5fd..f4a1e8a347 100644 --- a/ChangeLog.TODO +++ b/ChangeLog.TODO @@ -23,7 +23,6 @@ over to PHP4. - PUT method support (mlemos@acm.org) - Add ldap_mod_add(), ldap_mod_del() and ldap_mod_replace() (Gerritt Thomson) - Fix parameter count problem in odbc_setoption() -- Replace broken str_replace(). The fix in 3.0.7 was bogus (Sascha Schumann) - Really fix implode() this time. The fix in 3.0.7 was bogus - Added more option to the date() function: (Colin Viebrock) 'g' - hour, 12-hour format, no leading zeros diff --git a/ext/standard/string.c b/ext/standard/string.c index 7f56d03a22..0750aa8734 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1234,6 +1234,7 @@ static void _php3_char_to_str(char *str,uint len,char from,char *to,int to_len,p *target = 0; } +#if 0 /* * this is a binary safe equivalent to strnstr * note that we don't check for the end in str_to_str but here @@ -1290,6 +1291,54 @@ static char *_php3_str_to_str(char *haystack, int length, if(_new_length) *_new_length = q - new; return new; } +#endif + +static char *_php3_memstr(char *s, char *c, size_t n, size_t m) +{ + char *p; + + for(p = s; (p - s) < n; p++) + if(memcmp(p, c, m) == 0) + return p; + return NULL; +} + +#define ATTCHSTR(st, sz) \ + nl += sz; \ + n = realloc(n, nl + 1); \ + memcpy(n + no, st, sz); \ + no += sz + + +static char *_php3_str_to_str(char *a, int al, char *b, int bl, char *c, int cl, + int *newlen) +{ + char *n = NULL, *p, *q; + int nl = 0; + int no = 0; + + /* run through all occurences of b in a */ + for(p = q = a; (p = _php3_memstr(p, b, al - (p - a), bl)); q = p) { + /* attach everything between the previous occ. and this one */ + ATTCHSTR(q, p - q); + /* attach the replacement string c */ + ATTCHSTR(c, cl); + /* jump over string b in a */ + p += bl; + } + + /* anything left over ? */ + if((al - (q - a)) > 0) { + ATTCHSTR(q, al - (q - a)); + } + + if(newlen) *newlen = nl; + n[nl] = '\0'; + + return n; +} + +#undef ATTCHSTR /* {{{ proto string str_replace(string needle, string str, string haystack) Replace all occurrences of needle in haystack with str */