summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Objects/bytes_methods.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tools/python3/Objects/bytes_methods.c')
-rw-r--r--contrib/tools/python3/Objects/bytes_methods.c154
1 files changed, 42 insertions, 112 deletions
diff --git a/contrib/tools/python3/Objects/bytes_methods.c b/contrib/tools/python3/Objects/bytes_methods.c
index 33aa9c3db6e..981aa571643 100644
--- a/contrib/tools/python3/Objects/bytes_methods.c
+++ b/contrib/tools/python3/Objects/bytes_methods.c
@@ -1,4 +1,3 @@
-#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_bytes_methods.h"
@@ -454,31 +453,21 @@ stringlib_parse_args_finds().
*/
Py_LOCAL_INLINE(int)
-parse_args_finds_byte(const char *function_name, PyObject *args,
- PyObject **subobj, char *byte,
- Py_ssize_t *start, Py_ssize_t *end)
+parse_args_finds_byte(const char *function_name, PyObject **subobj, char *byte)
{
- PyObject *tmp_subobj;
- Py_ssize_t ival;
-
- if(!stringlib_parse_args_finds(function_name, args, &tmp_subobj,
- start, end))
- return 0;
-
- if (PyObject_CheckBuffer(tmp_subobj)) {
- *subobj = tmp_subobj;
+ if (PyObject_CheckBuffer(*subobj)) {
return 1;
}
- if (!_PyIndex_Check(tmp_subobj)) {
+ if (!_PyIndex_Check(*subobj)) {
PyErr_Format(PyExc_TypeError,
"argument should be integer or bytes-like object, "
"not '%.200s'",
- Py_TYPE(tmp_subobj)->tp_name);
+ Py_TYPE(*subobj)->tp_name);
return 0;
}
- ival = PyNumber_AsSsize_t(tmp_subobj, NULL);
+ Py_ssize_t ival = PyNumber_AsSsize_t(*subobj, NULL);
if (ival == -1 && PyErr_Occurred()) {
return 0;
}
@@ -509,19 +498,19 @@ parse_args_finds_byte(const char *function_name, PyObject *args,
Py_LOCAL_INLINE(Py_ssize_t)
find_internal(const char *str, Py_ssize_t len,
- const char *function_name, PyObject *args, int dir)
+ const char *function_name, PyObject *subobj,
+ Py_ssize_t start, Py_ssize_t end,
+ int dir)
{
- PyObject *subobj;
char byte;
Py_buffer subbuf;
const char *sub;
Py_ssize_t sub_len;
- Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Py_ssize_t res;
- if (!parse_args_finds_byte(function_name, args,
- &subobj, &byte, &start, &end))
+ if (!parse_args_finds_byte(function_name, &subobj, &byte)) {
return -2;
+ }
if (subobj) {
if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
@@ -567,37 +556,21 @@ find_internal(const char *str, Py_ssize_t len,
return res;
}
-PyDoc_STRVAR_shared(_Py_find__doc__,
-"B.find(sub[, start[, end]]) -> int\n\
-\n\
-Return the lowest index in B where subsection sub is found,\n\
-such that sub is contained within B[start,end]. Optional\n\
-arguments start and end are interpreted as in slice notation.\n\
-\n\
-Return -1 on failure.");
-
PyObject *
-_Py_bytes_find(const char *str, Py_ssize_t len, PyObject *args)
+_Py_bytes_find(const char *str, Py_ssize_t len, PyObject *sub,
+ Py_ssize_t start, Py_ssize_t end)
{
- Py_ssize_t result = find_internal(str, len, "find", args, +1);
+ Py_ssize_t result = find_internal(str, len, "find", sub, start, end, +1);
if (result == -2)
return NULL;
return PyLong_FromSsize_t(result);
}
-PyDoc_STRVAR_shared(_Py_index__doc__,
-"B.index(sub[, start[, end]]) -> int\n\
-\n\
-Return the lowest index in B where subsection sub is found,\n\
-such that sub is contained within B[start,end]. Optional\n\
-arguments start and end are interpreted as in slice notation.\n\
-\n\
-Raises ValueError when the subsection is not found.");
-
PyObject *
-_Py_bytes_index(const char *str, Py_ssize_t len, PyObject *args)
+_Py_bytes_index(const char *str, Py_ssize_t len, PyObject *sub,
+ Py_ssize_t start, Py_ssize_t end)
{
- Py_ssize_t result = find_internal(str, len, "index", args, +1);
+ Py_ssize_t result = find_internal(str, len, "index", sub, start, end, +1);
if (result == -2)
return NULL;
if (result == -1) {
@@ -608,37 +581,21 @@ _Py_bytes_index(const char *str, Py_ssize_t len, PyObject *args)
return PyLong_FromSsize_t(result);
}
-PyDoc_STRVAR_shared(_Py_rfind__doc__,
-"B.rfind(sub[, start[, end]]) -> int\n\
-\n\
-Return the highest index in B where subsection sub is found,\n\
-such that sub is contained within B[start,end]. Optional\n\
-arguments start and end are interpreted as in slice notation.\n\
-\n\
-Return -1 on failure.");
-
PyObject *
-_Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *args)
+_Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *sub,
+ Py_ssize_t start, Py_ssize_t end)
{
- Py_ssize_t result = find_internal(str, len, "rfind", args, -1);
+ Py_ssize_t result = find_internal(str, len, "rfind", sub, start, end, -1);
if (result == -2)
return NULL;
return PyLong_FromSsize_t(result);
}
-PyDoc_STRVAR_shared(_Py_rindex__doc__,
-"B.rindex(sub[, start[, end]]) -> int\n\
-\n\
-Return the highest index in B where subsection sub is found,\n\
-such that sub is contained within B[start,end]. Optional\n\
-arguments start and end are interpreted as in slice notation.\n\
-\n\
-Raise ValueError when the subsection is not found.");
-
PyObject *
-_Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *args)
+_Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *sub,
+ Py_ssize_t start, Py_ssize_t end)
{
- Py_ssize_t result = find_internal(str, len, "rindex", args, -1);
+ Py_ssize_t result = find_internal(str, len, "rindex", sub, start, end, -1);
if (result == -2)
return NULL;
if (result == -1) {
@@ -649,28 +606,20 @@ _Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *args)
return PyLong_FromSsize_t(result);
}
-PyDoc_STRVAR_shared(_Py_count__doc__,
-"B.count(sub[, start[, end]]) -> int\n\
-\n\
-Return the number of non-overlapping occurrences of subsection sub in\n\
-bytes B[start:end]. Optional arguments start and end are interpreted\n\
-as in slice notation.");
-
PyObject *
-_Py_bytes_count(const char *str, Py_ssize_t len, PyObject *args)
+_Py_bytes_count(const char *str, Py_ssize_t len, PyObject *sub_obj,
+ Py_ssize_t start, Py_ssize_t end)
{
- PyObject *sub_obj;
const char *sub;
Py_ssize_t sub_len;
char byte;
- Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
Py_buffer vsub;
PyObject *count_obj;
- if (!parse_args_finds_byte("count", args,
- &sub_obj, &byte, &start, &end))
+ if (!parse_args_finds_byte("count", &sub_obj, &byte)) {
return NULL;
+ }
if (sub_obj) {
if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
@@ -772,66 +721,47 @@ notfound:
static PyObject *
_Py_bytes_tailmatch(const char *str, Py_ssize_t len,
- const char *function_name, PyObject *args,
+ const char *function_name, PyObject *subobj,
+ Py_ssize_t start, Py_ssize_t end,
int direction)
{
- Py_ssize_t start = 0;
- Py_ssize_t end = PY_SSIZE_T_MAX;
- PyObject *subobj = NULL;
- int result;
-
- if (!stringlib_parse_args_finds(function_name, args, &subobj, &start, &end))
- return NULL;
if (PyTuple_Check(subobj)) {
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
- result = tailmatch(str, len, PyTuple_GET_ITEM(subobj, i),
- start, end, direction);
- if (result == -1)
+ PyObject *item = PyTuple_GET_ITEM(subobj, i);
+ int result = tailmatch(str, len, item, start, end, direction);
+ if (result < 0) {
return NULL;
+ }
else if (result) {
Py_RETURN_TRUE;
}
}
Py_RETURN_FALSE;
}
- result = tailmatch(str, len, subobj, start, end, direction);
+ int result = tailmatch(str, len, subobj, start, end, direction);
if (result == -1) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
+ if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Format(PyExc_TypeError,
"%s first arg must be bytes or a tuple of bytes, "
"not %s",
function_name, Py_TYPE(subobj)->tp_name);
+ }
return NULL;
}
- else
- return PyBool_FromLong(result);
+ return PyBool_FromLong(result);
}
-PyDoc_STRVAR_shared(_Py_startswith__doc__,
-"B.startswith(prefix[, start[, end]]) -> bool\n\
-\n\
-Return True if B starts with the specified prefix, False otherwise.\n\
-With optional start, test B beginning at that position.\n\
-With optional end, stop comparing B at that position.\n\
-prefix can also be a tuple of bytes to try.");
-
PyObject *
-_Py_bytes_startswith(const char *str, Py_ssize_t len, PyObject *args)
+_Py_bytes_startswith(const char *str, Py_ssize_t len, PyObject *subobj,
+ Py_ssize_t start, Py_ssize_t end)
{
- return _Py_bytes_tailmatch(str, len, "startswith", args, -1);
+ return _Py_bytes_tailmatch(str, len, "startswith", subobj, start, end, -1);
}
-PyDoc_STRVAR_shared(_Py_endswith__doc__,
-"B.endswith(suffix[, start[, end]]) -> bool\n\
-\n\
-Return True if B ends with the specified suffix, False otherwise.\n\
-With optional start, test B beginning at that position.\n\
-With optional end, stop comparing B at that position.\n\
-suffix can also be a tuple of bytes to try.");
-
PyObject *
-_Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *args)
+_Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *subobj,
+ Py_ssize_t start, Py_ssize_t end)
{
- return _Py_bytes_tailmatch(str, len, "endswith", args, +1);
+ return _Py_bytes_tailmatch(str, len, "endswith", subobj, start, end, +1);
}