]> granicus.if.org Git - php/commitdiff
MFB51: Fixed bug #35427 (str_word_count() handles '-' incorrectly).
authorIlia Alshanetsky <iliaa@php.net>
Tue, 29 Nov 2005 16:14:47 +0000 (16:14 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Tue, 29 Nov 2005 16:14:47 +0000 (16:14 +0000)
ext/standard/string.c
ext/standard/tests/strings/str_word_count.phpt

index 8e44173afa0d0256369ae48fab33c0a46f3b4187..cfc84902666c770d54c1676f922898a9a90a1a7c 100644 (file)
@@ -6531,33 +6531,38 @@ PHP_FUNCTION(str_word_count)
        if (type == 1 || type == 2) {
                array_init(return_value);
        }
-       
+
+       /* first character cannot be ' or -, unless explicitly allowed by the user */
+       if ((*p == '\'' && (!char_list || !ch['\''])) || (*p == '-' && (!char_list || !ch['-']))) {
+               p++;
+       }
+       /* last character cannot be -, unless explicitly allowed by the user */
+       if (*(e - 1) == '-' && (!char_list || !ch['-'])) {
+               e--;
+       }
+
        while (p < e) {
-               if (isalpha(*p) || (char_list && ch[(unsigned char)*p])) {
-                       s = ++p - 1;
-                       while (isalpha(*p) || *p == '\'' || (*p == '-' && isalpha(*(p+1))) || (char_list && ch[(unsigned char)*p])) {
-                               p++;
-                       }
-                       
+               s = p;
+               while (p < e && (isalpha(*p) || (char_list && ch[(unsigned char)*p]) || *p == '\'' || *p == '-')) {
+                       p++;
+               }
+               if (p > s) {
                        switch (type)
                        {
                                case 1:
                                        buf = estrndup(s, (p-s));
-                                       add_next_index_stringl(return_value, buf, (p-s), 1);
-                                       efree(buf);
+                                       add_next_index_stringl(return_value, buf, (p-s), 0);
                                        break;
                                case 2:
                                        buf = estrndup(s, (p-s));
-                                       add_index_stringl(return_value, (s - str), buf, p-s, 1);
-                                       efree(buf);
+                                       add_index_stringl(return_value, (s - str), buf, p-s, 0);
                                        break;
                                default:
                                        word_count++;
                                        break;          
                        }
-               } else {
-                       p++;
                }
+               p++;
        }
        
        if (!type) {
index 2e5d15d54aa47e151bfc439a39aebe61033ba72c..c2621d72115fc63d2c35bd5ae9cc0df0db99b23a 100644 (file)
@@ -36,7 +36,11 @@ 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, ""));
-
+var_dump(str_word_count("foo'0 bar-0var", 2, "0"));
+var_dump(str_word_count("'foo'", 2));
+var_dump(str_word_count("'foo'", 2, "'"));
+var_dump(str_word_count("-foo-", 2));
+var_dump(str_word_count("-foo-", 2, "-"));
 ?>
 --EXPECTF--
 array(6) {
@@ -225,4 +229,26 @@ array(7) {
   string(3) "bar"
   [15]=>
   string(3) "foo"
-}
\ No newline at end of file
+}
+array(2) {
+  [0]=>
+  string(5) "foo'0"
+  [6]=>
+  string(8) "bar-0var"
+}
+array(1) {
+  [1]=>
+  string(4) "foo'"
+}
+array(1) {
+  [0]=>
+  string(5) "'foo'"
+}
+array(1) {
+  [1]=>
+  string(3) "foo"
+}
+array(1) {
+  [0]=>
+  string(5) "-foo-"
+}