Skip to content

Commit 0399727

Browse files
committed
refactor index/find with ByteInnerOptions
1 parent 56c5790 commit 0399727

File tree

2 files changed

+6
-35
lines changed

2 files changed

+6
-35
lines changed

vm/src/obj/objbyteinner.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -628,25 +628,8 @@ impl PyByteInner {
628628
Ok(vm.new_bool(suff.as_slice() == &self.elements.do_slice(range)[offset]))
629629
}
630630

631-
pub fn find(
632-
&self,
633-
sub: PyObjectRef,
634-
start: OptionalArg<PyObjectRef>,
635-
end: OptionalArg<PyObjectRef>,
636-
vm: &VirtualMachine,
637-
) -> Result<isize, PyObjectRef> {
638-
let sub = match try_as_bytes_like(&sub) {
639-
Some(value) => value,
640-
None => match_class!(sub,
641-
i @ PyInt => vec![i.as_bigint().byte_or(vm)?],
642-
obj => {return Err(vm.new_type_error(format!("a bytes-like object is required, not {}", obj)));}),
643-
};
644-
645-
let range = self.elements.get_slice_range(
646-
&is_valid_slice_arg(start, vm)?,
647-
&is_valid_slice_arg(end, vm)?,
648-
);
649-
631+
pub fn find(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
632+
let (sub, range) = options.get_value(&self.elements, vm)?;
650633
// not allowed for this method
651634
if range.end < range.start {
652635
return Ok(-1isize);

vm/src/obj/objbytes.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -270,25 +270,13 @@ impl PyBytesRef {
270270
}
271271

272272
#[pymethod(name = "find")]
273-
fn find(
274-
self,
275-
sub: PyObjectRef,
276-
start: OptionalArg<PyObjectRef>,
277-
end: OptionalArg<PyObjectRef>,
278-
vm: &VirtualMachine,
279-
) -> PyResult {
280-
Ok(vm.new_int(self.inner.find(sub, start, end, vm)?))
273+
fn find(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
274+
self.inner.find(options, vm)
281275
}
282276

283277
#[pymethod(name = "index")]
284-
fn index(
285-
self,
286-
sub: PyObjectRef,
287-
start: OptionalArg<PyObjectRef>,
288-
end: OptionalArg<PyObjectRef>,
289-
vm: &VirtualMachine,
290-
) -> PyResult {
291-
let res = self.inner.find(sub, start, end, vm)?;
278+
fn index(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult {
279+
let res = self.inner.find(options, vm)?;
292280
if res == -1 {
293281
return Err(vm.new_value_error("substring not found".to_string()));
294282
}

0 commit comments

Comments
 (0)