Extract index calculataion into functions.

This commit is contained in:
Stephen Chung
2022-01-13 18:13:27 +08:00
parent 4b4a6c944d
commit 0f4e8848f9
14 changed files with 328 additions and 558 deletions

View File

@@ -2,6 +2,7 @@
#![allow(non_snake_case)]
use crate::engine::OP_EQUALS;
use crate::eval::calc_offset_len;
use crate::plugin::*;
use crate::{
def_package, Array, Dynamic, ExclusiveRange, FnPtr, InclusiveRange, NativeCallContext,
@@ -55,23 +56,18 @@ pub mod array_functions {
array1
}
}
pub fn insert(array: &mut Array, position: INT, item: Dynamic) {
pub fn insert(array: &mut Array, index: INT, item: Dynamic) {
if array.is_empty() {
array.push(item);
} else if position < 0 {
if let Some(n) = position.checked_abs() {
if n as usize > array.len() {
array.insert(0, item);
} else {
array.insert(array.len() - n as usize, item);
}
} else {
array.insert(0, item);
}
} else if (position as usize) >= array.len() {
return;
}
let (index, _) = calc_offset_len(array.len(), index, 0);
if index >= array.len() {
array.push(item);
} else {
array.insert(position as usize, item);
array.insert(index, item);
}
}
#[rhai_fn(return_raw)]
@@ -137,11 +133,11 @@ pub mod array_functions {
array.remove(0)
}
}
pub fn remove(array: &mut Array, len: INT) -> Dynamic {
if len < 0 || (len as usize) >= array.len() {
pub fn remove(array: &mut Array, index: INT) -> Dynamic {
if index < 0 || (index as usize) >= array.len() {
Dynamic::UNIT
} else {
array.remove(len as usize)
array.remove(index as usize)
}
}
pub fn clear(array: &mut Array) {
@@ -190,27 +186,13 @@ pub mod array_functions {
return;
}
let start = if start < 0 {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
let (start, len) = calc_offset_len(array.len(), start, len);
if start >= array.len() {
array.extend(replace);
return;
} else {
start as usize
};
let len = if len < 0 {
0
} else if len as usize > array.len() - start {
array.len() - start
} else {
len as usize
};
array.splice(start..start + len, replace.into_iter());
array.splice(start..start + len, replace);
}
}
#[rhai_fn(name = "extract")]
pub fn extract_range(array: &mut Array, range: ExclusiveRange) -> Array {
@@ -229,24 +211,7 @@ pub mod array_functions {
return Array::new();
}
let start = if start < 0 {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Array::new();
} else {
start as usize
};
let len = if len <= 0 {
0
} else if len as usize > array.len() - start {
array.len() - start
} else {
len as usize
};
let (start, len) = calc_offset_len(array.len(), start, len);
if len == 0 {
Array::new()
@@ -261,20 +226,20 @@ pub mod array_functions {
#[rhai_fn(name = "split")]
pub fn split_at(array: &mut Array, start: INT) -> Array {
if array.is_empty() {
Array::new()
} else if start < 0 {
if let Some(n) = start.checked_abs() {
if n as usize > array.len() {
mem::take(array)
} else {
let mut result = Array::new();
result.extend(array.drain(array.len() - n as usize..));
result
}
} else {
return Array::new();
}
let (start, len) = calc_offset_len(array.len(), start, INT::MAX);
if start == 0 {
if len >= array.len() {
mem::take(array)
} else {
let mut result = Array::new();
result.extend(array.drain(array.len() - len..));
result
}
} else if start as usize >= array.len() {
} else if start >= array.len() {
Array::new()
} else {
let mut result = Array::new();
@@ -424,16 +389,7 @@ pub mod array_functions {
return Ok(-1);
}
let start = if start < 0 {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Ok(-1);
} else {
start as usize
};
let (start, _) = calc_offset_len(array.len(), start, 0);
for (i, item) in array.iter_mut().enumerate().skip(start) {
if ctx
@@ -489,16 +445,7 @@ pub mod array_functions {
return Ok(-1);
}
let start = if start < 0 {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Ok(-1);
} else {
start as usize
};
let (start, _) = calc_offset_len(array.len(), start, 0);
for (i, item) in array.iter().enumerate().skip(start) {
if filter
@@ -943,26 +890,13 @@ pub mod array_functions {
return Array::new();
}
let start = if start < 0 {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Array::new();
} else {
start as usize
};
let (start, len) = calc_offset_len(array.len(), start, len);
let len = if len <= 0 {
0
} else if len as usize > array.len() - start {
array.len() - start
if len == 0 {
Array::new()
} else {
len as usize
};
array.drain(start..start + len).collect()
array.drain(start..start + len).collect()
}
}
#[rhai_fn(return_raw)]
pub fn retain(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResultOf<Array> {
@@ -1033,29 +967,16 @@ pub mod array_functions {
return Array::new();
}
let start = if start < 0 {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return mem::take(array);
let (start, len) = calc_offset_len(array.len(), start, len);
if len == 0 {
Array::new()
} else {
start as usize
};
let mut drained: Array = array.drain(..start).collect();
drained.extend(array.drain(len..));
let len = if len < 0 {
0
} else if len as usize > array.len() - start {
array.len() - start
} else {
len as usize
};
let mut drained: Array = array.drain(..start).collect();
drained.extend(array.drain(len..));
drained
drained
}
}
#[rhai_fn(name = "==", return_raw, pure)]
pub fn equals(ctx: NativeCallContext, array1: &mut Array, array2: Array) -> RhaiResultOf<bool> {

View File

@@ -1,7 +1,8 @@
#![allow(non_snake_case)]
use crate::eval::calc_index;
use crate::plugin::*;
use crate::{def_package, ExclusiveRange, InclusiveRange, Position, RhaiResultOf, ERR, INT};
use crate::{def_package, ExclusiveRange, InclusiveRange, Position, RhaiResultOf, ERR, INT, UINT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -19,62 +20,27 @@ mod bit_field_functions {
const BITS: usize = std::mem::size_of::<INT>() * 8;
#[rhai_fn(return_raw)]
pub fn get_bit(value: INT, index: INT) -> RhaiResultOf<bool> {
if index >= 0 {
let offset = index as usize;
pub fn get_bit(value: INT, bit: INT) -> RhaiResultOf<bool> {
let bit = calc_index(BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE)
})?;
if offset >= BITS {
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
Ok((value & (1 << offset)) != 0)
}
} else if let Some(abs_index) = index.checked_abs() {
let offset = abs_index as usize;
// Count from end if negative
if offset > BITS {
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
Ok((value & (1 << (BITS - offset))) != 0)
}
} else {
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
}
Ok((value & (1 << bit)) != 0)
}
#[rhai_fn(return_raw)]
pub fn set_bit(value: &mut INT, index: INT, new_value: bool) -> RhaiResultOf<()> {
if index >= 0 {
let offset = index as usize;
pub fn set_bit(value: &mut INT, bit: INT, new_value: bool) -> RhaiResultOf<()> {
let bit = calc_index(BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE)
})?;
if offset >= BITS {
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
let mask = 1 << offset;
if new_value {
*value |= mask;
} else {
*value &= !mask;
}
Ok(())
}
} else if let Some(abs_index) = index.checked_abs() {
let offset = abs_index as usize;
// Count from end if negative
if offset > BITS {
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
let mask = 1 << offset;
if new_value {
*value |= mask;
} else {
*value &= !mask;
}
Ok(())
}
let mask = 1 << bit;
if new_value {
*value |= mask;
} else {
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
*value &= !mask;
}
Ok(())
}
#[rhai_fn(name = "get_bits", return_raw)]
pub fn get_bits_range(value: INT, range: ExclusiveRange) -> RhaiResultOf<INT> {
@@ -89,46 +55,29 @@ mod bit_field_functions {
get_bits(value, from, to - from + 1)
}
#[rhai_fn(return_raw)]
pub fn get_bits(value: INT, index: INT, bits: INT) -> RhaiResultOf<INT> {
if bits < 1 {
pub fn get_bits(value: INT, bit: INT, bits: INT) -> RhaiResultOf<INT> {
if bits <= 0 {
return Ok(0);
}
let offset = if index >= 0 {
let offset = index as usize;
let bit = calc_index(BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE)
})?;
if offset >= BITS {
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
offset
} else if let Some(abs_index) = index.checked_abs() {
let offset = abs_index as usize;
// Count from end if negative
if offset > BITS {
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
BITS - offset
} else {
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
};
let bits = if offset + bits as usize > BITS {
BITS - offset
let bits = if bit + bits as usize > BITS {
BITS - bit
} else {
bits as usize
};
let mut base = 1;
let mut mask = 0;
for _ in 0..bits {
mask |= base;
base <<= 1;
if bit == 0 && bits == BITS {
return Ok(value);
}
Ok(((value & (mask << index)) >> index) & mask)
// 2^bits - 1
let mask = ((2 as UINT).pow(bits as u32) - 1) as crate::INT;
Ok(((value & (mask << bit)) >> bit) & mask)
}
#[rhai_fn(name = "set_bits", return_raw)]
pub fn set_bits_range(
@@ -151,47 +100,31 @@ mod bit_field_functions {
set_bits(value, from, to - from + 1, new_value)
}
#[rhai_fn(return_raw)]
pub fn set_bits(value: &mut INT, index: INT, bits: INT, new_value: INT) -> RhaiResultOf<()> {
if bits < 1 {
pub fn set_bits(value: &mut INT, bit: INT, bits: INT, new_value: INT) -> RhaiResultOf<()> {
if bits <= 0 {
return Ok(());
}
let offset = if index >= 0 {
let offset = index as usize;
let bit = calc_index(BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE)
})?;
if offset >= BITS {
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
offset
} else if let Some(abs_index) = index.checked_abs() {
let offset = abs_index as usize;
// Count from end if negative
if offset > BITS {
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
BITS - offset
} else {
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
};
let bits = if offset + bits as usize > BITS {
BITS - offset
let bits = if bit + bits as usize > BITS {
BITS - bit
} else {
bits as usize
};
let mut base = 1;
let mut mask = 0;
for _ in 0..bits {
mask |= base;
base <<= 1;
if bit == 0 && bits == BITS {
*value = new_value;
return Ok(());
}
*value &= !(mask << index);
*value |= (new_value & mask) << index;
// 2^bits - 1
let mask = ((2 as UINT).pow(bits as u32) - 1) as crate::INT;
*value &= !(mask << bit);
*value |= (new_value & mask) << bit;
Ok(())
}

View File

@@ -1,6 +1,7 @@
#![cfg(not(feature = "no_index"))]
#![allow(non_snake_case)]
use crate::eval::calc_offset_len;
use crate::plugin::*;
use crate::{
def_package, Blob, Dynamic, ExclusiveRange, InclusiveRange, NativeCallContext, Position,
@@ -86,25 +87,20 @@ pub mod blob_functions {
blob1
}
}
pub fn insert(blob: &mut Blob, position: INT, item: INT) {
pub fn insert(blob: &mut Blob, index: INT, item: INT) {
let item = (item & 0x000000ff) as u8;
if blob.is_empty() {
blob.push(item);
} else if position < 0 {
if let Some(n) = position.checked_abs() {
if n as usize > blob.len() {
blob.insert(0, item);
} else {
blob.insert(blob.len() - n as usize, item);
}
} else {
blob.insert(0, item);
}
} else if (position as usize) >= blob.len() {
return;
}
let (index, _) = calc_offset_len(blob.len(), index, 0);
if index >= blob.len() {
blob.push(item);
} else {
blob.insert(position as usize, item);
blob.insert(index, item);
}
}
#[rhai_fn(return_raw)]
@@ -196,28 +192,14 @@ pub mod blob_functions {
*blob = replace;
return;
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
blob.extend(replace);
return;
} else {
start as usize
};
let len = if len < 0 {
0
} else if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
blob.splice(start..start + len, replace.into_iter());
blob.splice(start..start + len, replace);
}
}
#[rhai_fn(name = "extract")]
pub fn extract_range(blob: &mut Blob, range: ExclusiveRange) -> Blob {
@@ -235,25 +217,14 @@ pub mod blob_functions {
if blob.is_empty() || len <= 0 {
return Blob::new();
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return Blob::new();
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
Blob::new()
} else {
start as usize
};
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
blob[start..start + len].to_vec()
blob[start..start + len].to_vec()
}
}
#[rhai_fn(name = "extract")]
pub fn extract_tail(blob: &mut Blob, start: INT) -> Blob {
@@ -262,20 +233,20 @@ pub mod blob_functions {
#[rhai_fn(name = "split")]
pub fn split_at(blob: &mut Blob, start: INT) -> Blob {
if blob.is_empty() {
Blob::new()
} else if start < 0 {
if let Some(n) = start.checked_abs() {
if n as usize > blob.len() {
mem::take(blob)
} else {
let mut result = Blob::new();
result.extend(blob.drain(blob.len() - n as usize..));
result
}
} else {
return Blob::new();
}
let (start, len) = calc_offset_len(blob.len(), start, INT::MAX);
if start == 0 {
if len > blob.len() {
mem::take(blob)
} else {
let mut result = Blob::new();
result.extend(blob.drain(blob.len() - len..));
result
}
} else if start as usize >= blob.len() {
} else if start >= blob.len() {
Blob::new()
} else {
let mut result = Blob::new();
@@ -299,25 +270,14 @@ pub mod blob_functions {
if blob.is_empty() || len <= 0 {
return Blob::new();
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return Blob::new();
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
Blob::new()
} else {
start as usize
};
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
blob.drain(start..start + len).collect()
blob.drain(start..start + len).collect()
}
}
#[rhai_fn(name = "retain")]
pub fn retain_range(blob: &mut Blob, range: ExclusiveRange) -> Blob {
@@ -335,28 +295,17 @@ pub mod blob_functions {
if blob.is_empty() || len <= 0 {
return Blob::new();
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return mem::take(blob);
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
mem::take(blob)
} else {
start as usize
};
let mut drained: Blob = blob.drain(..start).collect();
drained.extend(blob.drain(len..));
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
let mut drained: Blob = blob.drain(..start).collect();
drained.extend(blob.drain(len..));
drained
drained
}
}
#[inline]
@@ -364,23 +313,11 @@ pub mod blob_functions {
if blob.is_empty() || len <= 0 {
return 0;
}
let blob_len = blob.len();
let (start, len) = calc_offset_len(blob.len(), start, len);
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
if len == 0 {
return 0;
} else {
start as usize
};
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
}
const INT_BYTES: usize = mem::size_of::<INT>();
@@ -433,23 +370,12 @@ pub mod blob_functions {
if blob.is_empty() || len <= 0 {
return;
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
return;
} else {
start as usize
};
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
}
const INT_BYTES: usize = mem::size_of::<INT>();
@@ -502,23 +428,12 @@ pub mod blob_functions {
if blob.is_empty() || len <= 0 {
return 0.0;
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
return 0.0;
} else {
start as usize
};
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
}
const FLOAT_BYTES: usize = mem::size_of::<FLOAT>();
@@ -578,23 +493,12 @@ pub mod blob_functions {
if blob.is_empty() || len <= 0 {
return;
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
return;
} else {
start as usize
};
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
}
const FLOAT_BYTES: usize = mem::size_of::<FLOAT>();
@@ -650,23 +554,12 @@ pub mod blob_functions {
if len <= 0 || blob.is_empty() || string.is_empty() {
return;
}
let blob_len = blob.len();
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
let (start, len) = calc_offset_len(blob.len(), start, len);
if len == 0 {
return;
} else {
start as usize
};
let len = if len as usize > blob_len - start {
blob_len - start
} else {
len as usize
};
}
let len = usize::min(len, string.len());

View File

@@ -1,3 +1,4 @@
use crate::eval::calc_index;
use crate::plugin::*;
use crate::types::dynamic::Variant;
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT};
@@ -120,30 +121,9 @@ const BITS: usize = std::mem::size_of::<INT>() * 8;
impl BitRange {
pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> {
let from = if from >= 0 {
let offset = from as usize;
#[cfg(not(feature = "unchecked"))]
if offset >= BITS {
return Err(crate::ERR::ErrorBitFieldBounds(BITS, from, Position::NONE).into());
}
offset
} else {
#[cfg(not(feature = "unchecked"))]
if let Some(abs_from) = from.checked_abs() {
if (abs_from as usize) > BITS {
return Err(crate::ERR::ErrorBitFieldBounds(BITS, from, Position::NONE).into());
}
BITS - (abs_from as usize)
} else {
return Err(crate::ERR::ErrorBitFieldBounds(BITS, from, Position::NONE).into());
}
#[cfg(feature = "unchecked")]
{
BITS - (from.abs() as usize)
}
};
let from = calc_index(BITS, from, true, || {
crate::ERR::ErrorBitFieldBounds(BITS, from, Position::NONE)
})?;
let len = if len < 0 {
0

View File

@@ -77,8 +77,9 @@ mod map_functions {
for (m1, v1) in map1.iter_mut() {
if let Some(v2) = map2.get_mut(m1) {
let equals = ctx
.call_fn_raw(OP_EQUALS, true, false, &mut [v1, v2])
.map(|v| v.as_bool().unwrap_or(false))?;
.call_fn_raw(OP_EQUALS, true, false, &mut [v1, v2])?
.as_bool()
.unwrap_or(false);
if !equals {
return Ok(false);

View File

@@ -1,7 +1,7 @@
#![allow(non_snake_case)]
use crate::plugin::*;
use crate::{def_package, Position, RhaiResultOf, ERR, INT, UNSIGNED_INT};
use crate::{def_package, Position, RhaiResultOf, ERR, INT, UINT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -118,7 +118,7 @@ mod int_functions {
.into());
}
UNSIGNED_INT::from_str_radix(string.trim(), radix as u32)
UINT::from_str_radix(string.trim(), radix as u32)
.map(|v| v as INT)
.map_err(|err| {
ERR::ErrorArithmetic(