gh-117431: Adapt bytes and bytearray .startswith() and .endswith() to… · python/cpython@595bb49

GitHub

@@ -771,66 +771,47 @@ tailmatch(const char *str, Py_ssize_t len, PyObject *substr,

771771772772staticPyObject*

773773_Py_bytes_tailmatch(constchar*str, Py_ssize_tlen,

774-constchar*function_name, PyObject*args,

774+constchar*function_name, PyObject*subobj,

775+Py_ssize_tstart, Py_ssize_tend,

775776intdirection)

776777{

777-Py_ssize_tstart=0;

778-Py_ssize_tend=PY_SSIZE_T_MAX;

779-PyObject*subobj=NULL;

780-intresult;

781-782-if (!stringlib_parse_args_finds(function_name, args, &subobj, &start, &end))

783-returnNULL;

784778if (PyTuple_Check(subobj)) {

785779Py_ssize_ti;

786780for (i=0; i<PyTuple_GET_SIZE(subobj); i++) {

787-result=tailmatch(str, len, PyTuple_GET_ITEM(subobj, i),

788-start, end, direction);

789-if (result==-1)

781+PyObject*item=PyTuple_GET_ITEM(subobj, i);

782+intresult=tailmatch(str, len, item,start, end, direction);

783+if (result<0) {

790784returnNULL;

785+ }

791786elseif (result) {

792787Py_RETURN_TRUE;

793788 }

794789 }

795790Py_RETURN_FALSE;

796791 }

797-result=tailmatch(str, len, subobj, start, end, direction);

792+intresult=tailmatch(str, len, subobj, start, end, direction);

798793if (result==-1) {

799-if (PyErr_ExceptionMatches(PyExc_TypeError))

794+if (PyErr_ExceptionMatches(PyExc_TypeError)) {

800795PyErr_Format(PyExc_TypeError,

801796"%s first arg must be bytes or a tuple of bytes, "

802797"not %s",

803798function_name, Py_TYPE(subobj)->tp_name);

799+ }

804800returnNULL;

805801 }

806-else

807-returnPyBool_FromLong(result);

802+returnPyBool_FromLong(result);

808803}

809804810-PyDoc_STRVAR_shared(_Py_startswith__doc__,

811-"B.startswith(prefix[, start[, end]]) -> bool\n\

812-\n\

813-Return True if B starts with the specified prefix, False otherwise.\n\

814-With optional start, test B beginning at that position.\n\

815-With optional end, stop comparing B at that position.\n\

816-prefix can also be a tuple of bytes to try.");

817-818805PyObject*

819-_Py_bytes_startswith(constchar*str, Py_ssize_tlen, PyObject*args)

806+_Py_bytes_startswith(constchar*str, Py_ssize_tlen, PyObject*subobj,

807+Py_ssize_tstart, Py_ssize_tend)

820808{

821-return_Py_bytes_tailmatch(str, len, "startswith", args, -1);

809+return_Py_bytes_tailmatch(str, len, "startswith", subobj, start, end, -1);

822810}

823811824-PyDoc_STRVAR_shared(_Py_endswith__doc__,

825-"B.endswith(suffix[, start[, end]]) -> bool\n\

826-\n\

827-Return True if B ends with the specified suffix, False otherwise.\n\

828-With optional start, test B beginning at that position.\n\

829-With optional end, stop comparing B at that position.\n\

830-suffix can also be a tuple of bytes to try.");

831-832812PyObject*

833-_Py_bytes_endswith(constchar*str, Py_ssize_tlen, PyObject*args)

813+_Py_bytes_endswith(constchar*str, Py_ssize_tlen, PyObject*subobj,

814+Py_ssize_tstart, Py_ssize_tend)

834815{

835-return_Py_bytes_tailmatch(str, len, "endswith", args, +1);

816+return_Py_bytes_tailmatch(str, len, "endswith", subobj, start, end, +1);

836817}