-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
gh-97982: Reuse PyUnicode_Count in unicode_count
#98025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
b239ae5
e9d6358
886d6e3
482010a
4b2447e
35ecc77
8270d4b
2dabc73
74f8c1c
69a94e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8964,21 +8964,35 @@ _PyUnicode_InsertThousandsGrouping( | |
| return count; | ||
| } | ||
|
|
||
| static Py_ssize_t | ||
| anylib_count(int kind, PyObject *sstr, const void* sbuf, Py_ssize_t slen, | ||
| PyObject *str1, const void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) | ||
| { | ||
| switch (kind) { | ||
| case PyUnicode_1BYTE_KIND: | ||
| return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); | ||
| case PyUnicode_2BYTE_KIND: | ||
| return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); | ||
| case PyUnicode_4BYTE_KIND: | ||
| return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); | ||
| } | ||
| Py_UNREACHABLE(); | ||
| } | ||
|
|
||
| Py_ssize_t | ||
| PyUnicode_Count(PyObject *str, | ||
| PyObject *substr, | ||
| Py_ssize_t start, | ||
| Py_ssize_t end) | ||
| static Py_ssize_t | ||
| unicode_count_impl(PyObject *str, | ||
| PyObject *substr, | ||
| Py_ssize_t start, | ||
| Py_ssize_t end) | ||
| { | ||
| assert(PyUnicode_Check(str)); | ||
| assert(PyUnicode_Check(substr)); | ||
|
|
||
| Py_ssize_t result; | ||
| int kind1, kind2; | ||
| const void *buf1 = NULL, *buf2 = NULL; | ||
| Py_ssize_t len1, len2; | ||
|
|
||
| if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0) | ||
| return -1; | ||
|
|
||
| kind1 = PyUnicode_KIND(str); | ||
| kind2 = PyUnicode_KIND(substr); | ||
| if (kind1 < kind2) | ||
|
|
@@ -8998,28 +9012,10 @@ PyUnicode_Count(PyObject *str, | |
| goto onError; | ||
| } | ||
|
|
||
| switch (kind1) { | ||
| case PyUnicode_1BYTE_KIND: | ||
| result = ucs1lib_count( | ||
| ((const Py_UCS1*)buf1) + start, end - start, | ||
| buf2, len2, PY_SSIZE_T_MAX | ||
| ); | ||
| break; | ||
| case PyUnicode_2BYTE_KIND: | ||
| result = ucs2lib_count( | ||
| ((const Py_UCS2*)buf1) + start, end - start, | ||
| buf2, len2, PY_SSIZE_T_MAX | ||
| ); | ||
| break; | ||
| case PyUnicode_4BYTE_KIND: | ||
| result = ucs4lib_count( | ||
| ((const Py_UCS4*)buf1) + start, end - start, | ||
| buf2, len2, PY_SSIZE_T_MAX | ||
| ); | ||
| break; | ||
| default: | ||
| Py_UNREACHABLE(); | ||
| } | ||
| result = anylib_count(kind1, | ||
| str, buf1 + start, end - start, | ||
|
||
| substr, buf2, len2, | ||
| PY_SSIZE_T_MAX); | ||
|
|
||
| assert((kind2 != kind1) == (buf2 != PyUnicode_DATA(substr))); | ||
| if (kind2 != kind1) | ||
|
|
@@ -9033,6 +9029,18 @@ PyUnicode_Count(PyObject *str, | |
| return -1; | ||
| } | ||
|
|
||
| Py_ssize_t | ||
| PyUnicode_Count(PyObject *str, | ||
| PyObject *substr, | ||
| Py_ssize_t start, | ||
| Py_ssize_t end) | ||
| { | ||
| if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0) | ||
| return -1; | ||
|
|
||
| return unicode_count_impl(str, substr, start, end); | ||
| } | ||
|
|
||
| Py_ssize_t | ||
| PyUnicode_Find(PyObject *str, | ||
| PyObject *substr, | ||
|
|
@@ -9892,21 +9900,6 @@ anylib_find(int kind, PyObject *str1, const void *buf1, Py_ssize_t len1, | |
| Py_UNREACHABLE(); | ||
| } | ||
|
|
||
| static Py_ssize_t | ||
| anylib_count(int kind, PyObject *sstr, const void* sbuf, Py_ssize_t slen, | ||
| PyObject *str1, const void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) | ||
| { | ||
| switch (kind) { | ||
| case PyUnicode_1BYTE_KIND: | ||
| return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); | ||
| case PyUnicode_2BYTE_KIND: | ||
| return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); | ||
| case PyUnicode_4BYTE_KIND: | ||
| return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); | ||
| } | ||
| Py_UNREACHABLE(); | ||
| } | ||
|
|
||
| static void | ||
| replace_1char_inplace(PyObject *u, Py_ssize_t pos, | ||
| Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) | ||
|
|
@@ -10848,62 +10841,16 @@ unicode_count(PyObject *self, PyObject *args) | |
| PyObject *substring = NULL; /* initialize to fix a compiler warning */ | ||
| Py_ssize_t start = 0; | ||
| Py_ssize_t end = PY_SSIZE_T_MAX; | ||
| PyObject *result; | ||
| int kind1, kind2; | ||
| const void *buf1, *buf2; | ||
| Py_ssize_t len1, len2, iresult; | ||
| Py_ssize_t result; | ||
|
|
||
| if (!parse_args_finds_unicode("count", args, &substring, &start, &end)) | ||
| return NULL; | ||
|
|
||
| kind1 = PyUnicode_KIND(self); | ||
| kind2 = PyUnicode_KIND(substring); | ||
| if (kind1 < kind2) | ||
| return PyLong_FromLong(0); | ||
|
|
||
| len1 = PyUnicode_GET_LENGTH(self); | ||
| len2 = PyUnicode_GET_LENGTH(substring); | ||
| ADJUST_INDICES(start, end, len1); | ||
| if (end - start < len2) | ||
| return PyLong_FromLong(0); | ||
|
|
||
| buf1 = PyUnicode_DATA(self); | ||
| buf2 = PyUnicode_DATA(substring); | ||
| if (kind2 != kind1) { | ||
| buf2 = unicode_askind(kind2, buf2, len2, kind1); | ||
| if (!buf2) | ||
| return NULL; | ||
| } | ||
| switch (kind1) { | ||
| case PyUnicode_1BYTE_KIND: | ||
| iresult = ucs1lib_count( | ||
| ((const Py_UCS1*)buf1) + start, end - start, | ||
| buf2, len2, PY_SSIZE_T_MAX | ||
| ); | ||
| break; | ||
| case PyUnicode_2BYTE_KIND: | ||
| iresult = ucs2lib_count( | ||
| ((const Py_UCS2*)buf1) + start, end - start, | ||
| buf2, len2, PY_SSIZE_T_MAX | ||
| ); | ||
| break; | ||
| case PyUnicode_4BYTE_KIND: | ||
| iresult = ucs4lib_count( | ||
| ((const Py_UCS4*)buf1) + start, end - start, | ||
| buf2, len2, PY_SSIZE_T_MAX | ||
| ); | ||
| break; | ||
| default: | ||
| Py_UNREACHABLE(); | ||
| } | ||
|
|
||
| result = PyLong_FromSsize_t(iresult); | ||
|
|
||
| assert((kind2 == kind1) == (buf2 == PyUnicode_DATA(substring))); | ||
| if (kind2 != kind1) | ||
| PyMem_Free((void *)buf2); | ||
| result = unicode_count_impl(self, substring, start, end); | ||
| if (result == -1) | ||
| return NULL; | ||
|
|
||
| return result; | ||
| return PyLong_FromSsize_t(result); | ||
| } | ||
|
|
||
| /*[clinic input] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.