Skip to content

Commit 355cbaa

Browse files
authored
gh-91266: refactor bytearray strip methods (GH-32096)
1 parent 325d6f5 commit 355cbaa

File tree

2 files changed

+41
-85
lines changed

2 files changed

+41
-85
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor the ``bytearray`` strip methods ``strip``, ``lstrip`` and ``rstrip`` to use a common implementation.

Objects/bytearrayobject.c

Lines changed: 40 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,26 +1845,46 @@ bytearray_remove_impl(PyByteArrayObject *self, int value)
18451845
Py_RETURN_NONE;
18461846
}
18471847

1848-
/* XXX These two helpers could be optimized if argsize == 1 */
1848+
#define LEFTSTRIP 0
1849+
#define RIGHTSTRIP 1
1850+
#define BOTHSTRIP 2
18491851

1850-
static Py_ssize_t
1851-
lstrip_helper(const char *myptr, Py_ssize_t mysize,
1852-
const void *argptr, Py_ssize_t argsize)
1852+
static PyObject*
1853+
bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int striptype)
18531854
{
1854-
Py_ssize_t i = 0;
1855-
while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
1856-
i++;
1857-
return i;
1858-
}
1855+
Py_ssize_t mysize, byteslen;
1856+
const char* myptr;
1857+
const char* bytesptr;
1858+
Py_buffer vbytes;
18591859

1860-
static Py_ssize_t
1861-
rstrip_helper(const char *myptr, Py_ssize_t mysize,
1862-
const void *argptr, Py_ssize_t argsize)
1863-
{
1864-
Py_ssize_t i = mysize - 1;
1865-
while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
1866-
i--;
1867-
return i + 1;
1860+
if (bytes == Py_None) {
1861+
bytesptr = "\t\n\r\f\v ";
1862+
byteslen = 6;
1863+
}
1864+
else {
1865+
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1866+
return NULL;
1867+
bytesptr = (const char*)vbytes.buf;
1868+
byteslen = vbytes.len;
1869+
}
1870+
myptr = PyByteArray_AS_STRING(self);
1871+
mysize = Py_SIZE(self);
1872+
1873+
Py_ssize_t left = 0;
1874+
if (striptype != RIGHTSTRIP) {
1875+
while (left < mysize && memchr(bytesptr, (unsigned char)myptr[left], byteslen))
1876+
left++;
1877+
}
1878+
Py_ssize_t right = mysize;
1879+
if (striptype != LEFTSTRIP) {
1880+
do {
1881+
right--;
1882+
} while (right >= left && memchr(bytesptr, (unsigned char)myptr[right], byteslen));
1883+
right++;
1884+
}
1885+
if (bytes != Py_None)
1886+
PyBuffer_Release(&vbytes);
1887+
return PyByteArray_FromStringAndSize(myptr + left, right - left);
18681888
}
18691889

18701890
/*[clinic input]
@@ -1882,31 +1902,7 @@ static PyObject *
18821902
bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
18831903
/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
18841904
{
1885-
Py_ssize_t left, right, mysize, byteslen;
1886-
char *myptr;
1887-
const char *bytesptr;
1888-
Py_buffer vbytes;
1889-
1890-
if (bytes == Py_None) {
1891-
bytesptr = "\t\n\r\f\v ";
1892-
byteslen = 6;
1893-
}
1894-
else {
1895-
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1896-
return NULL;
1897-
bytesptr = (const char *) vbytes.buf;
1898-
byteslen = vbytes.len;
1899-
}
1900-
myptr = PyByteArray_AS_STRING(self);
1901-
mysize = Py_SIZE(self);
1902-
left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
1903-
if (left == mysize)
1904-
right = left;
1905-
else
1906-
right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1907-
if (bytes != Py_None)
1908-
PyBuffer_Release(&vbytes);
1909-
return PyByteArray_FromStringAndSize(myptr + left, right - left);
1905+
return bytearray_strip_impl_helper(self, bytes, BOTHSTRIP);
19101906
}
19111907

19121908
/*[clinic input]
@@ -1924,28 +1920,7 @@ static PyObject *
19241920
bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
19251921
/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
19261922
{
1927-
Py_ssize_t left, right, mysize, byteslen;
1928-
char *myptr;
1929-
const char *bytesptr;
1930-
Py_buffer vbytes;
1931-
1932-
if (bytes == Py_None) {
1933-
bytesptr = "\t\n\r\f\v ";
1934-
byteslen = 6;
1935-
}
1936-
else {
1937-
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1938-
return NULL;
1939-
bytesptr = (const char *) vbytes.buf;
1940-
byteslen = vbytes.len;
1941-
}
1942-
myptr = PyByteArray_AS_STRING(self);
1943-
mysize = Py_SIZE(self);
1944-
left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
1945-
right = mysize;
1946-
if (bytes != Py_None)
1947-
PyBuffer_Release(&vbytes);
1948-
return PyByteArray_FromStringAndSize(myptr + left, right - left);
1923+
return bytearray_strip_impl_helper(self, bytes, LEFTSTRIP);
19491924
}
19501925

19511926
/*[clinic input]
@@ -1963,27 +1938,7 @@ static PyObject *
19631938
bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
19641939
/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
19651940
{
1966-
Py_ssize_t right, mysize, byteslen;
1967-
char *myptr;
1968-
const char *bytesptr;
1969-
Py_buffer vbytes;
1970-
1971-
if (bytes == Py_None) {
1972-
bytesptr = "\t\n\r\f\v ";
1973-
byteslen = 6;
1974-
}
1975-
else {
1976-
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1977-
return NULL;
1978-
bytesptr = (const char *) vbytes.buf;
1979-
byteslen = vbytes.len;
1980-
}
1981-
myptr = PyByteArray_AS_STRING(self);
1982-
mysize = Py_SIZE(self);
1983-
right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1984-
if (bytes != Py_None)
1985-
PyBuffer_Release(&vbytes);
1986-
return PyByteArray_FromStringAndSize(myptr, right);
1941+
return bytearray_strip_impl_helper(self, bytes, RIGHTSTRIP);
19871942
}
19881943

19891944
/*[clinic input]

0 commit comments

Comments
 (0)