cvs: php-src /ext/standard string.c /ext/standard/tests/strings str_word_count.phpt
| From: | Andrey Hristov | Date: | Sat, 12 Mar 2005 14:41:21 +0000 |
| Subject: | cvs: php-src /ext/standard string.c /ext/standard/tests/strings str_word_count.phpt | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-30901@lists.php.net to get a copy of this message | ||
andrey Sat Mar 12 09:41:21 2005 EDT
Modified files:
/php-src/ext/standard string.c
/php-src/ext/standard/tests/strings str_word_count.phpt
Log:
added an optional third parameter to str_word_count(), which is used as
a list of characters which are considerd "is_alpha()". Thus V14GR4 can
be extracted as one word if the user supplies "14" as charlist.
FR #31560
# Nuno, are you wishing to document this one too? :)
http://cvs.php.net/diff.php/php-src/ext/standard/string.c?r1=1.432&r2=1.433&ty=u
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.432 php-src/ext/standard/string.c:1.433
--- php-src/ext/standard/string.c:1.432 Sat Mar 12 08:08:19 2005
+++ php-src/ext/standard/string.c Sat Mar 12 09:41:20 2005
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.432 2005/03/12 13:08:19 andrey Exp $ */
+/* $Id: string.c,v 1.433 2005/03/12 14:41:20 andrey Exp $ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
@@ -4643,7 +4643,7 @@
}
/* }}} */
-/* {{{ proto mixed str_word_count(string str, [int format])
+/* {{{ proto mixed str_word_count(string str, [int format [, string charlist]])
Counts the number of words inside a string. If format of 1 is specified,
then the function will return an array containing all the words
found inside the string. If format of 2 is specified, then the function
@@ -4656,38 +4656,44 @@
*/
PHP_FUNCTION(str_word_count)
{
- zval **str, **o_format;
+ zval **str, **o_format, **also_alphas;
char *s, *e, *p, *buf;
+ char *p_also = NULL;
int word_count = 0;
int type = 0;
int n_args = ZEND_NUM_ARGS();
- if (n_args > 2 || n_args < 1 || zend_get_parameters_ex(n_args, &str, &o_format) ==
FAILURE) {
+ if (n_args > 3 || n_args < 1 || zend_get_parameters_ex(n_args, &str, &o_format,
&also_alphas) == FAILURE) {
WRONG_PARAM_COUNT;
}
-
- if (n_args == 2) {
- convert_to_long_ex(o_format);
- type = Z_LVAL_PP(o_format);
+ switch (n_args) {
+ case 3:
+ convert_to_string_ex(also_alphas);
+ if (Z_STRLEN_PP(also_alphas)) {
+ p_also = Z_STRVAL_PP(also_alphas);
+ }
+ case 2:
+ convert_to_long_ex(o_format);
+ type = Z_LVAL_PP(o_format);
- if (type != 1 && type != 2) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "The specified format parameter, '%d'
is invalid.", type);
- RETURN_FALSE;
- }
+ if (type != 1 && type !=2 && !(n_args >= 3 && type == 0)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "The specified format parameter,
'%d' is invalid.", type);
+ RETURN_FALSE;
+ }
+ if (type) {
+ array_init(return_value);
+ }
}
convert_to_string_ex(str);
-
e = Z_STRLEN_PP(str) + (p = s = Z_STRVAL_PP(str));
-
- if (type) {
- array_init(return_value);
- }
-
while (p < e) {
if (isalpha(*p++)) {
- s = p - 1;
- while (isalpha(*p) || *p == '\'' || (*p == '-' &&
isalpha(*(p+1)))) {
+ s = p - 1; /* the word starts at s */
+ while (isalpha(*p) || *p == '\'' || (*p == '-' &&
isalpha(*(p+1)))
+ || (p_also && php_memnstr(p_also, p, 1, p_also + Z_STRLEN_PP(also_alphas)))
+ )
+ {
p++;
}
http://cvs.php.net/diff.php/php-src/ext/standard/tests/strings/str_word_count.phpt?r1=1.1&r2=1.2&ty=u
Index: php-src/ext/standard/tests/strings/str_word_count.phpt
diff -u php-src/ext/standard/tests/strings/str_word_count.phpt:1.1
php-src/ext/standard/tests/strings/str_word_count.phpt:1.2
--- php-src/ext/standard/tests/strings/str_word_count.phpt:1.1 Sat Mar 12 07:59:53 2005
+++ php-src/ext/standard/tests/strings/str_word_count.phpt Sat Mar 12 09:41:20 2005
@@ -17,6 +17,26 @@
var_dump(str_word_count($str, $b));
var_dump($str);
+$str2 = "F0o B4r 1s bar foo";
+var_dump(str_word_count($str2, NULL, "04"));
+var_dump(str_word_count($str2, NULL, "01"));
+var_dump(str_word_count($str2, NULL, "014"));
+var_dump(str_word_count($str2, NULL, array()));
+var_dump(str_word_count($str2, NULL, new stdClass));
+var_dump(str_word_count($str2, NULL, ""));
+var_dump(str_word_count($str2, 1, "04"));
+var_dump(str_word_count($str2, 1, "01"));
+var_dump(str_word_count($str2, 1, "014"));
+var_dump(str_word_count($str2, 1, array()));
+var_dump(str_word_count($str2, 1, new stdClass));
+var_dump(str_word_count($str2, 1, ""));
+var_dump(str_word_count($str2, 2, "04"));
+var_dump(str_word_count($str2, 2, "01"));
+var_dump(str_word_count($str2, 2, "014"));
+var_dump(str_word_count($str2, 2, array()));
+var_dump(str_word_count($str2, 2, new stdClass));
+var_dump(str_word_count($str2, 2, ""));
+
?>
--EXPECTF--
array(6) {
@@ -67,4 +87,194 @@
Warning: str_word_count(): The specified format parameter, '0' is invalid. in %s on line
14
bool(false)
string(55) "Hello friend, you're
- looking good today!"
\ No newline at end of file
+ looking good today!"
+int(5)
+int(6)
+int(5)
+
+Notice: Array to string conversion in %s on line 21
+int(7)
+
+Notice: Object of class stdClass to string conversion in %s on line 22
+int(7)
+int(7)
+array(5) {
+ [0]=>
+ string(3) "F0o"
+ [1]=>
+ string(3) "B4r"
+ [2]=>
+ string(1) "s"
+ [3]=>
+ string(3) "bar"
+ [4]=>
+ string(3) "foo"
+}
+array(6) {
+ [0]=>
+ string(3) "F0o"
+ [1]=>
+ string(1) "B"
+ [2]=>
+ string(1) "r"
+ [3]=>
+ string(1) "s"
+ [4]=>
+ string(3) "bar"
+ [5]=>
+ string(3) "foo"
+}
+array(5) {
+ [0]=>
+ string(3) "F0o"
+ [1]=>
+ string(3) "B4r"
+ [2]=>
+ string(1) "s"
+ [3]=>
+ string(3) "bar"
+ [4]=>
+ string(3) "foo"
+}
+
+Notice: Array to string conversion in %s on line 27
+array(7) {
+ [0]=>
+ string(1) "F"
+ [1]=>
+ string(1) "o"
+ [2]=>
+ string(1) "B"
+ [3]=>
+ string(1) "r"
+ [4]=>
+ string(1) "s"
+ [5]=>
+ string(3) "bar"
+ [6]=>
+ string(3) "foo"
+}
+
+Notice: Object of class stdClass to string conversion in %s on line 28
+array(7) {
+ [0]=>
+ string(1) "F"
+ [1]=>
+ string(1) "o"
+ [2]=>
+ string(1) "B"
+ [3]=>
+ string(1) "r"
+ [4]=>
+ string(1) "s"
+ [5]=>
+ string(3) "bar"
+ [6]=>
+ string(3) "foo"
+}
+array(7) {
+ [0]=>
+ string(1) "F"
+ [1]=>
+ string(1) "o"
+ [2]=>
+ string(1) "B"
+ [3]=>
+ string(1) "r"
+ [4]=>
+ string(1) "s"
+ [5]=>
+ string(3) "bar"
+ [6]=>
+ string(3) "foo"
+}
+array(5) {
+ [0]=>
+ string(3) "F0o"
+ [4]=>
+ string(3) "B4r"
+ [9]=>
+ string(1) "s"
+ [11]=>
+ string(3) "bar"
+ [15]=>
+ string(3) "foo"
+}
+array(6) {
+ [0]=>
+ string(3) "F0o"
+ [4]=>
+ string(1) "B"
+ [6]=>
+ string(1) "r"
+ [9]=>
+ string(1) "s"
+ [11]=>
+ string(3) "bar"
+ [15]=>
+ string(3) "foo"
+}
+array(5) {
+ [0]=>
+ string(3) "F0o"
+ [4]=>
+ string(3) "B4r"
+ [9]=>
+ string(1) "s"
+ [11]=>
+ string(3) "bar"
+ [15]=>
+ string(3) "foo"
+}
+
+Notice: Array to string conversion in %s on line 33
+array(7) {
+ [0]=>
+ string(1) "F"
+ [2]=>
+ string(1) "o"
+ [4]=>
+ string(1) "B"
+ [6]=>
+ string(1) "r"
+ [9]=>
+ string(1) "s"
+ [11]=>
+ string(3) "bar"
+ [15]=>
+ string(3) "foo"
+}
+
+Notice: Object of class stdClass to string conversion in %s on line 34
+array(7) {
+ [0]=>
+ string(1) "F"
+ [2]=>
+ string(1) "o"
+ [4]=>
+ string(1) "B"
+ [6]=>
+ string(1) "r"
+ [9]=>
+ string(1) "s"
+ [11]=>
+ string(3) "bar"
+ [15]=>
+ string(3) "foo"
+}
+array(7) {
+ [0]=>
+ string(1) "F"
+ [2]=>
+ string(1) "o"
+ [4]=>
+ string(1) "B"
+ [6]=>
+ string(1) "r"
+ [9]=>
+ string(1) "s"
+ [11]=>
+ string(3) "bar"
+ [15]=>
+ string(3) "foo"
+}
\ No newline at end of file