]> granicus.if.org Git - php/commitdiff
Short circuit str_replaces when we already know that needle does not occur in haystack.
authorSara Golemon <pollita@php.net>
Mon, 23 Feb 2004 20:13:14 +0000 (20:13 +0000)
committerSara Golemon <pollita@php.net>
Mon, 23 Feb 2004 20:13:14 +0000 (20:13 +0000)
Note: Prior bugfix was for #27176 not #27276

ext/standard/string.c

index 05649e274293a5222e4ac25c642bdcbf200ac343..aa9f212b2a73981da1f0d436663fbf1cc454ea8f 100644 (file)
@@ -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;