pollita         Tue Dec  2 20:31:57 2003 EDT

  Modified files:              
    /php-src/ext/standard       string.c 
  Log:
  Optimize strrpos/strripos for single char strings and fix offset to report correctly
  
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.403 php-src/ext/standard/string.c:1.404
--- php-src/ext/standard/string.c:1.403 Tue Dec  2 20:11:36 2003
+++ php-src/ext/standard/string.c       Tue Dec  2 20:31:56 2003
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.403 2003/12/03 01:11:36 pollita Exp $ */
+/* $Id: string.c,v 1.404 2003/12/03 01:31:56 pollita Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -1606,9 +1606,20 @@
                }
        }
 
+       if (needle_len == 1) {
+               /* Single character search can shortcut memcmps */
+               while (e >= p) {
+                       if (*e == *needle) {
+                               RETURN_LONG(e - p + (offset > 0 ? offset : 0));
+                       }
+                       e--;
+               }
+               RETURN_FALSE;
+       }
+
        while (e >= p) {
                if (memcmp(e, needle, needle_len) == 0) {
-                       RETURN_LONG(e - p);
+                       RETURN_LONG(e - p + (offset > 0 ? offset : 0));
                }
                e--;
        }
@@ -1646,6 +1657,27 @@
                RETURN_FALSE;
        }
 
+       if (needle_len == 1) {
+               /* Single character search can shortcut memcmps 
+                  Can also avoid tolower emallocs */
+               if (offset >= 0) {
+                       p = haystack + offset;
+                       e = haystack + haystack_len - 1;
+               } else {
+                       p = haystack;
+                       e = haystack + haystack_len - offset;
+               }
+               /* Borrow that ord_needle buffer to avoid repeatedly tolower()ing 
needle */
+               *ord_needle = tolower(*needle);
+               while (e >= p) {
+                       if (tolower(*e) == *ord_needle) {
+                               RETURN_LONG(e - p + (offset > 0 ? offset : 0));
+                       }
+                       e--;
+               }
+               RETURN_FALSE;
+       }
+
        needle_dup = estrndup(needle, needle_len);
        php_strtolower(needle_dup, needle_len);
        haystack_dup = estrndup(haystack, haystack_len);
@@ -1667,7 +1699,7 @@
                if (memcmp(e, needle_dup, needle_len) == 0) {
                        efree(haystack_dup);
                        efree(needle_dup);
-                       RETURN_LONG(e - p);
+                       RETURN_LONG(e - p + (offset > 0 ? offset : 0));
                }
                e--;
        }

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

Reply via email to