andrey          Wed Sep 25 15:06:30 2002 EDT

  Modified files:              
    /php4/ext/standard  string.c 
  Log:
  Added common handler for strspn() and strcspn(). Almost of the code is 
  identical. Modified to accept up to 4 params. Last 2 optional start & len.
  They are analogic to start & len of substr(). So the behavior when start &
  len are used is like
  strspn(substr($s,$start,$len),$good_chars) and
  strcspn(substr($s,$start,$len), $bad_chars)
  
  
Index: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.298 php4/ext/standard/string.c:1.299
--- php4/ext/standard/string.c:1.298    Wed Sep 25 14:06:05 2002
+++ php4/ext/standard/string.c  Wed Sep 25 15:06:29 2002
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.298 2002/09/25 18:06:05 andrey Exp $ */
+/* $Id: string.c,v 1.299 2002/09/25 19:06:29 andrey Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -54,6 +54,9 @@
 #define PHP_PATHINFO_EXTENSION         4
 #define PHP_PATHINFO_ALL       (PHP_PATHINFO_DIRNAME | PHP_PATHINFO_BASENAME | 
PHP_PATHINFO_EXTENSION)
 
+#define STR_STRSPN                             0
+#define STR_STRCSPN                            1
+
 /* {{{ register_string_constants
  */
 void register_string_constants(INIT_FUNC_ARGS)
@@ -195,39 +198,71 @@
 }
 /* }}} */
 
-/* {{{ proto int strspn(string str, string mask)
-   Finds length of initial segment consisting entirely of characters found in mask */
-PHP_FUNCTION(strspn)
+static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior)
 {
-       zval **s1, **s2;
+       char *s11, *s22;
+       long len1, len2, start, len;
        
-       if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
-               WRONG_PARAM_COUNT;
+       start = 0;
+       len = 0;
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ll", &s11, &len1,
+                               &s22, &len2, &start, &len) == FAILURE) {
+               return;
+       }
+       
+       if (ZEND_NUM_ARGS() < 4) {
+               len = len1;
+       }
+       
+       /* look at substr() function for more information */
+       
+       if (start < 0) {
+               start += len1;
+               if (start < 0) {
+                       start = 0;
+               }
+       } else if (start > len1) {
+               RETURN_FALSE;
+       }
+       
+       if (len < 0) {
+               len += (len1 - start);
+               if (len < 0) {
+                       len = 0;
+               }
+       }
+       
+       if ((start + len) > len1) {
+               len = len1 - start;
        }
-       convert_to_string_ex(s1);
-       convert_to_string_ex(s2);
 
-       RETURN_LONG(php_strspn(Z_STRVAL_PP(s1), Z_STRVAL_PP(s2),
-                              Z_STRVAL_PP(s1) + Z_STRLEN_PP(s1),
-                              Z_STRVAL_PP(s2) + Z_STRLEN_PP(s2)));
+       if (behavior == STR_STRSPN) {
+               RETURN_LONG(php_strspn(s11 + start /*str1_start*/,
+                                               s22 /*str2_start*/,
+                                               s11 + start + len /*str1_end*/,
+                                               s22 + len2 /*str2_end*/));
+       } else if (behavior == STR_STRCSPN) {
+               RETURN_LONG(php_strcspn(s11 + start /*str1_start*/,
+                                               s22 /*str2_start*/,
+                                               s11 + start + len /*str1_end*/,
+                                               s22 + len2 /*str2_end*/));
+       }
+       
+}
+
+/* {{{ proto int strspn(string str, string mask [, start [, len]])
+   Finds length of initial segment consisting entirely of characters found in mask. 
+If start or/and length is provided works like 
+strspn(substr($s,$start,$len),$good_chars) */
+PHP_FUNCTION(strspn)
+{
+       php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, STR_STRSPN);
 }
 /* }}} */
 
-/* {{{ proto int strcspn(string str, string mask)
-   Finds length of initial segment consisting entirely of characters not found in 
mask */
+/* {{{ proto int strcspn(string str, string mask [, start [, len]])
+   Finds length of initial segment consisting entirely of characters not found in 
+mask. If start or/and length is provide works like 
+strcspn(substr($s,$start,$len),$bad_chars) */
 PHP_FUNCTION(strcspn)
 {
-       zval **s1, **s2;
-       
-       if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
-               WRONG_PARAM_COUNT;
-       }
-       convert_to_string_ex(s1);
-       convert_to_string_ex(s2);
-
-       RETURN_LONG(php_strcspn(Z_STRVAL_PP(s1), Z_STRVAL_PP(s2),
-                               Z_STRVAL_PP(s1) + Z_STRLEN_PP(s1),
-                               Z_STRVAL_PP(s2) + Z_STRLEN_PP(s2)));
+       php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, STR_STRCSPN);
 }
 /* }}} */
 



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

Reply via email to