Skip to content

Commit a0c7236

Browse files
committed
[mypyc] feat: optimize C code for str.count
1 parent 555edf3 commit a0c7236

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

mypyc/lib-rt/str_ops.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -532,28 +532,13 @@ PyObject *CPy_Encode(PyObject *obj, PyObject *encoding, PyObject *errors) {
532532
}
533533
}
534534

535-
Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start) {
536-
Py_ssize_t temp_start = CPyTagged_AsSsize_t(start);
537-
if (temp_start == -1 && PyErr_Occurred()) {
538-
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
539-
return -1;
540-
}
535+
Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, Py_ssize_t start) {
541536
Py_ssize_t end = PyUnicode_GET_LENGTH(unicode);
542-
return PyUnicode_Count(unicode, substring, temp_start, end);
537+
return PyUnicode_Count(unicode, substring, start, end);
543538
}
544539

545-
Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end) {
546-
Py_ssize_t temp_start = CPyTagged_AsSsize_t(start);
547-
if (temp_start == -1 && PyErr_Occurred()) {
548-
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
549-
return -1;
550-
}
551-
Py_ssize_t temp_end = CPyTagged_AsSsize_t(end);
552-
if (temp_end == -1 && PyErr_Occurred()) {
553-
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
554-
return -1;
555-
}
556-
return PyUnicode_Count(unicode, substring, temp_start, temp_end);
540+
Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, Py_ssize_t start, Py_ssize_t end) {
541+
return PyUnicode_Count(unicode, substring, start, end);
557542
}
558543

559544

mypyc/primitives/str_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@
318318
# str.count(substring, start)
319319
method_op(
320320
name="count",
321-
arg_types=[str_rprimitive, str_rprimitive, int_rprimitive],
321+
arg_types=[str_rprimitive, str_rprimitive, c_pyssize_t_rprimitive],
322322
return_type=c_pyssize_t_rprimitive,
323323
c_function_name="CPyStr_Count",
324324
error_kind=ERR_NEG_INT,
@@ -327,7 +327,7 @@
327327
# str.count(substring, start, end)
328328
method_op(
329329
name="count",
330-
arg_types=[str_rprimitive, str_rprimitive, int_rprimitive, int_rprimitive],
330+
arg_types=[str_rprimitive, str_rprimitive, c_pyssize_t_rprimitive, c_pyssize_t_rprimitive],
331331
return_type=c_pyssize_t_rprimitive,
332332
c_function_name="CPyStr_CountFull",
333333
error_kind=ERR_NEG_INT,

0 commit comments

Comments
 (0)