Skip to content

Commit b239ae5

Browse files
committed
pythongh-97982: Reuse PyUnicode_Count in unicode_count
1 parent e500cc0 commit b239ae5

File tree

2 files changed

+3
-50
lines changed

2 files changed

+3
-50
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reuse ``PyUnicode_Count`` function in ``unicode_count`` in
2+
``unicodeobject.c``.

Objects/unicodeobject.c

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10858,60 +10858,11 @@ unicode_count(PyObject *self, PyObject *args)
1085810858
Py_ssize_t start = 0;
1085910859
Py_ssize_t end = PY_SSIZE_T_MAX;
1086010860
PyObject *result;
10861-
int kind1, kind2;
10862-
const void *buf1, *buf2;
10863-
Py_ssize_t len1, len2, iresult;
1086410861

1086510862
if (!parse_args_finds_unicode("count", args, &substring, &start, &end))
1086610863
return NULL;
1086710864

10868-
kind1 = PyUnicode_KIND(self);
10869-
kind2 = PyUnicode_KIND(substring);
10870-
if (kind1 < kind2)
10871-
return PyLong_FromLong(0);
10872-
10873-
len1 = PyUnicode_GET_LENGTH(self);
10874-
len2 = PyUnicode_GET_LENGTH(substring);
10875-
ADJUST_INDICES(start, end, len1);
10876-
if (end - start < len2)
10877-
return PyLong_FromLong(0);
10878-
10879-
buf1 = PyUnicode_DATA(self);
10880-
buf2 = PyUnicode_DATA(substring);
10881-
if (kind2 != kind1) {
10882-
buf2 = unicode_askind(kind2, buf2, len2, kind1);
10883-
if (!buf2)
10884-
return NULL;
10885-
}
10886-
switch (kind1) {
10887-
case PyUnicode_1BYTE_KIND:
10888-
iresult = ucs1lib_count(
10889-
((const Py_UCS1*)buf1) + start, end - start,
10890-
buf2, len2, PY_SSIZE_T_MAX
10891-
);
10892-
break;
10893-
case PyUnicode_2BYTE_KIND:
10894-
iresult = ucs2lib_count(
10895-
((const Py_UCS2*)buf1) + start, end - start,
10896-
buf2, len2, PY_SSIZE_T_MAX
10897-
);
10898-
break;
10899-
case PyUnicode_4BYTE_KIND:
10900-
iresult = ucs4lib_count(
10901-
((const Py_UCS4*)buf1) + start, end - start,
10902-
buf2, len2, PY_SSIZE_T_MAX
10903-
);
10904-
break;
10905-
default:
10906-
Py_UNREACHABLE();
10907-
}
10908-
10909-
result = PyLong_FromSsize_t(iresult);
10910-
10911-
assert((kind2 == kind1) == (buf2 == PyUnicode_DATA(substring)));
10912-
if (kind2 != kind1)
10913-
PyMem_Free((void *)buf2);
10914-
10865+
result = PyLong_FromSsize_t(PyUnicode_Count(self, substring, start, end));
1091510866
return result;
1091610867
}
1091710868

0 commit comments

Comments
 (0)