gh-117431: Adapt bytes and bytearray .startswith() and .endswith() to Argument Clinic (#117495)

This change gives a significant speedup, as the METH_FASTCALL calling
convention is now used.
This commit is contained in:
Erlend E. Aasland 2024-04-03 13:11:14 +02:00 committed by GitHub
parent 1dc1521042
commit 595bb496b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 324 additions and 61 deletions

View file

@ -2285,16 +2285,52 @@ bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix)
return PyBytes_FromStringAndSize(self_start, self_len);
}
static PyObject *
bytes_startswith(PyBytesObject *self, PyObject *args)
{
return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
}
/*[clinic input]
@text_signature "($self, prefix[, start[, end]], /)"
bytes.startswith
prefix as subobj: object
A bytes or a tuple of bytes to try.
start: slice_index(accept={int, NoneType}, c_default='0') = None
Optional start position. Default: start of the bytes.
end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
Optional stop position. Default: end of the bytes.
/
Return True if the bytes starts with the specified prefix, False otherwise.
[clinic start generated code]*/
static PyObject *
bytes_endswith(PyBytesObject *self, PyObject *args)
bytes_startswith_impl(PyBytesObject *self, PyObject *subobj,
Py_ssize_t start, Py_ssize_t end)
/*[clinic end generated code: output=b1e8da1cbd528e8c input=8a4165df8adfa6c9]*/
{
return _Py_bytes_endswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
subobj, start, end);
}
/*[clinic input]
@text_signature "($self, suffix[, start[, end]], /)"
bytes.endswith
suffix as subobj: object
A bytes or a tuple of bytes to try.
start: slice_index(accept={int, NoneType}, c_default='0') = None
Optional start position. Default: start of the bytes.
end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
Optional stop position. Default: end of the bytes.
/
Return True if the bytes ends with the specified suffix, False otherwise.
[clinic start generated code]*/
static PyObject *
bytes_endswith_impl(PyBytesObject *self, PyObject *subobj, Py_ssize_t start,
Py_ssize_t end)
/*[clinic end generated code: output=038b633111f3629d input=b5c3407a2a5c9aac]*/
{
return _Py_bytes_endswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
subobj, start, end);
}
@ -2491,8 +2527,7 @@ bytes_methods[] = {
{"count", (PyCFunction)bytes_count, METH_VARARGS,
_Py_count__doc__},
BYTES_DECODE_METHODDEF
{"endswith", (PyCFunction)bytes_endswith, METH_VARARGS,
_Py_endswith__doc__},
BYTES_ENDSWITH_METHODDEF
STRINGLIB_EXPANDTABS_METHODDEF
{"find", (PyCFunction)bytes_find, METH_VARARGS,
_Py_find__doc__},
@ -2532,8 +2567,7 @@ bytes_methods[] = {
BYTES_RSTRIP_METHODDEF
BYTES_SPLIT_METHODDEF
BYTES_SPLITLINES_METHODDEF
{"startswith", (PyCFunction)bytes_startswith, METH_VARARGS,
_Py_startswith__doc__},
BYTES_STARTSWITH_METHODDEF
BYTES_STRIP_METHODDEF
{"swapcase", stringlib_swapcase, METH_NOARGS,
_Py_swapcase__doc__},