Skip to content

Commit 1bc8e01

Browse files
morealSnowapril
authored andcommitted
Pass calculated range instead args
1 parent 44bacb1 commit 1bc8e01

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

vm/src/anystr.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ pub struct StartsEndsWithArgs {
6161
}
6262

6363
impl StartsEndsWithArgs {
64-
fn get_value(self, len: usize) -> (PyObjectRef, std::ops::Range<usize>) {
64+
pub fn get_value(self, len: usize) -> (PyObjectRef, std::ops::Range<usize>) {
6565
let range = adjust_indices(self.start, self.end, len);
6666
(self.affix, range)
6767
}
6868

6969
pub fn has_subrange(&self) -> bool {
70-
self.start.is_none() && self.end.is_none()
70+
self.start.is_some() || self.end.is_some()
7171
}
7272
}
7373

@@ -197,8 +197,8 @@ pub trait AnyStr<'s>: 's {
197197
#[inline]
198198
fn py_startsendswith<T, F>(
199199
&self,
200-
args: StartsEndsWithArgs,
201-
len: usize,
200+
affix: PyObjectRef,
201+
range: std::ops::Range<usize>,
202202
func_name: &str,
203203
py_type_name: &str,
204204
func: F,
@@ -208,17 +208,10 @@ pub trait AnyStr<'s>: 's {
208208
T: TryFromObject,
209209
F: Fn(&Self, &T) -> bool,
210210
{
211-
let (affix, value) = if args.has_subrange() {
212-
// If it doesn't have subrange, it uses bytes operation.
213-
(args.affix, self.get_bytes(0..len))
214-
} else {
215-
let (affix, range) = args.get_value(len);
216-
if !range.is_normal() {
217-
return Ok(false);
218-
}
219-
220-
(affix, self.get_chars(range))
221-
};
211+
if !range.is_normal() {
212+
return Ok(false);
213+
}
214+
let value = self.get_chars(range);
222215

223216
single_or_tuple_any(
224217
affix,

vm/src/builtins/bytearray.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,10 @@ impl PyByteArray {
435435

436436
#[pymethod]
437437
fn endswith(&self, options: anystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
438+
let (affix, range) = options.get_value(self.len());
438439
self.borrow_buf().py_startsendswith(
439-
options,
440-
self.len(),
440+
affix,
441+
range,
441442
"endswith",
442443
"bytes",
443444
|s, x: &PyBytesInner| s.ends_with(&x.elements[..]),
@@ -451,9 +452,10 @@ impl PyByteArray {
451452
options: anystr::StartsEndsWithArgs,
452453
vm: &VirtualMachine,
453454
) -> PyResult<bool> {
455+
let (affix, range) = options.get_value(self.len());
454456
self.borrow_buf().py_startsendswith(
455-
options,
456-
self.len(),
457+
affix,
458+
range,
457459
"startswith",
458460
"bytes",
459461
|s, x: &PyBytesInner| s.starts_with(&x.elements[..]),

vm/src/builtins/bytes.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ impl PyBytes {
275275

276276
#[pymethod]
277277
fn endswith(&self, options: anystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
278+
let (affix, range) = options.get_value(self.len());
278279
self.inner.elements[..].py_startsendswith(
279-
options,
280-
self.len(),
280+
affix,
281+
range,
281282
"endswith",
282283
"bytes",
283284
|s, x: &PyBytesInner| s.ends_with(&x.elements[..]),
@@ -291,9 +292,11 @@ impl PyBytes {
291292
options: anystr::StartsEndsWithArgs,
292293
vm: &VirtualMachine,
293294
) -> PyResult<bool> {
295+
let (affix, range) = options.get_value(self.len());
296+
294297
self.inner.elements[..].py_startsendswith(
295-
options,
296-
self.len(),
298+
affix,
299+
range,
297300
"startswith",
298301
"bytes",
299302
|s, x: &PyBytesInner| s.starts_with(&x.elements[..]),

vm/src/builtins/pystr.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,14 @@ impl PyStr {
707707
#[pymethod]
708708
fn endswith(&self, args: anystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
709709
let len = if args.has_subrange() {
710-
self.byte_len()
711-
} else {
712710
self.char_len()
711+
} else {
712+
self.byte_len()
713713
};
714-
714+
let (affix, range) = args.get_value(len);
715715
self.as_str().py_startsendswith(
716-
args,
717-
len,
716+
affix,
717+
range,
718718
"endswith",
719719
"str",
720720
|s, x: &PyStrRef| s.ends_with(x.as_str()),
@@ -725,14 +725,14 @@ impl PyStr {
725725
#[pymethod]
726726
fn startswith(&self, args: anystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
727727
let len = if args.has_subrange() {
728-
self.byte_len()
729-
} else {
730728
self.char_len()
729+
} else {
730+
self.byte_len()
731731
};
732-
732+
let (affix, range) = args.get_value(len);
733733
self.as_str().py_startsendswith(
734-
args,
735-
len,
734+
affix,
735+
range,
736736
"startswith",
737737
"str",
738738
|s, x: &PyStrRef| s.starts_with(x.as_str()),

0 commit comments

Comments
 (0)