@@ -389,24 +389,38 @@ impl PyByteArray {
389
389
self . borrow_value ( ) . rstrip ( chars) . into ( )
390
390
}
391
391
392
+ /// removeprefix($self, prefix, /)
393
+ ///
394
+ ///
395
+ /// Return a bytearray object with the given prefix string removed if present.
396
+ ///
397
+ /// If the bytearray starts with the prefix string, return string[len(prefix):]
398
+ /// Otherwise, return a copy of the original bytearray.
392
399
#[ pymethod( name = "removeprefix" ) ]
393
400
fn removeprefix ( & self , prefix : PyByteInner ) -> PyByteArray {
394
- let value = self . borrow_value ( ) ;
395
- if value. elements . starts_with ( & prefix. elements ) {
396
- return value. elements [ prefix. elements . len ( ) ..] . to_vec ( ) . into ( ) ;
397
- }
398
- value. elements . to_vec ( ) . into ( )
401
+ self . borrow_value ( ) . elements [ ..]
402
+ . py_removeprefix ( & prefix. elements , prefix. elements . len ( ) , |s, p| {
403
+ s. starts_with ( p)
404
+ } )
405
+ . to_vec ( )
406
+ . into ( )
399
407
}
400
408
409
+ /// removesuffix(self, prefix, /)
410
+ ///
411
+ ///
412
+ /// Return a bytearray object with the given suffix string removed if present.
413
+ ///
414
+ /// If the bytearray ends with the suffix string, return string[:len(suffix)]
415
+ /// Otherwise, return a copy of the original bytearray.
401
416
#[ pymethod( name = "removesuffix" ) ]
402
417
fn removesuffix ( & self , suffix : PyByteInner ) -> PyByteArray {
403
- let value = self . borrow_value ( ) ;
404
- if value. elements . ends_with ( & suffix. elements ) {
405
- return value. elements [ ..value. elements . len ( ) - suffix. elements . len ( ) ]
406
- . to_vec ( )
407
- . into ( ) ;
408
- }
409
- value. elements . to_vec ( ) . into ( )
418
+ self . borrow_value ( ) . elements [ ..]
419
+ . py_removesuffix ( & suffix. elements , suffix. elements . len ( ) , |s, p| {
420
+ s. ends_with ( p)
421
+ } )
422
+ . to_vec ( )
423
+ . into ( )
410
424
}
411
425
412
426
#[ pymethod( name = "split" ) ]
0 commit comments