[PHP-CVS] cvs: php-src / NEWS /ext/standard string.c /ext/standard/tests/strings substr_count.phpt
by Ilia Alshanetsky other posts by this author
Jun 18 2005 11:23AM messages near this date
[PHP-CVS] cvs: php-src /build shtool
|
[PHP-CVS] cvs: php-src /ext/date TODO
iliaa Sat Jun 18 14:23:12 2005 EDT
Modified files:
/php-src NEWS
/php-src/ext/standard string.c
/php-src/ext/standard/tests/strings substr_count.phpt
Log:
Added offset & length parameters to substr_count() function.
http://cvs.php.net/diff.php/php-src/NEWS?r1=1.1929&r2=1.1930&ty=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.1929 php-src/NEWS:1.1930
--- php-src/NEWS:1.1929 Fri Jun 17 21:06:58 2005
+++ php-src/NEWS Sat Jun 18 14:23:12 2005
@@ -8,6 +8,7 @@
(Derick)
- Added bindto socket context option. (Ilia)
- Added offset parameter to the stream_copy_to_stream() function. (Ilia)
+- Added offset & length parameters to substr_count() function. (Ilia)
- Fixed PDO shutdown problem (possible inifite loop running rollback on
shutdown). (Wez)
- Fixed PECL bug #3714 (beginTransaction doesn't work if you're in
http://cvs.php.net/diff.php/php-src/ext/standard/string.c?r1=1.439&r2=1.440&ty=u
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.439 php-src/ext/standard/string.c:1.440
--- php-src/ext/standard/string.c:1.439 Thu Jun 2 04:51:20 2005
+++ php-src/ext/standard/string.c Sat Jun 18 14:23:12 2005
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.439 2005/06/02 08:51:20 derick Exp $ */
+/* $Id: string.c,v 1.440 2005/06/18 18:23:12 iliaa Exp $ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
@@ -4420,15 +4420,16 @@
}
/* }}} */
-/* {{{ proto int substr_count(string haystack, string needle)
+/* {{{ proto int substr_count(string haystack, string needle [, int offset [, int length]])
Returns the number of times a substring occurs in the string */
PHP_FUNCTION(substr_count)
{
- zval **haystack, **needle;
+ zval **haystack, **needle, **offset, **length;
+ int ac = ZEND_NUM_ARGS();
int count = 0;
char *p, *endp, cmp;
- if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) {
+ if (ac < 2 || ac > 4 || zend_get_parameters_ex(ac, &haystack, &needle, &offset, &length) =
= FAILURE) {
WRONG_PARAM_COUNT;
}
@@ -4443,6 +4444,23 @@
p = Z_STRVAL_PP(haystack);
endp = p + Z_STRLEN_PP(haystack);
+ if (ac > 2) {
+ convert_to_long_ex(offset);
+ p += Z_LVAL_PP(offset);
+ if (p > endp) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset value %ld exceeds string length.", Z
_LVAL_PP(offset));
+ RETURN_FALSE;
+ }
+ if (ac == 4) {
+ convert_to_long_ex(length);
+ if ((p + Z_LVAL_PP(length)) > endp) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length value %ld exceeds string length.",
Z_LVAL_PP(length));
+ RETURN_FALSE;
+ }
+ endp = p + Z_LVAL_PP(length);
+ }
+ }
+
if (Z_STRLEN_PP(needle) == 1) {
cmp = Z_STRVAL_PP(needle)[0];
http://cvs.php.net/diff.php/php-src/ext/standard/tests/strings/substr_count.phpt?r1=1.2&r2=1
.3&ty=u
Index: php-src/ext/standard/tests/strings/substr_count.phpt
diff -u php-src/ext/standard/tests/strings/substr_count.phpt:1.2 php-src/ext/standard/tests/
strings/substr_count.phpt:1.3
--- php-src/ext/standard/tests/strings/substr_count.phpt:1.2 Wed May 19 04:45:23 2004
+++ php-src/ext/standard/tests/strings/substr_count.phpt Sat Jun 18 14:23:12 2005
@@ -13,6 +13,9 @@
$a = str_repeat("abcacbabca", 100);
var_dump(@substr_count($a, "bca"));
+
+ var_dump(substr_count($a, "bca", 200));
+ var_dump(substr_count($a, "bca", 200, 50));
?>
--EXPECT--
bool(false)
@@ -22,3 +25,5 @@
int(0)
int(100)
int(200)
+int(160)
+int(10)
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
|