moriyoshi Sat Nov 9 12:05:47 2002 EDT
Modified files:
/php4/ext/mbstring mbfilter.c mbfilter.h mbstring.c mbstring.h
Log:
Added mb_substr_count() as per the request #15097
Index: php4/ext/mbstring/mbfilter.c
diff -u php4/ext/mbstring/mbfilter.c:1.47 php4/ext/mbstring/mbfilter.c:1.48
--- php4/ext/mbstring/mbfilter.c:1.47 Sat Nov 2 08:38:32 2002
+++ php4/ext/mbstring/mbfilter.c Sat Nov 9 12:05:47 2002
@@ -1,6 +1,7 @@
-/* charset=UTF-8
- * vim: encoding=utf-8:
- * */
+/*
+ * charset=UTF-8
+ * vim600: encoding=utf-8
+ */
/*
* "streamable kanji code filter and converter"
@@ -79,7 +80,7 @@
*
*/
-/* $Id: mbfilter.c,v 1.47 2002/11/02 13:38:32 moriyoshi Exp $ */
+/* $Id: mbfilter.c,v 1.48 2002/11/09 17:05:47 moriyoshi Exp $ */
#ifdef HAVE_CONFIG_H
@@ -7831,6 +7832,90 @@
return result;
}
+/*
+ * substr_count
+ */
+
+int
+mbfl_substr_count(
+ mbfl_string *haystack,
+ mbfl_string *needle
+ TSRMLS_DC)
+{
+ int n, result = 0;
+ unsigned char *p;
+ mbfl_convert_filter *filter;
+ struct collector_strpos_data pc;
+
+ if (haystack == NULL || needle == NULL) {
+ return -8;
+ }
+ /* needle is converted into wchar */
+ mbfl_wchar_device_init(&pc.needle TSRMLS_CC);
+ filter = mbfl_convert_filter_new(
+ needle->no_encoding,
+ mbfl_no_encoding_wchar,
+ mbfl_wchar_device_output, 0, &pc.needle TSRMLS_CC);
+ if (filter == NULL) {
+ return -4;
+ }
+ p = needle->val;
+ n = needle->len;
+ if (p != NULL) {
+ while (n > 0) {
+ if ((*filter->filter_function)(*p++, filter TSRMLS_CC) < 0) {
+ break;
+ }
+ n--;
+ }
+ }
+ mbfl_convert_filter_flush(filter TSRMLS_CC);
+ mbfl_convert_filter_delete(filter TSRMLS_CC);
+ pc.needle_len = pc.needle.pos;
+ if (pc.needle.buffer == NULL) {
+ return -4;
+ }
+ if (pc.needle_len <= 0) {
+ mbfl_wchar_device_clear(&pc.needle TSRMLS_CC);
+ return -2;
+ }
+ /* initialize filter and collector data */
+ filter = mbfl_convert_filter_new(
+ haystack->no_encoding,
+ mbfl_no_encoding_wchar,
+ collector_strpos, 0, &pc TSRMLS_CC);
+ if (filter == NULL) {
+ mbfl_wchar_device_clear(&pc.needle TSRMLS_CC);
+ return -4;
+ }
+ pc.start = 0;
+ pc.output = 0;
+ pc.needle_pos = 0;
+ pc.found_pos = 0;
+ pc.matched_pos = -1;
+
+ /* feed data */
+ p = haystack->val;
+ n = haystack->len;
+ if (p != NULL) {
+ while (n > 0) {
+ if ((*filter->filter_function)(*p++, filter TSRMLS_CC) < 0) {
+ pc.matched_pos = -4;
+ break;
+ }
+ if (pc.matched_pos >= 0) {
+ ++result;
+ pc.matched_pos = -1;
+ }
+ n--;
+ }
+ }
+ mbfl_convert_filter_flush(filter TSRMLS_CC);
+ mbfl_convert_filter_delete(filter TSRMLS_CC);
+ mbfl_wchar_device_clear(&pc.needle TSRMLS_CC);
+
+ return result;
+}
/*
* substr
Index: php4/ext/mbstring/mbfilter.h
diff -u php4/ext/mbstring/mbfilter.h:1.15 php4/ext/mbstring/mbfilter.h:1.16
--- php4/ext/mbstring/mbfilter.h:1.15 Wed Aug 14 02:38:07 2002
+++ php4/ext/mbstring/mbfilter.h Sat Nov 9 12:05:47 2002
@@ -86,7 +86,7 @@
*
*/
-/* $Id: mbfilter.h,v 1.15 2002/08/14 06:38:07 dets Exp $ */
+/* $Id: mbfilter.h,v 1.16 2002/11/09 17:05:47 moriyoshi Exp $ */
#ifndef MBFL_MBFILTER_H
@@ -486,6 +486,13 @@
*/
int
mbfl_strpos(mbfl_string *haystack, mbfl_string *needle, int offset, int reverse
TSRMLS_DC);
+
+
+/*
+ * substr_count
+ */
+int
+mbfl_substr_count(mbfl_string *haystack, mbfl_string *needle TSRMLS_DC);
/*
* substr
Index: php4/ext/mbstring/mbstring.c
diff -u php4/ext/mbstring/mbstring.c:1.129 php4/ext/mbstring/mbstring.c:1.130
--- php4/ext/mbstring/mbstring.c:1.129 Sat Nov 9 11:15:40 2002
+++ php4/ext/mbstring/mbstring.c Sat Nov 9 12:05:47 2002
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: mbstring.c,v 1.129 2002/11/09 16:15:40 moriyoshi Exp $ */
+/* $Id: mbstring.c,v 1.130 2002/11/09 17:05:47 moriyoshi Exp $ */
/*
* PHP4 Multibyte String module "mbstring" (currently only for Japanese)
@@ -173,6 +173,7 @@
{MB_OVERLOAD_STRING, "substr", "mb_substr", "mb_orig_substr"},
{MB_OVERLOAD_STRING, "strtolower", "mb_strtolower", "mb_orig_strtolower"},
{MB_OVERLOAD_STRING, "strtoupper", "mb_strtoupper", "mb_orig_strtoupper"},
+ {MB_OVERLOAD_STRING, "substr_count", "mb_substr_count",
+"mb_orig_substr_count"},
#if HAVE_MBREGEX
{MB_OVERLOAD_REGEX, "ereg", "mb_ereg", "mb_orig_ereg"},
{MB_OVERLOAD_REGEX, "eregi", "mb_eregi", "mb_orig_eregi"},
@@ -216,6 +217,7 @@
PHP_FE(mb_strlen, NULL)
PHP_FE(mb_strpos, NULL)
PHP_FE(mb_strrpos, NULL)
+ PHP_FE(mb_substr_count, NULL)
PHP_FE(mb_substr, NULL)
PHP_FE(mb_strcut, NULL)
PHP_FE(mb_strwidth, NULL)
@@ -2113,6 +2115,61 @@
needle.val = (unsigned char *)Z_STRVAL_PP(arg2);
needle.len = Z_STRLEN_PP(arg2);
n = mbfl_strpos(&haystack, &needle, 0, 1 TSRMLS_CC);
+ if (n >= 0) {
+ RETVAL_LONG(n);
+ } else {
+ RETVAL_FALSE;
+ }
+}
+/* }}} */
+
+/* {{{ proto int mb_substr_count(string haystack, string needle [, string encoding])
+ Count the number of substring occurrences */
+PHP_FUNCTION(mb_substr_count)
+{
+ pval **arg1, **arg2, **arg3;
+ int n;
+ mbfl_string haystack, needle;
+
+ mbfl_string_init(&haystack);
+ mbfl_string_init(&needle);
+ haystack.no_language = MBSTRG(current_language);
+ haystack.no_encoding = MBSTRG(current_internal_encoding);
+ needle.no_language = MBSTRG(current_language);
+ needle.no_encoding = MBSTRG(current_internal_encoding);
+ switch (ZEND_NUM_ARGS()) {
+ case 2:
+ if (zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ break;
+ case 3:
+ if (zend_get_parameters_ex(3, &arg1, &arg2, &arg3) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string_ex(arg3);
+ haystack.no_encoding = needle.no_encoding =
+mbfl_name2no_encoding(Z_STRVAL_PP(arg3));
+ if (haystack.no_encoding == mbfl_no_encoding_invalid) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding
+\"%s\"", Z_STRVAL_PP(arg3));
+ RETURN_FALSE;
+ }
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_string_ex(arg1);
+ convert_to_string_ex(arg2);
+
+ if (Z_STRLEN_PP(arg2) <= 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,"Empty needle");
+ RETURN_FALSE;
+ }
+ haystack.val = (unsigned char *)Z_STRVAL_PP(arg1);
+ haystack.len = Z_STRLEN_PP(arg1);
+ needle.val = (unsigned char *)Z_STRVAL_PP(arg2);
+ needle.len = Z_STRLEN_PP(arg2);
+ n = mbfl_substr_count(&haystack, &needle TSRMLS_CC);
if (n >= 0) {
RETVAL_LONG(n);
} else {
Index: php4/ext/mbstring/mbstring.h
diff -u php4/ext/mbstring/mbstring.h:1.39 php4/ext/mbstring/mbstring.h:1.40
--- php4/ext/mbstring/mbstring.h:1.39 Wed Oct 23 19:25:26 2002
+++ php4/ext/mbstring/mbstring.h Sat Nov 9 12:05:47 2002
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: mbstring.h,v 1.39 2002/10/23 23:25:26 moriyoshi Exp $ */
+/* $Id: mbstring.h,v 1.40 2002/11/09 17:05:47 moriyoshi Exp $ */
/*
* PHP4 Multibyte String module "mbstring" (currently only for Japanese)
@@ -91,6 +91,7 @@
PHP_FUNCTION(mb_strlen);
PHP_FUNCTION(mb_strpos);
PHP_FUNCTION(mb_strrpos);
+PHP_FUNCTION(mb_substr_count);
PHP_FUNCTION(mb_substr);
PHP_FUNCTION(mb_strcut);
PHP_FUNCTION(mb_strwidth);
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php