]> granicus.if.org Git - php/commitdiff
@Added substr_count() from Peter Kovacs. (Andrei)
authorAndrei Zmievski <andrei@php.net>
Wed, 26 Apr 2000 00:29:59 +0000 (00:29 +0000)
committerAndrei Zmievski <andrei@php.net>
Wed, 26 Apr 2000 00:29:59 +0000 (00:29 +0000)
# also some todo stuff

TODO
ext/pcre/php_pcre.c
ext/standard/basic_functions.c
ext/standard/php_string.h
ext/standard/string.c

diff --git a/TODO b/TODO
index 22cf796612cf7ad8c4db62e8cf50b73305a7a6f1..97fe1228c7e0f004265f9fffae61bfc9b46332e5 100644 (file)
--- a/TODO
+++ b/TODO
@@ -31,6 +31,8 @@ global
     * find a better way to implement script timeouts. SIGVTALRM is used
       by some POSIX threads implementations (i.e. OpenBSD) and is not
       available in ZTS mode.
+    * add aliases to functions to conform to new naming conventions, e.g.
+      str_to_upper().
 
 documentation
 -------------
index 7a9289803fe4a41076d9a48a56653b2a5aaf19ee..f8a19fb99cd95780e833129f019d6dbacd20a38e 100644 (file)
@@ -24,6 +24,7 @@
        - Make new modifier, similar to /e, that passes matches to
          a user-defined function
        - add option to preg_grep() to return entries that _don't_ match
+       - add option to preg_grep() to return the matching keys
 */
 
 #include "php.h"
index 587ecea7e1926c1b9ada1da91d4f693a4d5111d3..1fa2dab0fddbaec3eb9093687f7d32491b515f2d 100644 (file)
@@ -122,6 +122,7 @@ function_entry basic_functions[] = {
        
        PHP_FE(strnatcmp,                                                               NULL)
        PHP_FE(strnatcasecmp,                                                   NULL)
+       PHP_FE(substr_count,                                                    NULL)
        PHP_FE(strspn,                                                                  NULL)
        PHP_FE(strcspn,                                                                 NULL)
        PHP_FE(strtok,                                                                  NULL)
index 81d703553d5539902256aaebe447364470314035..9e31cb3cd928ea7e758dabcc0b6119ad085c8a61 100644 (file)
@@ -84,6 +84,7 @@ PHP_FUNCTION(str_repeat);
 PHP_FUNCTION(substr_replace);
 PHP_FUNCTION(strnatcmp);
 PHP_FUNCTION(strnatcasecmp);
+PHP_FUNCTION(substr_count);
 
 #define strnatcmp(a, b) \
        strnatcmp_ex(a, strlen(a), b, strlen(b), 0)
index 03930d85bc8a722152778f2d662c9ef8e9480ab0..39d9bd5e189e957dc4a678aefba46e25a8226fe5 100644 (file)
@@ -2473,12 +2473,65 @@ static void php_strnatcmp(INTERNAL_FUNCTION_PARAMETERS, int fold_case)
                                                         fold_case));
 }
 
+
+/* {{{ proto int strnatcmp(string s1, string s2)
+   Returns the result of string comparison using 'natural' algorithm */
 PHP_FUNCTION(strnatcmp)
 {
        php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
 }
+/* }}} */
 
+
+/* {{{ proto int strnatcasecmp(string s1, string s2)
+   Returns the result of case-insensitive string comparison using 'natural' algorithm */
 PHP_FUNCTION(strnatcasecmp)
 {
        php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
 }
+/* }}} */
+
+
+/* {{{ proto int str_count(string haystack, string needle)
+   Returns the number of times a substring occurs in the string. */
+PHP_FUNCTION(substr_count)
+{
+       zval **haystack, **needle;      
+       int i, length, count = 0;
+       char *p, *endp, cmp;
+
+       if (ARG_COUNT(ht) != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+
+       convert_to_string_ex(haystack);
+       convert_to_string_ex(needle);
+
+       if ((*needle)->value.str.len == 0) {
+               php_error(E_WARNING, "Empty substring");
+               RETURN_FALSE;
+       } else if ((*needle)->value.str.len == 1) {
+               // Special optimized case to avoid calls to php_memnstr
+               for (i = 0, p = (*haystack)->value.str.val, 
+                    length = (*haystack)->value.str.len, cmp = (*needle)->value.str.val[0]; 
+                    i < length; i++) {
+                       if (p[i] == cmp) {
+                               count++;
+                       }
+               }
+       } else {
+               p = (*haystack)->value.str.val;
+               endp = p + (*haystack)->value.str.len;
+               while (p <= endp) {
+                       if( (p = php_memnstr(p, (*needle)->value.str.val, (*needle)->value.str.len, endp)) != NULL ) {
+                               p += (*needle)->value.str.len;
+                               count++;
+                       } else {
+                               break;
+                       }
+               }
+       }
+
+       RETURN_LONG(count);
+}
+/* }}} */