Skip to content

Commit c646019

Browse files
morealSnowapril
authored andcommitted
Pass slicer manually
1 parent 1bc8e01 commit c646019

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

vm/src/anystr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ pub trait AnyStr<'s>: 's {
195195
F: Fn(&Self) -> PyObjectRef;
196196

197197
#[inline]
198-
fn py_startsendswith<T, F>(
198+
fn py_startsendswith<T, F, FS>(
199199
&self,
200200
affix: PyObjectRef,
201201
range: std::ops::Range<usize>,
202+
slicer: FS,
202203
func_name: &str,
203204
py_type_name: &str,
204205
func: F,
@@ -207,11 +208,12 @@ pub trait AnyStr<'s>: 's {
207208
where
208209
T: TryFromObject,
209210
F: Fn(&Self, &T) -> bool,
211+
FS: Fn(&Self, std::ops::Range<usize>) -> &Self,
210212
{
211213
if !range.is_normal() {
212214
return Ok(false);
213215
}
214-
let value = self.get_chars(range);
216+
let value = slicer(&self, range);
215217

216218
single_or_tuple_any(
217219
affix,

vm/src/builtins/bytearray.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ impl PyByteArray {
439439
self.borrow_buf().py_startsendswith(
440440
affix,
441441
range,
442+
AnyStr::get_bytes,
442443
"endswith",
443444
"bytes",
444445
|s, x: &PyBytesInner| s.ends_with(&x.elements[..]),
@@ -456,6 +457,7 @@ impl PyByteArray {
456457
self.borrow_buf().py_startsendswith(
457458
affix,
458459
range,
460+
AnyStr::get_bytes,
459461
"startswith",
460462
"bytes",
461463
|s, x: &PyBytesInner| s.starts_with(&x.elements[..]),

vm/src/builtins/bytes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ impl PyBytes {
279279
self.inner.elements[..].py_startsendswith(
280280
affix,
281281
range,
282+
AnyStr::get_bytes,
282283
"endswith",
283284
"bytes",
284285
|s, x: &PyBytesInner| s.ends_with(&x.elements[..]),
@@ -293,10 +294,10 @@ impl PyBytes {
293294
vm: &VirtualMachine,
294295
) -> PyResult<bool> {
295296
let (affix, range) = options.get_value(self.len());
296-
297297
self.inner.elements[..].py_startsendswith(
298298
affix,
299299
range,
300+
AnyStr::get_bytes,
300301
"startswith",
301302
"bytes",
302303
|s, x: &PyBytesInner| s.starts_with(&x.elements[..]),

vm/src/builtins/pystr.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ impl PyStr {
706706

707707
#[pymethod]
708708
fn endswith(&self, args: anystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
709+
let has_subrange = args.has_subrange();
709710
let len = if args.has_subrange() {
710711
self.char_len()
711712
} else {
@@ -715,6 +716,11 @@ impl PyStr {
715716
self.as_str().py_startsendswith(
716717
affix,
717718
range,
719+
if has_subrange {
720+
str::get_chars
721+
} else {
722+
str::get_bytes
723+
},
718724
"endswith",
719725
"str",
720726
|s, x: &PyStrRef| s.ends_with(x.as_str()),
@@ -724,7 +730,8 @@ impl PyStr {
724730

725731
#[pymethod]
726732
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 {
728735
self.char_len()
729736
} else {
730737
self.byte_len()
@@ -733,6 +740,11 @@ impl PyStr {
733740
self.as_str().py_startsendswith(
734741
affix,
735742
range,
743+
if has_subrange {
744+
str::get_chars
745+
} else {
746+
str::get_bytes
747+
},
736748
"startswith",
737749
"str",
738750
|s, x: &PyStrRef| s.starts_with(x.as_str()),

0 commit comments

Comments
 (0)