Merge pull request #1885 from youknowone/startsendswith

cleanup startsendswith args with FromArgs
This commit is contained in:
Jeong YunWon
2020-04-27 11:11:07 +09:00
committed by GitHub
4 changed files with 31 additions and 57 deletions

View File

@@ -300,17 +300,9 @@ impl PyByteArray {
}
#[pymethod(name = "endswith")]
fn endswith(
&self,
suffix: PyObjectRef,
start: OptionalArg<Option<isize>>,
end: OptionalArg<Option<isize>>,
vm: &VirtualMachine,
) -> PyResult<bool> {
fn endswith(&self, options: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
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<Option<isize>>,
end: OptionalArg<Option<isize>>,
options: pystr::StartsEndsWithArgs,
vm: &VirtualMachine,
) -> PyResult<bool> {
self.borrow_value().elements[..].py_startsendswith(
prefix,
start,
end,
options,
"startswith",
"bytes",
|s, x: &PyByteInner| s.starts_with(&x.elements[..]),

View File

@@ -273,17 +273,9 @@ impl PyBytes {
}
#[pymethod(name = "endswith")]
fn endswith(
&self,
suffix: PyObjectRef,
start: OptionalArg<Option<isize>>,
end: OptionalArg<Option<isize>>,
vm: &VirtualMachine,
) -> PyResult<bool> {
fn endswith(&self, options: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
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<Option<isize>>,
end: OptionalArg<Option<isize>>,
options: pystr::StartsEndsWithArgs,
vm: &VirtualMachine,
) -> PyResult<bool> {
self.inner.elements[..].py_startsendswith(
prefix,
start,
end,
options,
"startswith",
"bytes",
|s, x: &PyByteInner| s.starts_with(&x.elements[..]),

View File

@@ -507,17 +507,9 @@ impl PyString {
}
#[pymethod]
fn endswith(
&self,
suffix: PyObjectRef,
start: OptionalArg<Option<isize>>,
end: OptionalArg<Option<isize>>,
vm: &VirtualMachine,
) -> PyResult<bool> {
fn endswith(&self, args: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
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<Option<isize>>,
end: OptionalArg<Option<isize>>,
vm: &VirtualMachine,
) -> PyResult<bool> {
fn startswith(&self, args: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
self.value.as_str().py_startsendswith(
prefix,
start,
end,
args,
"startswith",
"str",
|s, x: &PyStringRef| s.starts_with(x.as_str()),

View File

@@ -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<isize>,
#[pyarg(positional_only, optional = true)]
end: OptionalOption<isize>,
}
impl StartsEndsWithArgs {
fn get_value(self, len: usize) -> (PyObjectRef, std::ops::Range<usize>) {
let range = adjust_indices(self.start, self.end, len);
(self.affix, range)
}
}
// help get optional string indices
pub fn adjust_indices(
start: OptionalOption<isize>,
@@ -134,13 +151,10 @@ pub trait PyCommonString<E> {
where
F: Fn(&Self) -> PyObjectRef;
#[allow(clippy::too_many_arguments)]
#[inline]
fn py_startsendswith<T, F>(
&self,
affix: PyObjectRef,
start: OptionalOption<isize>,
end: OptionalOption<isize>,
args: StartsEndsWithArgs,
func_name: &str,
py_type_name: &str,
func: F,
@@ -150,7 +164,7 @@ pub trait PyCommonString<E> {
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(