gh-97982: Factorize PyUnicode_Count() and unicode_count() code (#98025)

Add unicode_count_impl() to factorize PyUnicode_Count()
and unicode_count() code.
This commit is contained in:
Nikita Sobolev 2022-10-12 19:27:53 +03:00 committed by GitHub
parent e9569ec43e
commit ccab67ba79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 60 deletions

View file

@ -241,6 +241,10 @@ class UnicodeTest(string_tests.CommonTest,
self.checkequal(0, 'a' * 10, 'count', 'a\u0102')
self.checkequal(0, 'a' * 10, 'count', 'a\U00100304')
self.checkequal(0, '\u0102' * 10, 'count', '\u0102\U00100304')
# test subclass
class MyStr(str):
pass
self.checkequal(3, MyStr('aaa'), 'count', 'a')
def test_find(self):
string_tests.CommonTest.test_find(self)
@ -3002,6 +3006,12 @@ class CAPITest(unittest.TestCase):
self.assertEqual(unicode_count(uni, ch, 0, len(uni)), 1)
self.assertEqual(unicode_count(st, ch, 0, len(st)), 0)
# subclasses should still work
class MyStr(str):
pass
self.assertEqual(unicode_count(MyStr('aab'), 'a', 0, 3), 2)
# Test PyUnicode_FindChar()
@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')