File tree Expand file tree Collapse file tree 4 files changed +21
-4
lines changed Expand file tree Collapse file tree 4 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -195,10 +195,11 @@ pub trait AnyStr<'s>: 's {
195
195
F : Fn ( & Self ) -> PyObjectRef ;
196
196
197
197
#[ inline]
198
- fn py_startsendswith < T , F > (
198
+ fn py_startsendswith < T , F , FS > (
199
199
& self ,
200
200
affix : PyObjectRef ,
201
201
range : std:: ops:: Range < usize > ,
202
+ slicer : FS ,
202
203
func_name : & str ,
203
204
py_type_name : & str ,
204
205
func : F ,
@@ -207,11 +208,12 @@ pub trait AnyStr<'s>: 's {
207
208
where
208
209
T : TryFromObject ,
209
210
F : Fn ( & Self , & T ) -> bool ,
211
+ FS : Fn ( & Self , std:: ops:: Range < usize > ) -> & Self ,
210
212
{
211
213
if !range. is_normal ( ) {
212
214
return Ok ( false ) ;
213
215
}
214
- let value = self . get_chars ( range) ;
216
+ let value = slicer ( & self , range) ;
215
217
216
218
single_or_tuple_any (
217
219
affix,
Original file line number Diff line number Diff line change @@ -439,6 +439,7 @@ impl PyByteArray {
439
439
self . borrow_buf ( ) . py_startsendswith (
440
440
affix,
441
441
range,
442
+ AnyStr :: get_bytes,
442
443
"endswith" ,
443
444
"bytes" ,
444
445
|s, x : & PyBytesInner | s. ends_with ( & x. elements [ ..] ) ,
@@ -456,6 +457,7 @@ impl PyByteArray {
456
457
self . borrow_buf ( ) . py_startsendswith (
457
458
affix,
458
459
range,
460
+ AnyStr :: get_bytes,
459
461
"startswith" ,
460
462
"bytes" ,
461
463
|s, x : & PyBytesInner | s. starts_with ( & x. elements [ ..] ) ,
Original file line number Diff line number Diff line change @@ -279,6 +279,7 @@ impl PyBytes {
279
279
self . inner . elements [ ..] . py_startsendswith (
280
280
affix,
281
281
range,
282
+ AnyStr :: get_bytes,
282
283
"endswith" ,
283
284
"bytes" ,
284
285
|s, x : & PyBytesInner | s. ends_with ( & x. elements [ ..] ) ,
@@ -293,10 +294,10 @@ impl PyBytes {
293
294
vm : & VirtualMachine ,
294
295
) -> PyResult < bool > {
295
296
let ( affix, range) = options. get_value ( self . len ( ) ) ;
296
-
297
297
self . inner . elements [ ..] . py_startsendswith (
298
298
affix,
299
299
range,
300
+ AnyStr :: get_bytes,
300
301
"startswith" ,
301
302
"bytes" ,
302
303
|s, x : & PyBytesInner | s. starts_with ( & x. elements [ ..] ) ,
Original file line number Diff line number Diff line change @@ -706,6 +706,7 @@ impl PyStr {
706
706
707
707
#[ pymethod]
708
708
fn endswith ( & self , args : anystr:: StartsEndsWithArgs , vm : & VirtualMachine ) -> PyResult < bool > {
709
+ let has_subrange = args. has_subrange ( ) ;
709
710
let len = if args. has_subrange ( ) {
710
711
self . char_len ( )
711
712
} else {
@@ -715,6 +716,11 @@ impl PyStr {
715
716
self . as_str ( ) . py_startsendswith (
716
717
affix,
717
718
range,
719
+ if has_subrange {
720
+ str:: get_chars
721
+ } else {
722
+ str:: get_bytes
723
+ } ,
718
724
"endswith" ,
719
725
"str" ,
720
726
|s, x : & PyStrRef | s. ends_with ( x. as_str ( ) ) ,
@@ -724,7 +730,8 @@ impl PyStr {
724
730
725
731
#[ pymethod]
726
732
fn startswith ( & self , args : anystr:: StartsEndsWithArgs , vm : & VirtualMachine ) -> PyResult < bool > {
727
- let len = if args. has_subrange ( ) {
733
+ let has_subrange = args. has_subrange ( ) ;
734
+ let len = if has_subrange {
728
735
self . char_len ( )
729
736
} else {
730
737
self . byte_len ( )
@@ -733,6 +740,11 @@ impl PyStr {
733
740
self . as_str ( ) . py_startsendswith (
734
741
affix,
735
742
range,
743
+ if has_subrange {
744
+ str:: get_chars
745
+ } else {
746
+ str:: get_bytes
747
+ } ,
736
748
"startswith" ,
737
749
"str" ,
738
750
|s, x : & PyStrRef | s. starts_with ( x. as_str ( ) ) ,
You can’t perform that action at this time.
0 commit comments