iliaa           Sat Jun 10 15:29:07 2006 UTC

  Modified files:              (Branch: PHP_5_2)
    /php-src/ext/standard       string.c 
    /php-src    NEWS 
  Log:
  Improved performance of str_replace() when doing 1 char to 1 char or 1 char
  to many chars replacement by 30-40%.
  
  
http://cvs.php.net/viewcvs.cgi/php-src/ext/standard/string.c?r1=1.445.2.14.2.1&r2=1.445.2.14.2.2&diff_format=u
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.445.2.14.2.1 
php-src/ext/standard/string.c:1.445.2.14.2.2
--- php-src/ext/standard/string.c:1.445.2.14.2.1        Wed May 10 13:07:15 2006
+++ php-src/ext/standard/string.c       Sat Jun 10 15:29:06 2006
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.445.2.14.2.1 2006/05/10 13:07:15 iliaa Exp $ */
+/* $Id: string.c,v 1.445.2.14.2.2 2006/06/10 15:29:06 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -3022,10 +3022,18 @@
        int char_count = 0;
        int replaced = 0;
        char *source, *target, *tmp, *source_end=str+len, *tmp_end = NULL;
-       
-       for (source = str; source < source_end; source++) {
-               if ((case_sensitivity && *source == from) || (!case_sensitivity 
&& tolower(*source) == tolower(from))) {
+               
+       if (case_sensitivity) {
+               char *p = str, *e = p + len;
+               while ((p = memchr(p, from, (e - p)))) {
                        char_count++;
+                       p++;
+               }
+       } else {
+               for (source = str; source < source_end; source++) {
+                       if (tolower(*source) == tolower(from)) {
+                               char_count++;
+                       }
                }
        }
 
@@ -3037,20 +3045,36 @@
        Z_STRLEN_P(result) = len + (char_count * (to_len - 1));
        Z_STRVAL_P(result) = target = emalloc(Z_STRLEN_P(result) + 1);
        Z_TYPE_P(result) = IS_STRING;
-       
-       for (source = str; source < source_end; source++) {
-               if ((case_sensitivity && *source == from) || (!case_sensitivity 
&& tolower(*source) == tolower(from))) {
-                       replaced = 1;
-                       if (replace_count) {
-                               *replace_count += 1;
-                       }
-                       for (tmp = to, tmp_end = tmp+to_len; tmp < tmp_end; 
tmp++) {
-                               *target = *tmp;
+
+       if (case_sensitivity) {
+               char *p = str, *e = p + len, *s = str;
+               while ((p = memchr(p, from, (e - p)))) {
+                       memcpy(target, s, (p - s));
+                       target += p - s;
+                       memcpy(target, to, to_len);
+                       target += to_len;
+                       p++;
+                       s = p;
+               }
+               if (s < e) {
+                       memcpy(target, s, (e - s));
+                       target += e - s;
+               }
+       } else {
+               for (source = str; source < source_end; source++) {
+                       if (tolower(*source) == tolower(from)) {
+                               replaced = 1;
+                               if (replace_count) {
+                                       *replace_count += 1;
+                               }
+                               for (tmp = to, tmp_end = tmp+to_len; tmp < 
tmp_end; tmp++) {
+                                       *target = *tmp;
+                                       target++;
+                               }
+                       } else {
+                               *target = *source;
                                target++;
                        }
-               } else {
-                       *target = *source;
-                       target++;
                }
        }
        *target = 0;
http://cvs.php.net/viewcvs.cgi/php-src/NEWS?r1=1.2027.2.547.2.82&r2=1.2027.2.547.2.83&diff_format=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.2027.2.547.2.82 php-src/NEWS:1.2027.2.547.2.83
--- php-src/NEWS:1.2027.2.547.2.82      Fri Jun  9 15:17:39 2006
+++ php-src/NEWS        Sat Jun 10 15:29:06 2006
@@ -1,6 +1,8 @@
 PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? 2006, PHP 5.2.0
+- Improved performance of str_replace() when doing 1 char to 1 char or 1 char
+  to many chars replacement by 30-40%. (Ilia)
 - Added memory_get_peak_usage() function for retrieving peak memory usage of
   a PHP script. (Ilia)
 - Changed Apache 2 Handler SAPI to call ap_set_content_type() once only. (Mike)

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to