summaryrefslogtreecommitdiffstats
path: root/Objects/bytearrayobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r--Objects/bytearrayobject.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index b271e57..5a803be 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -1181,6 +1181,71 @@ bytearray_endswith(PyByteArrayObject *self, PyObject *args)
return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
}
+/*[clinic input]
+bytearray.removeprefix as bytearray_removeprefix
+
+ prefix: Py_buffer
+ /
+
+Return a bytearray with the given prefix string removed if present.
+
+If the bytearray starts with the prefix string, return
+bytearray[len(prefix):]. Otherwise, return a copy of the original
+bytearray.
+[clinic start generated code]*/
+
+static PyObject *
+bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix)
+/*[clinic end generated code: output=6cabc585e7f502e0 input=968aada38aedd262]*/
+{
+ const char *self_start = PyByteArray_AS_STRING(self);
+ Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
+ const char *prefix_start = prefix->buf;
+ Py_ssize_t prefix_len = prefix->len;
+
+ if (self_len >= prefix_len
+ && memcmp(self_start, prefix_start, prefix_len) == 0)
+ {
+ return PyByteArray_FromStringAndSize(self_start + prefix_len,
+ self_len - prefix_len);
+ }
+
+ return PyByteArray_FromStringAndSize(self_start, self_len);
+}
+
+/*[clinic input]
+bytearray.removesuffix as bytearray_removesuffix
+
+ suffix: Py_buffer
+ /
+
+Return a bytearray with the given suffix string removed if present.
+
+If the bytearray ends with the suffix string and that suffix is not
+empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of
+the original bytearray.
+[clinic start generated code]*/
+
+static PyObject *
+bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix)
+/*[clinic end generated code: output=2bc8cfb79de793d3 input=c1827e810b2f6b99]*/
+{
+ const char *self_start = PyByteArray_AS_STRING(self);
+ Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
+ const char *suffix_start = suffix->buf;
+ Py_ssize_t suffix_len = suffix->len;
+
+ if (self_len >= suffix_len
+ && memcmp(self_start + self_len - suffix_len,
+ suffix_start, suffix_len) == 0)
+ {
+ return PyByteArray_FromStringAndSize(self_start,
+ self_len - suffix_len);
+ }
+
+ return PyByteArray_FromStringAndSize(self_start, self_len);
+}
+
/*[clinic input]
bytearray.translate
@@ -2203,6 +2268,8 @@ bytearray_methods[] = {
BYTEARRAY_POP_METHODDEF
BYTEARRAY_REMOVE_METHODDEF
BYTEARRAY_REPLACE_METHODDEF
+ BYTEARRAY_REMOVEPREFIX_METHODDEF
+ BYTEARRAY_REMOVESUFFIX_METHODDEF
BYTEARRAY_REVERSE_METHODDEF
{"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
{"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},