@@ -1845,26 +1845,46 @@ bytearray_remove_impl(PyByteArrayObject *self, int value)
1845
1845
Py_RETURN_NONE ;
1846
1846
}
1847
1847
1848
- /* XXX These two helpers could be optimized if argsize == 1 */
1848
+ #define LEFTSTRIP 0
1849
+ #define RIGHTSTRIP 1
1850
+ #define BOTHSTRIP 2
1849
1851
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 )
1853
1854
{
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 ;
1859
1859
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 );
1868
1888
}
1869
1889
1870
1890
/*[clinic input]
@@ -1882,31 +1902,7 @@ static PyObject *
1882
1902
bytearray_strip_impl (PyByteArrayObject * self , PyObject * bytes )
1883
1903
/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
1884
1904
{
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 );
1910
1906
}
1911
1907
1912
1908
/*[clinic input]
@@ -1924,28 +1920,7 @@ static PyObject *
1924
1920
bytearray_lstrip_impl (PyByteArrayObject * self , PyObject * bytes )
1925
1921
/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
1926
1922
{
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 );
1949
1924
}
1950
1925
1951
1926
/*[clinic input]
@@ -1963,27 +1938,7 @@ static PyObject *
1963
1938
bytearray_rstrip_impl (PyByteArrayObject * self , PyObject * bytes )
1964
1939
/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
1965
1940
{
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 );
1987
1942
}
1988
1943
1989
1944
/*[clinic input]
0 commit comments