From 66b63956fd0ea927632221bf03823f68524a90d3 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 27 Apr 2020 01:03:50 +0900 Subject: [PATCH] cleanup startsendswith args with FromArgs --- vm/src/obj/objbytearray.rs | 20 ++++---------------- vm/src/obj/objbytes.rs | 20 ++++---------------- vm/src/obj/objstr.rs | 24 ++++-------------------- vm/src/obj/pystr.rs | 24 +++++++++++++++++++----- 4 files changed, 31 insertions(+), 57 deletions(-) diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index bfead7864f..1c7d523162 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -300,17 +300,9 @@ impl PyByteArray { } #[pymethod(name = "endswith")] - fn endswith( - &self, - suffix: PyObjectRef, - start: OptionalArg>, - end: OptionalArg>, - vm: &VirtualMachine, - ) -> PyResult { + fn endswith(&self, options: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult { self.borrow_value().elements[..].py_startsendswith( - suffix, - start, - end, + options, "endswith", "bytes", |s, x: &PyByteInner| s.ends_with(&x.elements[..]), @@ -321,15 +313,11 @@ impl PyByteArray { #[pymethod(name = "startswith")] fn startswith( &self, - prefix: PyObjectRef, - start: OptionalArg>, - end: OptionalArg>, + options: pystr::StartsEndsWithArgs, vm: &VirtualMachine, ) -> PyResult { self.borrow_value().elements[..].py_startsendswith( - prefix, - start, - end, + options, "startswith", "bytes", |s, x: &PyByteInner| s.starts_with(&x.elements[..]), diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs index cca75d6123..83a26d557d 100644 --- a/vm/src/obj/objbytes.rs +++ b/vm/src/obj/objbytes.rs @@ -273,17 +273,9 @@ impl PyBytes { } #[pymethod(name = "endswith")] - fn endswith( - &self, - suffix: PyObjectRef, - start: OptionalArg>, - end: OptionalArg>, - vm: &VirtualMachine, - ) -> PyResult { + fn endswith(&self, options: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult { self.inner.elements[..].py_startsendswith( - suffix, - start, - end, + options, "endswith", "bytes", |s, x: &PyByteInner| s.ends_with(&x.elements[..]), @@ -294,15 +286,11 @@ impl PyBytes { #[pymethod(name = "startswith")] fn startswith( &self, - prefix: PyObjectRef, - start: OptionalArg>, - end: OptionalArg>, + options: pystr::StartsEndsWithArgs, vm: &VirtualMachine, ) -> PyResult { self.inner.elements[..].py_startsendswith( - prefix, - start, - end, + options, "startswith", "bytes", |s, x: &PyByteInner| s.starts_with(&x.elements[..]), diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 9638838bfd..9366737972 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -507,17 +507,9 @@ impl PyString { } #[pymethod] - fn endswith( - &self, - suffix: PyObjectRef, - start: OptionalArg>, - end: OptionalArg>, - vm: &VirtualMachine, - ) -> PyResult { + fn endswith(&self, args: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult { self.value.as_str().py_startsendswith( - suffix, - start, - end, + args, "endswith", "str", |s, x: &PyStringRef| s.ends_with(x.as_str()), @@ -526,17 +518,9 @@ impl PyString { } #[pymethod] - fn startswith( - &self, - prefix: PyObjectRef, - start: OptionalArg>, - end: OptionalArg>, - vm: &VirtualMachine, - ) -> PyResult { + fn startswith(&self, args: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult { self.value.as_str().py_startsendswith( - prefix, - start, - end, + args, "startswith", "str", |s, x: &PyStringRef| s.starts_with(x.as_str()), diff --git a/vm/src/obj/pystr.rs b/vm/src/obj/pystr.rs index 7af1d93832..c0c0d113e2 100644 --- a/vm/src/obj/pystr.rs +++ b/vm/src/obj/pystr.rs @@ -54,6 +54,23 @@ impl ExpandTabsArgs { } } +#[derive(FromArgs)] +pub struct StartsEndsWithArgs { + #[pyarg(positional_only, optional = false)] + affix: PyObjectRef, + #[pyarg(positional_only, optional = true)] + start: OptionalOption, + #[pyarg(positional_only, optional = true)] + end: OptionalOption, +} + +impl StartsEndsWithArgs { + fn get_value(self, len: usize) -> (PyObjectRef, std::ops::Range) { + let range = adjust_indices(self.start, self.end, len); + (self.affix, range) + } +} + // help get optional string indices pub fn adjust_indices( start: OptionalOption, @@ -134,13 +151,10 @@ pub trait PyCommonString { where F: Fn(&Self) -> PyObjectRef; - #[allow(clippy::too_many_arguments)] #[inline] fn py_startsendswith( &self, - affix: PyObjectRef, - start: OptionalOption, - end: OptionalOption, + args: StartsEndsWithArgs, func_name: &str, py_type_name: &str, func: F, @@ -150,7 +164,7 @@ pub trait PyCommonString { T: TryFromObject, F: Fn(&Self, &T) -> bool, { - let range = adjust_indices(start, end, self.len()); + let (affix, range) = args.get_value(self.len()); if range.is_normal() { let value = self.get_slice(range); single_or_tuple_any(