summaryrefslogtreecommitdiffstats
path: root/Objects/clinic
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend@python.org>2024-04-03 07:11:39 (GMT)
committerGitHub <noreply@github.com>2024-04-03 07:11:39 (GMT)
commit444156ede44204ef16c9d3cfcb03a637535fd5bf (patch)
treebfaf62af52402c60451346130a28501dd34eca5b /Objects/clinic
parent65524ab38875bb0b89fb499531bb772a4fb45b01 (diff)
downloadcpython-444156ede44204ef16c9d3cfcb03a637535fd5bf.zip
cpython-444156ede44204ef16c9d3cfcb03a637535fd5bf.tar.gz
cpython-444156ede44204ef16c9d3cfcb03a637535fd5bf.tar.bz2
gh-117431: Adapt str.startswith and str.endswith to Argument Clinic (#117466)
This change gives a significant speedup, as the METH_FASTCALL calling convention is now used.
Diffstat (limited to 'Objects/clinic')
-rw-r--r--Objects/clinic/unicodeobject.c.h104
1 files changed, 103 insertions, 1 deletions
diff --git a/Objects/clinic/unicodeobject.c.h b/Objects/clinic/unicodeobject.c.h
index 3e5167d..c956bb1 100644
--- a/Objects/clinic/unicodeobject.c.h
+++ b/Objects/clinic/unicodeobject.c.h
@@ -1369,6 +1369,108 @@ exit:
return return_value;
}
+PyDoc_STRVAR(unicode_startswith__doc__,
+"startswith($self, prefix[, start[, end]], /)\n"
+"--\n"
+"\n"
+"Return True if the string starts with the specified prefix, False otherwise.\n"
+"\n"
+" prefix\n"
+" A string or a tuple of strings to try.\n"
+" start\n"
+" Optional start position. Default: start of the string.\n"
+" end\n"
+" Optional stop position. Default: end of the string.");
+
+#define UNICODE_STARTSWITH_METHODDEF \
+ {"startswith", _PyCFunction_CAST(unicode_startswith), METH_FASTCALL, unicode_startswith__doc__},
+
+static PyObject *
+unicode_startswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
+ Py_ssize_t end);
+
+static PyObject *
+unicode_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *subobj;
+ Py_ssize_t start = 0;
+ Py_ssize_t end = PY_SSIZE_T_MAX;
+
+ if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) {
+ goto exit;
+ }
+ subobj = 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_startswith_impl(self, subobj, start, end);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(unicode_endswith__doc__,
+"endswith($self, prefix[, start[, end]], /)\n"
+"--\n"
+"\n"
+"Return True if the string ends with the specified prefix, False otherwise.\n"
+"\n"
+" prefix\n"
+" A string or a tuple of strings to try.\n"
+" start\n"
+" Optional start position. Default: start of the string.\n"
+" end\n"
+" Optional stop position. Default: end of the string.");
+
+#define UNICODE_ENDSWITH_METHODDEF \
+ {"endswith", _PyCFunction_CAST(unicode_endswith), METH_FASTCALL, unicode_endswith__doc__},
+
+static PyObject *
+unicode_endswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
+ Py_ssize_t end);
+
+static PyObject *
+unicode_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *subobj;
+ Py_ssize_t start = 0;
+ Py_ssize_t end = PY_SSIZE_T_MAX;
+
+ if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) {
+ goto exit;
+ }
+ subobj = 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_endswith_impl(self, subobj, start, end);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(unicode___format____doc__,
"__format__($self, format_spec, /)\n"
"--\n"
@@ -1507,4 +1609,4 @@ skip_optional_pos:
exit:
return return_value;
}
-/*[clinic end generated code: output=1aab29bab5201c78 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e495e878d8283217 input=a9049054013a1b77]*/