gh-117431: Adapt str.find and friends to Argument Clinic (#117468)

This change gives a significant speedup, as the METH_FASTCALL calling
convention is now used. The following methods are adapted:

- str.count
- str.find
- str.index
- str.rfind
- str.rindex
This commit is contained in:
Erlend E. Aasland 2024-04-03 17:59:18 +02:00 committed by GitHub
parent 345194de8c
commit 7ecd55d604
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 444 additions and 222 deletions

View file

@ -136,6 +136,61 @@ exit:
return return_value;
}
PyDoc_STRVAR(unicode_count__doc__,
"count($self, sub[, start[, end]], /)\n"
"--\n"
"\n"
"Return the number of non-overlapping occurrences of substring sub in string S[start:end].\n"
"\n"
"Optional arguments start and end are interpreted as in slice notation.");
#define UNICODE_COUNT_METHODDEF \
{"count", _PyCFunction_CAST(unicode_count), METH_FASTCALL, unicode_count__doc__},
static Py_ssize_t
unicode_count_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
Py_ssize_t end);
static PyObject *
unicode_count(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *substr;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
Py_ssize_t _return_value;
if (!_PyArg_CheckPositional("count", nargs, 1, 3)) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("count", "argument 1", "str", args[0]);
goto exit;
}
substr = args[0];
if (nargs < 2) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[1], &start)) {
goto exit;
}
if (nargs < 3) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[2], &end)) {
goto exit;
}
skip_optional:
_return_value = unicode_count_impl(str, substr, start, end);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyLong_FromSsize_t(_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(unicode_encode__doc__,
"encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
"--\n"
@ -301,6 +356,118 @@ exit:
return return_value;
}
PyDoc_STRVAR(unicode_find__doc__,
"find($self, sub, start=None, end=None, /)\n"
"--\n"
"\n"
"Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
"\n"
"Optional arguments start and end are interpreted as in slice notation.\n"
"Return -1 on failure.");
#define UNICODE_FIND_METHODDEF \
{"find", _PyCFunction_CAST(unicode_find), METH_FASTCALL, unicode_find__doc__},
static Py_ssize_t
unicode_find_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
Py_ssize_t end);
static PyObject *
unicode_find(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *substr;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
Py_ssize_t _return_value;
if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("find", "argument 1", "str", args[0]);
goto exit;
}
substr = args[0];
if (nargs < 2) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[1], &start)) {
goto exit;
}
if (nargs < 3) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[2], &end)) {
goto exit;
}
skip_optional:
_return_value = unicode_find_impl(str, substr, start, end);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyLong_FromSsize_t(_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(unicode_index__doc__,
"index($self, sub, start=None, end=None, /)\n"
"--\n"
"\n"
"Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
"\n"
"Optional arguments start and end are interpreted as in slice notation.\n"
"Raises ValueError when the substring is not found.");
#define UNICODE_INDEX_METHODDEF \
{"index", _PyCFunction_CAST(unicode_index), METH_FASTCALL, unicode_index__doc__},
static Py_ssize_t
unicode_index_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
Py_ssize_t end);
static PyObject *
unicode_index(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *substr;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
Py_ssize_t _return_value;
if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("index", "argument 1", "str", args[0]);
goto exit;
}
substr = args[0];
if (nargs < 2) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[1], &start)) {
goto exit;
}
if (nargs < 3) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[2], &end)) {
goto exit;
}
skip_optional:
_return_value = unicode_index_impl(str, substr, start, end);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyLong_FromSsize_t(_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(unicode_isascii__doc__,
"isascii($self, /)\n"
"--\n"
@ -892,6 +1059,118 @@ exit:
return return_value;
}
PyDoc_STRVAR(unicode_rfind__doc__,
"rfind($self, sub, start=None, end=None, /)\n"
"--\n"
"\n"
"Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
"\n"
"Optional arguments start and end are interpreted as in slice notation.\n"
"Return -1 on failure.");
#define UNICODE_RFIND_METHODDEF \
{"rfind", _PyCFunction_CAST(unicode_rfind), METH_FASTCALL, unicode_rfind__doc__},
static Py_ssize_t
unicode_rfind_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
Py_ssize_t end);
static PyObject *
unicode_rfind(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *substr;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
Py_ssize_t _return_value;
if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("rfind", "argument 1", "str", args[0]);
goto exit;
}
substr = args[0];
if (nargs < 2) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[1], &start)) {
goto exit;
}
if (nargs < 3) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[2], &end)) {
goto exit;
}
skip_optional:
_return_value = unicode_rfind_impl(str, substr, start, end);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyLong_FromSsize_t(_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(unicode_rindex__doc__,
"rindex($self, sub, start=None, end=None, /)\n"
"--\n"
"\n"
"Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].\n"
"\n"
"Optional arguments start and end are interpreted as in slice notation.\n"
"Raises ValueError when the substring is not found.");
#define UNICODE_RINDEX_METHODDEF \
{"rindex", _PyCFunction_CAST(unicode_rindex), METH_FASTCALL, unicode_rindex__doc__},
static Py_ssize_t
unicode_rindex_impl(PyObject *str, PyObject *substr, Py_ssize_t start,
Py_ssize_t end);
static PyObject *
unicode_rindex(PyObject *str, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *substr;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
Py_ssize_t _return_value;
if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("rindex", "argument 1", "str", args[0]);
goto exit;
}
substr = args[0];
if (nargs < 2) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[1], &start)) {
goto exit;
}
if (nargs < 3) {
goto skip_optional;
}
if (!_PyEval_SliceIndex(args[2], &end)) {
goto exit;
}
skip_optional:
_return_value = unicode_rindex_impl(str, substr, start, end);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyLong_FromSsize_t(_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(unicode_rjust__doc__,
"rjust($self, width, fillchar=\' \', /)\n"
"--\n"
@ -1609,4 +1888,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=1734aa1fcc9b076a input=a9049054013a1b77]*/
/*[clinic end generated code: output=3aa49013ffa3fa93 input=a9049054013a1b77]*/