Use isize directly

This commit is contained in:
Aviv Palivoda
2019-10-17 13:06:14 +03:00
parent be4a3f3299
commit 70a22ab196
4 changed files with 16 additions and 33 deletions

View File

@@ -2,8 +2,6 @@
use std::cell::{Cell, RefCell};
use std::convert::TryFrom;
use num_traits::ToPrimitive;
use super::objbyteinner::{
ByteInnerExpandtabsOptions, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
ByteInnerPosition, ByteInnerSplitOptions, ByteInnerSplitlinesOptions,
@@ -506,18 +504,13 @@ impl PyByteArrayRef {
}
#[pymethod(name = "insert")]
fn insert(self, index: PyIntRef, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
fn insert(self, mut index: isize, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
let bytes = &mut self.inner.borrow_mut().elements;
let len = isize::try_from(bytes.len())
.map_err(|_e| vm.new_overflow_error("bytearray too big".to_string()))?;
let x = x.as_bigint().byte_or(vm)?;
let mut index = index
.as_bigint()
.to_isize()
.ok_or_else(|| vm.new_overflow_error("index too big".to_string()))?;
if index >= len {
bytes.push(x);
return Ok(());
@@ -550,17 +543,17 @@ impl PyByteArrayRef {
}
#[pymethod(name = "__mul__")]
fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().repeat(n, vm)?))
}
#[pymethod(name = "__rmul__")]
fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult {
self.repeat(n, vm)
}
#[pymethod(name = "__imul__")]
fn irepeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
fn irepeat(self, n: isize, vm: &VirtualMachine) -> PyResult<()> {
self.inner.borrow_mut().irepeat(n, vm)
}

View File

@@ -1073,16 +1073,12 @@ impl PyByteInner {
res
}
pub fn repeat(&self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
pub fn repeat(&self, n: isize, _vm: &VirtualMachine) -> PyResult<Vec<u8>> {
if self.elements.is_empty() {
// We can multiple an empty vector by any integer, even if it doesn't fit in an isize.
return Ok(vec![]);
}
let n = n.as_bigint().to_isize().ok_or_else(|| {
vm.new_overflow_error("can't multiply bytes that many times".to_string())
})?;
if n <= 0 {
Ok(vec![])
} else {
@@ -1097,16 +1093,12 @@ impl PyByteInner {
}
}
pub fn irepeat(&mut self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
pub fn irepeat(&mut self, n: isize, _vm: &VirtualMachine) -> PyResult<()> {
if self.elements.is_empty() {
// We can multiple an empty vector by any integer, even if it doesn't fit in an isize.
return Ok(());
}
let n = n.as_bigint().to_isize().ok_or_else(|| {
vm.new_overflow_error("can't multiply bytes that many times".to_string())
})?;
if n <= 0 {
self.elements.clear();
} else {

View File

@@ -421,12 +421,12 @@ impl PyBytesRef {
}
#[pymethod(name = "__mul__")]
fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytes(self.inner.repeat(n, vm)?))
}
#[pymethod(name = "__rmul__")]
fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult {
self.repeat(n, vm)
}

View File

@@ -292,14 +292,10 @@ impl PyString {
}
#[pymethod(name = "__mul__")]
fn mul(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
if !objtype::isinstance(&val, &vm.ctx.int_type()) {
return Err(vm.new_type_error(format!("Cannot multiply {} and {}", self, val)));
}
objint::get_value(&val)
.to_isize()
.map(|multiplier| multiplier.max(0))
.and_then(|multiplier| multiplier.to_usize())
fn mul(&self, multiplier: isize, vm: &VirtualMachine) -> PyResult<String> {
multiplier
.max(0)
.to_usize()
.map(|multiplier| self.value.repeat(multiplier))
.ok_or_else(|| {
vm.new_overflow_error("cannot fit 'int' into an index-sized integer".to_string())
@@ -307,7 +303,7 @@ impl PyString {
}
#[pymethod(name = "__rmul__")]
fn rmul(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
fn rmul(&self, val: isize, vm: &VirtualMachine) -> PyResult<String> {
self.mul(val, vm)
}
@@ -1358,7 +1354,9 @@ fn try_update_quantity_from_tuple(
Ok(tuple_index)
}
}
None => Err(vm.new_type_error("not enough arguments for format string".to_string())),
None => {
Err(vm.new_type_error("not enough arguments for format string".to_string()))
}
}
}
_ => Ok(tuple_index),