Skip to content

Commit ad111b0

Browse files
authored
Merge pull request RustPython#1885 from youknowone/startsendswith
cleanup startsendswith args with FromArgs
2 parents eaa839b + 66b6395 commit ad111b0

File tree

4 files changed

+31
-57
lines changed

4 files changed

+31
-57
lines changed

vm/src/obj/objbytearray.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,9 @@ impl PyByteArray {
300300
}
301301

302302
#[pymethod(name = "endswith")]
303-
fn endswith(
304-
&self,
305-
suffix: PyObjectRef,
306-
start: OptionalArg<Option<isize>>,
307-
end: OptionalArg<Option<isize>>,
308-
vm: &VirtualMachine,
309-
) -> PyResult<bool> {
303+
fn endswith(&self, options: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
310304
self.borrow_value().elements[..].py_startsendswith(
311-
suffix,
312-
start,
313-
end,
305+
options,
314306
"endswith",
315307
"bytes",
316308
|s, x: &PyByteInner| s.ends_with(&x.elements[..]),
@@ -321,15 +313,11 @@ impl PyByteArray {
321313
#[pymethod(name = "startswith")]
322314
fn startswith(
323315
&self,
324-
prefix: PyObjectRef,
325-
start: OptionalArg<Option<isize>>,
326-
end: OptionalArg<Option<isize>>,
316+
options: pystr::StartsEndsWithArgs,
327317
vm: &VirtualMachine,
328318
) -> PyResult<bool> {
329319
self.borrow_value().elements[..].py_startsendswith(
330-
prefix,
331-
start,
332-
end,
320+
options,
333321
"startswith",
334322
"bytes",
335323
|s, x: &PyByteInner| s.starts_with(&x.elements[..]),

vm/src/obj/objbytes.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,9 @@ impl PyBytes {
273273
}
274274

275275
#[pymethod(name = "endswith")]
276-
fn endswith(
277-
&self,
278-
suffix: PyObjectRef,
279-
start: OptionalArg<Option<isize>>,
280-
end: OptionalArg<Option<isize>>,
281-
vm: &VirtualMachine,
282-
) -> PyResult<bool> {
276+
fn endswith(&self, options: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
283277
self.inner.elements[..].py_startsendswith(
284-
suffix,
285-
start,
286-
end,
278+
options,
287279
"endswith",
288280
"bytes",
289281
|s, x: &PyByteInner| s.ends_with(&x.elements[..]),
@@ -294,15 +286,11 @@ impl PyBytes {
294286
#[pymethod(name = "startswith")]
295287
fn startswith(
296288
&self,
297-
prefix: PyObjectRef,
298-
start: OptionalArg<Option<isize>>,
299-
end: OptionalArg<Option<isize>>,
289+
options: pystr::StartsEndsWithArgs,
300290
vm: &VirtualMachine,
301291
) -> PyResult<bool> {
302292
self.inner.elements[..].py_startsendswith(
303-
prefix,
304-
start,
305-
end,
293+
options,
306294
"startswith",
307295
"bytes",
308296
|s, x: &PyByteInner| s.starts_with(&x.elements[..]),

vm/src/obj/objstr.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -507,17 +507,9 @@ impl PyString {
507507
}
508508

509509
#[pymethod]
510-
fn endswith(
511-
&self,
512-
suffix: PyObjectRef,
513-
start: OptionalArg<Option<isize>>,
514-
end: OptionalArg<Option<isize>>,
515-
vm: &VirtualMachine,
516-
) -> PyResult<bool> {
510+
fn endswith(&self, args: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
517511
self.value.as_str().py_startsendswith(
518-
suffix,
519-
start,
520-
end,
512+
args,
521513
"endswith",
522514
"str",
523515
|s, x: &PyStringRef| s.ends_with(x.as_str()),
@@ -526,17 +518,9 @@ impl PyString {
526518
}
527519

528520
#[pymethod]
529-
fn startswith(
530-
&self,
531-
prefix: PyObjectRef,
532-
start: OptionalArg<Option<isize>>,
533-
end: OptionalArg<Option<isize>>,
534-
vm: &VirtualMachine,
535-
) -> PyResult<bool> {
521+
fn startswith(&self, args: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
536522
self.value.as_str().py_startsendswith(
537-
prefix,
538-
start,
539-
end,
523+
args,
540524
"startswith",
541525
"str",
542526
|s, x: &PyStringRef| s.starts_with(x.as_str()),

vm/src/obj/pystr.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ impl ExpandTabsArgs {
5454
}
5555
}
5656

57+
#[derive(FromArgs)]
58+
pub struct StartsEndsWithArgs {
59+
#[pyarg(positional_only, optional = false)]
60+
affix: PyObjectRef,
61+
#[pyarg(positional_only, optional = true)]
62+
start: OptionalOption<isize>,
63+
#[pyarg(positional_only, optional = true)]
64+
end: OptionalOption<isize>,
65+
}
66+
67+
impl StartsEndsWithArgs {
68+
fn get_value(self, len: usize) -> (PyObjectRef, std::ops::Range<usize>) {
69+
let range = adjust_indices(self.start, self.end, len);
70+
(self.affix, range)
71+
}
72+
}
73+
5774
// help get optional string indices
5875
pub fn adjust_indices(
5976
start: OptionalOption<isize>,
@@ -134,13 +151,10 @@ pub trait PyCommonString<E> {
134151
where
135152
F: Fn(&Self) -> PyObjectRef;
136153

137-
#[allow(clippy::too_many_arguments)]
138154
#[inline]
139155
fn py_startsendswith<T, F>(
140156
&self,
141-
affix: PyObjectRef,
142-
start: OptionalOption<isize>,
143-
end: OptionalOption<isize>,
157+
args: StartsEndsWithArgs,
144158
func_name: &str,
145159
py_type_name: &str,
146160
func: F,
@@ -150,7 +164,7 @@ pub trait PyCommonString<E> {
150164
T: TryFromObject,
151165
F: Fn(&Self, &T) -> bool,
152166
{
153-
let range = adjust_indices(start, end, self.len());
167+
let (affix, range) = args.get_value(self.len());
154168
if range.is_normal() {
155169
let value = self.get_slice(range);
156170
single_or_tuple_any(

0 commit comments

Comments
 (0)