summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Zmievski <andrei@php.net>1999-12-13 23:40:36 +0000
committerAndrei Zmievski <andrei@php.net>1999-12-13 23:40:36 +0000
commita15916a47aba3e89d2b6cad8533868c7ee958c25 (patch)
tree29d67ed25fb75cf23be9001184bad97060f80147
parent457a13dac5d7bc56d41055090d8db2fffa9506f1 (diff)
downloadphp-git-a15916a47aba3e89d2b6cad8533868c7ee958c25.tar.gz
Made strspn() and strcspn() binary-safe.
# Please test if you can, especially cases with embedded chr(0). @ Made strspn() and strcspn() binary-safe.
-rw-r--r--TODO1
-rw-r--r--ext/standard/php_string.h4
-rw-r--r--ext/standard/string.c40
3 files changed, 40 insertions, 5 deletions
diff --git a/TODO b/TODO
index 87c2c207da..51f2258d68 100644
--- a/TODO
+++ b/TODO
@@ -25,7 +25,6 @@ ext/standard
------------
* strpad() (Andrei)
* NOT binary safe:
- strcspn()
strtok()
basename()
dirname()
diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h
index d7078f0201..3010676590 100644
--- a/ext/standard/php_string.h
+++ b/ext/standard/php_string.h
@@ -102,6 +102,8 @@ extern PHPAPI void php_char_to_str(char *str, uint len, char from, char *to, int
extern PHPAPI void php_implode(pval *delim, pval *arr, pval *return_value);
extern PHPAPI void php_explode(pval *delim, pval *str, pval *return_value);
-PHPAPI inline char *php_memnstr(char *haystack, char *needle, int needle_len, char *end);
+extern PHPAPI inline char *php_memnstr(char *haystack, char *needle, int needle_len, char *end);
+extern PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end);
+extern PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end);
#endif /* _PHPSTRING_H */
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 47896b913a..cfea7aa4d7 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -86,14 +86,16 @@ PHP_FUNCTION(bin2hex)
Find length of initial segment consisting entirely of characters found in mask */
PHP_FUNCTION(strspn)
{
- pval **s1,**s2;
+ zval **s1,**s2;
if (ARG_COUNT(ht)!=2 || getParametersEx(2, &s1, &s2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(s1);
convert_to_string_ex(s2);
- RETURN_LONG(strspn((*s1)->value.str.val,(*s2)->value.str.val));
+ RETURN_LONG(php_strspn((*s1)->value.str.val, (*s2)->value.str.val,
+ (*s1)->value.str.val + (*s1)->value.str.len,
+ (*s2)->value.str.val + (*s2)->value.str.len));
}
/* }}} */
@@ -108,7 +110,9 @@ PHP_FUNCTION(strcspn)
}
convert_to_string_ex(s1);
convert_to_string_ex(s2);
- RETURN_LONG(strcspn((*s1)->value.str.val,(*s2)->value.str.val));
+ RETURN_LONG(php_strcspn((*s1)->value.str.val, (*s2)->value.str.val,
+ (*s1)->value.str.val + (*s1)->value.str.len,
+ (*s2)->value.str.val + (*s2)->value.str.len));
}
/* }}} */
@@ -532,6 +536,36 @@ PHPAPI char *php_stristr(unsigned char *s, unsigned char *t,
return php_memnstr(s, t, t_len, s + s_len);
}
+PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end)
+{
+ register const char *p = s1, *spanp;
+ register char c = *p;
+
+cont:
+ for (spanp = s2; p != s1_end && spanp != s2_end;)
+ if (*spanp++ == c) {
+ c = *(++p);
+ goto cont;
+ }
+ return (p - s1);
+}
+
+PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end)
+{
+ register const char *p, *spanp;
+ register char c = *s1;
+
+ for (p = s1;;) {
+ spanp = s2;
+ do {
+ if (*spanp == c || p == s1_end)
+ return (p - s1);
+ } while (spanp++ < s2_end);
+ c = *(++p);
+ }
+ /* NOTREACHED */
+}
+
/* {{{ proto string stristr(string haystack, string needle)
Find first occurrence of a string within another, case insensitive */
PHP_FUNCTION(stristr)