johannes                Tue Aug  9 16:49:19 2005 EDT

  Added files:                 
    /php-src/ext/standard/tests/strings stristr.phpt 

  Modified files:              
    /php-src    NEWS 
    /php-src/ext/standard       string.c 
    /php-src/ext/standard/tests/strings strstr.phpt 
  Log:
  - Added option to strstr() and stristr() to get a particular haystack 
component
  
http://cvs.php.net/diff.php/php-src/NEWS?r1=1.2030&r2=1.2031&ty=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.2030 php-src/NEWS:1.2031
--- php-src/NEWS:1.2030 Tue Aug  9 11:07:18 2005
+++ php-src/NEWS        Tue Aug  9 16:49:12 2005
@@ -6,6 +6,8 @@
   . Added CURLINFO_HEADER_OUT to facilitate request retrieval.
 - Added an optional parameter to parse_url() to allow retrieval of distinct URL
   components. (Ilia)
+- Added an optional parameter to strstr() and stristr() for retrieval of either
+  the part of haystack before or after first occurence of needle. (Johannes)
 
 ?? ??? 2005, PHP 5.1
 - Upgraded PCRE library to version 6.2. (Andrei)
http://cvs.php.net/diff.php/php-src/ext/standard/string.c?r1=1.445&r2=1.446&ty=u
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.445 php-src/ext/standard/string.c:1.446
--- php-src/ext/standard/string.c:1.445 Wed Aug  3 10:08:13 2005
+++ php-src/ext/standard/string.c       Tue Aug  9 16:49:18 2005
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.445 2005/08/03 14:08:13 sniper Exp $ */
+/* $Id: string.c,v 1.446 2005/08/09 20:49:18 johannes Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -1433,52 +1433,60 @@
 }
 /* }}} */
 
-/* {{{ proto string stristr(string haystack, string needle)
+/* {{{ proto string stristr(string haystack, string needle[, bool part])
    Finds first occurrence of a string within another, case insensitive */
 PHP_FUNCTION(stristr)
 {
-       zval **haystack, **needle;
+       char *haystack;
+       long haystack_len;
+       zval *needle;
+       zend_bool part = 0;
        char *found = NULL;
        int  found_offset;
        char *haystack_orig;
        char needle_char[2];
        
-       if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, 
&needle) == FAILURE) {
-               WRONG_PARAM_COUNT;
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|b", &haystack, 
&haystack_len, &needle, &part) == FAILURE) {
+               return;
        }
 
-       SEPARATE_ZVAL(haystack);
-       SEPARATE_ZVAL(needle);
-       
-       convert_to_string_ex(haystack);
+       SEPARATE_ZVAL(&needle);
 
-       haystack_orig = estrndup(Z_STRVAL_PP(haystack), Z_STRLEN_PP(haystack));
+       haystack_orig = estrndup(haystack, haystack_len);
 
-       if (Z_TYPE_PP(needle) == IS_STRING) {
-               if (!Z_STRLEN_PP(needle)) {
+       if (Z_TYPE_P(needle) == IS_STRING) {
+               if (!Z_STRLEN_P(needle)) {
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty 
delimiter.");
                        efree(haystack_orig);
                        RETURN_FALSE;
                }
 
-               found = php_stristr(Z_STRVAL_PP(haystack),
-                                                       Z_STRVAL_PP(needle),
-                                                       Z_STRLEN_PP(haystack),
-                                                       Z_STRLEN_PP(needle));
+               found = php_stristr(haystack,
+                                                       Z_STRVAL_P(needle),
+                                                       haystack_len,
+                                                       Z_STRLEN_P(needle));
        } else {
-               convert_to_long_ex(needle);
-               needle_char[0] = (char) Z_LVAL_PP(needle);
+               convert_to_long_ex(&needle);
+               needle_char[0] = (char) Z_LVAL_P(needle);
                needle_char[1] = 0;
 
-               found = php_stristr(Z_STRVAL_PP(haystack),
+               found = php_stristr(haystack,
                                                        needle_char,
-                                                       Z_STRLEN_PP(haystack),
+                                                       haystack_len,
                                                        1);
        }
 
        if (found) {
-               found_offset = found - Z_STRVAL_PP(haystack);
-               RETVAL_STRINGL(haystack_orig + found_offset, 
Z_STRLEN_PP(haystack) - found_offset, 1);
+               found_offset = found - haystack;
+               if (part) {
+                       char *ret;
+                       ret = emalloc(found_offset + 1);
+                       strncpy(ret, haystack_orig, found_offset);
+                       ret[found_offset] = '\0';
+                       RETVAL_STRINGL(ret , found_offset, 0);
+               } else {
+                       RETVAL_STRINGL(haystack_orig + found_offset, 
haystack_len - found_offset, 1);
+               }
        } else {
                RETVAL_FALSE;
        }
@@ -1487,45 +1495,54 @@
 }
 /* }}} */
 
-/* {{{ proto string strstr(string haystack, string needle)
+/* {{{ proto string strstr(string haystack, string needle[, bool part])
    Finds first occurrence of a string within another */
 PHP_FUNCTION(strstr)
 {
-       zval **haystack, **needle;
+       char *haystack;
+       long haystack_len;
+       zval *needle;
+       zend_bool part = 0;
        char *found = NULL;
        char needle_char[2];
        long found_offset;
-       
-       if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, 
&needle) == FAILURE) {
-               WRONG_PARAM_COUNT;
-       }
 
-       convert_to_string_ex(haystack);
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|b", &haystack, 
&haystack_len, &needle, &part) == FAILURE) {
+               return;
+       }
 
-       if (Z_TYPE_PP(needle) == IS_STRING) {
-               if (!Z_STRLEN_PP(needle)) {
+       if (Z_TYPE_P(needle) == IS_STRING) {
+               if (!Z_STRLEN_P(needle)) {
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty 
delimiter.");
                        RETURN_FALSE;
                }
 
-               found = php_memnstr(Z_STRVAL_PP(haystack), 
-                                   Z_STRVAL_PP(needle),
-                                   Z_STRLEN_PP(needle), 
-                                   Z_STRVAL_PP(haystack) + 
Z_STRLEN_PP(haystack));
+               found = php_memnstr(haystack,
+                                   Z_STRVAL_P(needle),
+                                   Z_STRLEN_P(needle), 
+                                   haystack + haystack_len);
        } else {
-               convert_to_long_ex(needle);
-               needle_char[0] = (char) Z_LVAL_PP(needle);
+               convert_to_long_ex(&needle);
+               needle_char[0] = (char) Z_LVAL_P(needle);
                needle_char[1] = 0;
 
-               found = php_memnstr(Z_STRVAL_PP(haystack), 
+               found = php_memnstr(haystack,
                                                        needle_char,
                                                        1,
-                                   Z_STRVAL_PP(haystack) + 
Z_STRLEN_PP(haystack));
+                                   haystack + haystack_len);
        }
 
        if (found) {
-               found_offset = found - Z_STRVAL_PP(haystack);
-               RETURN_STRINGL(found, Z_STRLEN_PP(haystack) - found_offset, 1);
+               found_offset = found - haystack;
+               if (part) {
+                       char *ret;
+                       ret = emalloc(found_offset + 1);
+                       strncpy(ret, haystack, found_offset);
+                       ret[found_offset] = '\0';
+                       RETURN_STRINGL(ret , found_offset, 0);
+               } else {
+                       RETURN_STRINGL(found, haystack_len - found_offset, 1);
+               }
        } else {
                RETURN_FALSE;
        }
http://cvs.php.net/diff.php/php-src/ext/standard/tests/strings/strstr.phpt?r1=1.2&r2=1.3&ty=u
Index: php-src/ext/standard/tests/strings/strstr.phpt
diff -u php-src/ext/standard/tests/strings/strstr.phpt:1.2 
php-src/ext/standard/tests/strings/strstr.phpt:1.3
--- php-src/ext/standard/tests/strings/strstr.phpt:1.2  Wed May 19 04:45:23 2004
+++ php-src/ext/standard/tests/strings/strstr.phpt      Tue Aug  9 16:49:19 2005
@@ -14,6 +14,8 @@
        var_dump(@strstr("a", ""));
        var_dump(@strstr("", "a"));
        var_dump(md5(@strstr("\\\\a\\", "\\a")));
+       var_dump(strstr("test string", " ", false));
+       var_dump(strstr("test string", " ", true));
 ?>
 --EXPECT--
 string(11) "test string"
@@ -28,3 +30,5 @@
 bool(false)
 bool(false)
 string(32) "6ec19f52f0766c463f3bb240f4396913"
+string(7) " string"
+string(4) "test"

http://cvs.php.net/co.php/php-src/ext/standard/tests/strings/stristr.phpt?r=1.1&p=1
Index: php-src/ext/standard/tests/strings/stristr.phpt
+++ php-src/ext/standard/tests/strings/stristr.phpt
--TEST--
stristr() function
--FILE--
<?php
        var_dump(stristr("tEsT sTrInG", "tEsT"));
        var_dump(stristr("tEsT sTrInG", "stRiNg"));
        var_dump(stristr("tEsT sTrInG", "stRiN"));
        var_dump(stristr("tEsT sTrInG", "t S"));
        var_dump(stristr("tEsT sTrInG", "g"));
        var_dump(md5(stristr("te".chr(0)."st", chr(0))));
        var_dump(@stristr("", ""));
        var_dump(@stristr("a", ""));
        var_dump(@stristr("", "a"));
        var_dump(md5(@stristr("\\\\a\\", "\\a")));
        var_dump(stristr("tEsT sTrInG", " ", false));
        var_dump(stristr("tEsT sTrInG", " ", true));
?>
--EXPECT--
string(11) "tEsT sTrInG"
string(6) "sTrInG"
string(6) "sTrInG"
string(8) "T sTrInG"
string(1) "G"
string(32) "7272696018bdeb2c9a3f8d01fc2a9273"
bool(false)
bool(false)
bool(false)
string(32) "6ec19f52f0766c463f3bb240f4396913"
string(7) " sTrInG"
string(4) "tEsT"

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

Reply via email to