idx++;
}
/* last character cannot be -, unless explicitly allowed by the user */
- if (str[str_len-1] == (UChar)0x2d /*'-'*/ &&
+ if (str_len && str[str_len-1] == (UChar)0x2d /*'-'*/ &&
(!char_list || !u_memchr(char_list, 0x2d /*'-'*/, char_list_len))) {
str_len--;
}
p++;
}
/* last character cannot be -, unless explicitly allowed by the user */
- if (*(e - 1) == '-' && (!char_list || !ch['-'])) {
+ if (str_len && *(e - 1) == '-' && (!char_list || !ch['-'])) {
e--;
}
return;
}
- if (type == 1 || type == 2) {
- array_init(return_value);
+ switch (type) {
+ case 1:
+ case 2:
+ array_init(return_value);
+ break;
+ case 0:
+ /* nothing to be done */
+ break;
+ default:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid format value %ld", type);
+ RETURN_FALSE;
}
if (str_type == IS_UNICODE) {
var_dump(str_word_count("'foo'", 2, "'"));
var_dump(str_word_count("-foo-", 2));
var_dump(str_word_count("-foo-", 2, "-"));
+
+echo "Done\n";
?>
--EXPECTF--
array(6) {
string(5) "today"
}
int(6)
-NULL
-NULL
-NULL
-NULL
+
+Warning: str_word_count(): Invalid format value 3 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 123 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 1569325056 in %s on line %d
+bool(false)
Warning: str_word_count() expects parameter 2 to be long, array given in %s on line %d
NULL
[0]=>
string(5) "-foo-"
}
+Done
--UEXPECTF--
array(6) {
[0]=>
unicode(5) "today"
}
int(6)
-NULL
-NULL
-NULL
-NULL
+
+Warning: str_word_count(): Invalid format value 3 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 123 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 1569325056 in %s on line %d
+bool(false)
Warning: str_word_count() expects parameter 2 to be long, array given in %s on line %d
NULL
[0]=>
unicode(5) "-foo-"
}
+Done
--- /dev/null
+--TEST--
+str_word_count() and invalid arguments
+--FILE--
+<?php
+
+var_dump(str_word_count(""));
+var_dump(str_word_count("", -1));
+var_dump(str_word_count("", -1, $a));
+var_dump($a);
+
+echo "Done\n";
+?>
+--EXPECTF--
+int(0)
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Notice: Undefined variable: a in %s on line %d
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Notice: Undefined variable: a in %s on line %d
+NULL
+Done