fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on

the current behaviour ;-)
This commit is contained in:
Fredrik Lundh 2006-05-29 22:42:07 +00:00
parent a355c14fa1
commit b51b470eb8
2 changed files with 12 additions and 1 deletions

View File

@ -106,10 +106,18 @@ class CommonTest(unittest.TestCase):
self.checkequal(3, 'aaa', 'count', 'a')
self.checkequal(0, 'aaa', 'count', 'b')
self.checkequal(0, 'aaa', 'count', 'b')
self.checkequal(2, 'aaa', 'count', 'a', 1)
self.checkequal(0, 'aaa', 'count', 'a', 10)
self.checkequal(1, 'aaa', 'count', 'a', -1)
self.checkequal(3, 'aaa', 'count', 'a', -10)
self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
self.checkequal(3, 'aaa', 'count', '', 1)
self.checkequal(1, 'aaa', 'count', '', 10)
self.checkequal(2, 'aaa', 'count', '', -1)
self.checkequal(4, 'aaa', 'count', '', -10)
self.checkraises(TypeError, 'hello', 'count')
self.checkraises(TypeError, 'hello', 'count', 42)

View File

@ -13,8 +13,11 @@ stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
{
Py_ssize_t count;
if (sub_len == 0)
if (sub_len == 0) {
if (str_len < 0)
return 1; /* start >= len(str) */
return str_len + 1;
}
count = fastsearch(str, str_len, sub, sub_len, FAST_COUNT);