From: Sara Golemon Date: Mon, 23 Feb 2004 20:13:14 +0000 (+0000) Subject: Short circuit str_replaces when we already know that needle does not occur in haystack. X-Git-Tag: RELEASE_0_2_0~205 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1c827976646f32357e9638e422ceac31698b1407;p=php Short circuit str_replaces when we already know that needle does not occur in haystack. Note: Prior bugfix was for #27176 not #27276 --- diff --git a/ext/standard/string.c b/ext/standard/string.c index 05649e2742..aa9f212b2a 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -3008,7 +3008,16 @@ PHPAPI char *php_str_to_str_ex(char *haystack, int length, o += needle_len; count++; } - new_str = safe_emalloc(count, str_len - needle_len, length + 1); + if (count == 0) { + /* Needle doesn't occur, shortcircuit the actual replacement. */ + new_str = estrndup(haystack, length); + if (_new_length) { + *_new_length = length; + } + return new_str; + } else { + new_str = safe_emalloc(count, str_len - needle_len, length + 1); + } } e = s = new_str;