2020-05-06 13:45:17 +02:00
|
|
|
#![cfg(not(feature = "no_index"))]
|
2020-08-14 12:58:34 +02:00
|
|
|
#![allow(non_snake_case)]
|
2020-05-06 13:45:17 +02:00
|
|
|
|
2021-03-23 05:13:53 +01:00
|
|
|
use crate::engine::OP_EQUALS;
|
2020-08-14 12:58:34 +02:00
|
|
|
use crate::plugin::*;
|
2021-02-23 09:06:36 +01:00
|
|
|
use crate::{def_package, Array, Dynamic, EvalAltResult, FnPtr, NativeCallContext, Position, INT};
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
use std::{any::TypeId, cmp::Ordering, mem};
|
2020-08-14 12:58:34 +02:00
|
|
|
|
|
|
|
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
2020-09-21 10:15:52 +02:00
|
|
|
combine_with_exported_module!(lib, "array", array_functions);
|
|
|
|
|
2020-08-14 12:58:34 +02:00
|
|
|
// Register array iterator
|
2020-10-15 16:11:18 +02:00
|
|
|
lib.set_iterable::<Array>();
|
2020-08-14 12:58:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
mod array_functions {
|
2021-02-19 16:13:53 +01:00
|
|
|
#[rhai_fn(name = "len", get = "len", pure)]
|
|
|
|
pub fn len(array: &mut Array) -> INT {
|
|
|
|
array.len() as INT
|
2020-08-14 12:58:34 +02:00
|
|
|
}
|
2021-02-23 09:06:36 +01:00
|
|
|
#[rhai_fn(name = "push", name = "+=")]
|
|
|
|
pub fn push(array: &mut Array, item: Dynamic) {
|
|
|
|
array.push(item);
|
|
|
|
}
|
2020-09-08 12:01:34 +02:00
|
|
|
#[rhai_fn(name = "append", name = "+=")]
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn append(array: &mut Array, y: Array) {
|
|
|
|
array.extend(y);
|
2020-08-14 12:58:34 +02:00
|
|
|
}
|
2020-08-16 17:41:59 +02:00
|
|
|
#[rhai_fn(name = "+")]
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn concat(mut array: Array, y: Array) -> Array {
|
|
|
|
array.extend(y);
|
|
|
|
array
|
2020-08-16 17:41:59 +02:00
|
|
|
}
|
2021-02-23 09:06:36 +01:00
|
|
|
pub fn insert(array: &mut Array, position: INT, item: Dynamic) {
|
2021-04-19 09:11:03 +02:00
|
|
|
if position < 0 {
|
2021-04-10 09:00:03 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-02-23 09:06:36 +01:00
|
|
|
} else if (position as usize) >= array.len() {
|
|
|
|
push(array, item);
|
|
|
|
} else {
|
|
|
|
array.insert(position as usize, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[rhai_fn(return_raw)]
|
|
|
|
pub fn pad(
|
|
|
|
_ctx: NativeCallContext,
|
|
|
|
array: &mut Array,
|
|
|
|
len: INT,
|
|
|
|
item: Dynamic,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<(), Box<EvalAltResult>> {
|
2021-02-23 09:06:36 +01:00
|
|
|
// Check if array will be over max size limit
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if _ctx.engine().max_array_size() > 0
|
|
|
|
&& len > 0
|
|
|
|
&& (len as usize) > _ctx.engine().max_array_size()
|
|
|
|
{
|
|
|
|
return EvalAltResult::ErrorDataTooLarge("Size of array".to_string(), Position::NONE)
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
if len > 0 && len as usize > array.len() {
|
|
|
|
array.resize(len as usize, item);
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(())
|
2021-02-23 09:06:36 +01:00
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn pop(array: &mut Array) -> Dynamic {
|
|
|
|
array.pop().unwrap_or_else(|| ().into())
|
2020-08-14 12:58:34 +02:00
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn shift(array: &mut Array) -> Dynamic {
|
|
|
|
if array.is_empty() {
|
2020-08-14 12:58:34 +02:00
|
|
|
().into()
|
|
|
|
} else {
|
2021-02-19 16:13:53 +01:00
|
|
|
array.remove(0)
|
2020-08-14 12:58:34 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn remove(array: &mut Array, len: INT) -> Dynamic {
|
|
|
|
if len < 0 || (len as usize) >= array.len() {
|
2020-08-14 12:58:34 +02:00
|
|
|
().into()
|
|
|
|
} else {
|
2021-02-19 16:13:53 +01:00
|
|
|
array.remove(len as usize)
|
2020-08-14 12:58:34 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn clear(array: &mut Array) {
|
|
|
|
array.clear();
|
2020-08-14 12:58:34 +02:00
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn truncate(array: &mut Array, len: INT) {
|
2020-08-14 12:58:34 +02:00
|
|
|
if len >= 0 {
|
2021-02-19 16:13:53 +01:00
|
|
|
array.truncate(len as usize);
|
2020-08-14 12:58:34 +02:00
|
|
|
} else {
|
2021-02-19 16:13:53 +01:00
|
|
|
array.clear();
|
2020-08-14 12:58:34 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn chop(array: &mut Array, len: INT) {
|
|
|
|
if len as usize >= array.len() {
|
2020-10-13 08:39:49 +02:00
|
|
|
} else if len >= 0 {
|
2021-02-19 16:13:53 +01:00
|
|
|
array.drain(0..array.len() - len as usize);
|
2020-10-13 08:39:49 +02:00
|
|
|
} else {
|
2021-02-19 16:13:53 +01:00
|
|
|
array.clear();
|
2020-10-13 08:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn reverse(array: &mut Array) {
|
|
|
|
array.reverse();
|
2020-09-24 04:51:21 +02:00
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn splice(array: &mut Array, start: INT, len: INT, replace: Array) {
|
2020-10-13 04:57:29 +02:00
|
|
|
let start = if start < 0 {
|
2021-04-10 09:00:03 +02:00
|
|
|
let arr_len = array.len();
|
|
|
|
start
|
|
|
|
.checked_abs()
|
|
|
|
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if start as usize >= array.len() {
|
2021-04-10 09:00:03 +02:00
|
|
|
array.extend(replace.into_iter());
|
|
|
|
return;
|
2020-10-13 04:57:29 +02:00
|
|
|
} else {
|
|
|
|
start as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
let len = if len < 0 {
|
|
|
|
0
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if len as usize > array.len() - start {
|
|
|
|
array.len() - start
|
2020-10-13 04:57:29 +02:00
|
|
|
} else {
|
|
|
|
len as usize
|
|
|
|
};
|
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
array.splice(start..start + len, replace.into_iter());
|
2020-10-13 04:57:29 +02:00
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn extract(array: &mut Array, start: INT, len: INT) -> Array {
|
2020-10-13 08:39:49 +02:00
|
|
|
let start = if start < 0 {
|
2021-04-10 09:00:03 +02:00
|
|
|
let arr_len = array.len();
|
|
|
|
start
|
|
|
|
.checked_abs()
|
|
|
|
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if start as usize >= array.len() {
|
2021-04-10 09:00:03 +02:00
|
|
|
return Default::default();
|
2020-10-13 08:39:49 +02:00
|
|
|
} else {
|
|
|
|
start as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
let len = if len < 0 {
|
|
|
|
0
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if len as usize > array.len() - start {
|
|
|
|
array.len() - start
|
2020-10-13 08:39:49 +02:00
|
|
|
} else {
|
|
|
|
len as usize
|
|
|
|
};
|
|
|
|
|
2021-07-24 08:11:16 +02:00
|
|
|
array[start..start + len].to_vec()
|
2020-10-13 08:39:49 +02:00
|
|
|
}
|
|
|
|
#[rhai_fn(name = "extract")]
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn extract_tail(array: &mut Array, start: INT) -> Array {
|
2020-10-13 08:39:49 +02:00
|
|
|
let start = if start < 0 {
|
2021-04-10 09:00:03 +02:00
|
|
|
let arr_len = array.len();
|
|
|
|
start
|
|
|
|
.checked_abs()
|
|
|
|
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if start as usize >= array.len() {
|
2021-04-10 09:00:03 +02:00
|
|
|
return Default::default();
|
2020-10-13 08:39:49 +02:00
|
|
|
} else {
|
|
|
|
start as usize
|
|
|
|
};
|
|
|
|
|
2021-07-24 08:11:16 +02:00
|
|
|
array[start..].to_vec()
|
2020-10-13 08:39:49 +02:00
|
|
|
}
|
2021-02-23 04:57:50 +01:00
|
|
|
#[rhai_fn(name = "split")]
|
|
|
|
pub fn split_at(array: &mut Array, start: INT) -> Array {
|
2021-04-19 09:11:03 +02:00
|
|
|
if start < 0 {
|
2021-04-10 09:00:03 +02:00
|
|
|
if let Some(n) = start.checked_abs() {
|
|
|
|
if n as usize > array.len() {
|
|
|
|
mem::take(array)
|
|
|
|
} else {
|
|
|
|
let mut result: Array = Default::default();
|
|
|
|
result.extend(array.drain(array.len() - n as usize..));
|
|
|
|
result
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mem::take(array)
|
|
|
|
}
|
2021-02-23 04:57:50 +01:00
|
|
|
} else if start as usize >= array.len() {
|
|
|
|
Default::default()
|
|
|
|
} else {
|
|
|
|
let mut result: Array = Default::default();
|
|
|
|
result.extend(array.drain(start as usize..));
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn map(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
mapper: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<Array, Box<EvalAltResult>> {
|
2021-03-23 05:13:53 +01:00
|
|
|
let mut ar = Array::with_capacity(array.len());
|
2020-10-18 15:47:34 +02:00
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate() {
|
|
|
|
ar.push(
|
2020-10-18 15:47:34 +02:00
|
|
|
mapper
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [item.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(mapper.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
mapper.call_dynamic(&ctx, None, [item.clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
|
|
|
"map".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?,
|
|
|
|
);
|
|
|
|
}
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(ar)
|
2020-10-18 07:18:12 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn filter(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
filter: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<Array, Box<EvalAltResult>> {
|
2021-03-23 05:13:53 +01:00
|
|
|
let mut ar = Array::new();
|
2020-10-18 15:47:34 +02:00
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate() {
|
2020-10-18 15:47:34 +02:00
|
|
|
if filter
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [item.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(filter.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
filter.call_dynamic(&ctx, None, [item.clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
|
|
|
"filter".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-02-19 16:13:53 +01:00
|
|
|
ar.push(item.clone());
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-14 12:58:34 +02:00
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(ar)
|
2020-06-15 16:04:14 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(return_raw, pure)]
|
|
|
|
pub fn contains(
|
|
|
|
ctx: NativeCallContext,
|
|
|
|
array: &mut Array,
|
2021-03-09 06:44:54 +01:00
|
|
|
value: Dynamic,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<bool, Box<EvalAltResult>> {
|
2021-03-09 06:44:54 +01:00
|
|
|
for item in array.iter_mut() {
|
2021-03-09 06:16:05 +01:00
|
|
|
if ctx
|
2021-03-09 06:44:54 +01:00
|
|
|
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [item, &mut value.clone()])
|
2021-03-09 06:16:05 +01:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
|
|
|
|
if fn_sig.starts_with(OP_EQUALS) =>
|
|
|
|
{
|
|
|
|
if item.type_id() == value.type_id() {
|
|
|
|
// No default when comparing same type
|
|
|
|
Err(err)
|
|
|
|
} else {
|
|
|
|
Ok(Dynamic::FALSE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(true);
|
2021-03-09 06:16:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(false)
|
2021-03-09 06:16:05 +01:00
|
|
|
}
|
|
|
|
#[rhai_fn(return_raw, pure)]
|
2020-11-08 16:00:37 +01:00
|
|
|
pub fn index_of(
|
2021-03-09 06:16:05 +01:00
|
|
|
ctx: NativeCallContext,
|
|
|
|
array: &mut Array,
|
2021-03-09 06:44:54 +01:00
|
|
|
value: Dynamic,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<INT, Box<EvalAltResult>> {
|
2021-04-10 09:00:03 +02:00
|
|
|
index_of_starting_from(ctx, array, value, 0)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "index_of", return_raw, pure)]
|
|
|
|
pub fn index_of_starting_from(
|
|
|
|
ctx: NativeCallContext,
|
|
|
|
array: &mut Array,
|
|
|
|
value: Dynamic,
|
|
|
|
start: INT,
|
|
|
|
) -> Result<INT, Box<EvalAltResult>> {
|
|
|
|
let start = if start < 0 {
|
|
|
|
let arr_len = array.len();
|
|
|
|
start
|
|
|
|
.checked_abs()
|
|
|
|
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
|
|
|
|
} else if start as usize >= array.len() {
|
|
|
|
return Ok(-1);
|
|
|
|
} else {
|
|
|
|
start as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i, item) in array.iter_mut().enumerate().skip(start) {
|
2021-03-09 06:16:05 +01:00
|
|
|
if ctx
|
2021-03-09 06:44:54 +01:00
|
|
|
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [item, &mut value.clone()])
|
2021-03-09 06:16:05 +01:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
|
|
|
|
if fn_sig.starts_with(OP_EQUALS) =>
|
|
|
|
{
|
|
|
|
if item.type_id() == value.type_id() {
|
|
|
|
// No default when comparing same type
|
|
|
|
Err(err)
|
|
|
|
} else {
|
|
|
|
Ok(Dynamic::FALSE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(i as INT);
|
2021-03-09 06:16:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(-1 as INT)
|
2021-03-09 06:16:05 +01:00
|
|
|
}
|
|
|
|
#[rhai_fn(name = "index_of", return_raw, pure)]
|
|
|
|
pub fn index_of_filter(
|
2020-11-08 16:00:37 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-11-08 16:00:37 +01:00
|
|
|
filter: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<INT, Box<EvalAltResult>> {
|
2021-04-10 09:00:03 +02:00
|
|
|
index_of_filter_starting_from(ctx, array, filter, 0)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "index_of", return_raw, pure)]
|
|
|
|
pub fn index_of_filter_starting_from(
|
|
|
|
ctx: NativeCallContext,
|
|
|
|
array: &mut Array,
|
|
|
|
filter: FnPtr,
|
|
|
|
start: INT,
|
|
|
|
) -> Result<INT, Box<EvalAltResult>> {
|
|
|
|
let start = if start < 0 {
|
|
|
|
let arr_len = array.len();
|
|
|
|
start
|
|
|
|
.checked_abs()
|
|
|
|
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
|
|
|
|
} else if start as usize >= array.len() {
|
|
|
|
return Ok(-1);
|
|
|
|
} else {
|
|
|
|
start as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i, item) in array.iter().enumerate().skip(start) {
|
2020-11-08 16:00:37 +01:00
|
|
|
if filter
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [item.clone()])
|
2020-11-08 16:00:37 +01:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(filter.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
filter.call_dynamic(&ctx, None, [item.clone(), (i as INT).into()])
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
2020-11-11 06:25:45 +01:00
|
|
|
"index_of".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-11-08 16:00:37 +01:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-11-08 16:00:37 +01:00
|
|
|
))
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(i as INT);
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(-1 as INT)
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn some(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
filter: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<bool, Box<EvalAltResult>> {
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate() {
|
2020-10-18 15:47:34 +02:00
|
|
|
if filter
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [item.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(filter.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
filter.call_dynamic(&ctx, None, [item.clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
2020-11-11 06:25:45 +01:00
|
|
|
"some".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(true);
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
2020-06-30 12:34:32 +02:00
|
|
|
}
|
2020-10-12 16:49:51 +02:00
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(false)
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn all(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
filter: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<bool, Box<EvalAltResult>> {
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate() {
|
2020-10-18 15:47:34 +02:00
|
|
|
if !filter
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [item.clone()])
|
2020-10-12 16:49:51 +02:00
|
|
|
.or_else(|err| match *err {
|
2020-10-18 15:47:34 +02:00
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(filter.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
filter.call_dynamic(&ctx, None, [item.clone(), (i as INT).into()])
|
2020-10-12 16:49:51 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
2020-11-11 06:25:45 +01:00
|
|
|
"all".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-12 16:49:51 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-12 16:49:51 +02:00
|
|
|
))
|
2020-10-18 15:47:34 +02:00
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(false);
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
2020-10-12 16:49:51 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(true)
|
2020-10-13 04:57:29 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn reduce(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
reducer: FnPtr,
|
|
|
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
2021-03-22 04:18:09 +01:00
|
|
|
let mut result = Dynamic::UNIT;
|
2020-10-18 15:47:34 +02:00
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate() {
|
2020-10-18 15:47:34 +02:00
|
|
|
result = reducer
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [result.clone(), item.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(reducer.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
reducer.call_dynamic(&ctx, None, [result, item.clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
|
|
|
"reduce".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?;
|
2020-10-13 04:57:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
Ok(result)
|
2020-10-12 16:49:51 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(name = "reduce", return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn reduce_with_initial(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
reducer: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
initial: Dynamic,
|
2020-10-18 15:47:34 +02:00
|
|
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
2021-03-22 04:18:09 +01:00
|
|
|
let mut result = initial;
|
2020-10-18 15:47:34 +02:00
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate() {
|
2020-10-18 15:47:34 +02:00
|
|
|
result = reducer
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [result.clone(), item.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(reducer.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
reducer.call_dynamic(&ctx, None, [result, item.clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
|
|
|
"reduce".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?;
|
|
|
|
}
|
2020-10-12 16:49:51 +02:00
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
Ok(result)
|
2020-10-14 15:27:31 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn reduce_rev(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
reducer: FnPtr,
|
|
|
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
2021-03-22 04:18:09 +01:00
|
|
|
let mut result = Dynamic::UNIT;
|
2020-10-18 15:47:34 +02:00
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate().rev() {
|
2020-10-18 15:47:34 +02:00
|
|
|
result = reducer
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [result.clone(), item.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(reducer.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
reducer.call_dynamic(&ctx, None, [result, item.clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
2020-11-11 06:25:45 +01:00
|
|
|
"reduce_rev".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?;
|
|
|
|
}
|
2020-10-14 15:27:31 +02:00
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
Ok(result)
|
2020-10-14 15:27:31 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(name = "reduce_rev", return_raw, pure)]
|
2020-10-18 15:47:34 +02:00
|
|
|
pub fn reduce_rev_with_initial(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
reducer: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
initial: Dynamic,
|
2020-10-18 15:47:34 +02:00
|
|
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
2021-03-22 04:18:09 +01:00
|
|
|
let mut result = initial;
|
2020-10-18 15:47:34 +02:00
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
for (i, item) in array.iter().enumerate().rev() {
|
2020-10-18 15:47:34 +02:00
|
|
|
result = reducer
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [result.clone(), item.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(reducer.fn_name()) =>
|
|
|
|
{
|
2021-03-15 04:36:30 +01:00
|
|
|
reducer.call_dynamic(&ctx, None, [result, item.clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
2020-11-11 06:25:45 +01:00
|
|
|
"reduce_rev".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?;
|
|
|
|
}
|
2020-10-14 15:27:31 +02:00
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
Ok(result)
|
2020-10-13 04:57:29 +02:00
|
|
|
}
|
2020-10-18 15:47:34 +02:00
|
|
|
#[rhai_fn(return_raw)]
|
|
|
|
pub fn sort(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
comparer: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<(), Box<EvalAltResult>> {
|
2021-02-19 16:13:53 +01:00
|
|
|
array.sort_by(|x, y| {
|
2020-10-18 15:47:34 +02:00
|
|
|
comparer
|
2021-03-15 04:36:30 +01:00
|
|
|
.call_dynamic(&ctx, None, [x.clone(), y.clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.ok()
|
|
|
|
.and_then(|v| v.as_int().ok())
|
2021-05-25 04:54:48 +02:00
|
|
|
.map(|v| match v {
|
|
|
|
v if v > 0 => Ordering::Greater,
|
|
|
|
v if v < 0 => Ordering::Less,
|
|
|
|
0 => Ordering::Equal,
|
|
|
|
_ => unreachable!(),
|
2020-10-18 15:47:34 +02:00
|
|
|
})
|
2021-05-25 04:54:48 +02:00
|
|
|
.unwrap_or_else(|| x.type_id().cmp(&y.type_id()))
|
2020-10-18 15:47:34 +02:00
|
|
|
});
|
2020-10-13 04:57:29 +02:00
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(())
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
#[rhai_fn(return_raw)]
|
|
|
|
pub fn drain(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
filter: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<Array, Box<EvalAltResult>> {
|
2021-03-23 05:13:53 +01:00
|
|
|
let mut drained = Array::with_capacity(array.len());
|
2020-10-13 04:57:29 +02:00
|
|
|
|
2021-04-10 09:00:03 +02:00
|
|
|
let mut i = 0;
|
|
|
|
let mut x = 0;
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2021-04-10 09:00:03 +02:00
|
|
|
while x < array.len() {
|
2020-10-18 15:47:34 +02:00
|
|
|
if filter
|
2021-04-10 09:00:03 +02:00
|
|
|
.call_dynamic(&ctx, None, [array[x].clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(filter.fn_name()) =>
|
|
|
|
{
|
2021-04-10 09:00:03 +02:00
|
|
|
filter.call_dynamic(&ctx, None, [array[x].clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
2020-11-11 06:25:45 +01:00
|
|
|
"drain".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-04-10 09:00:03 +02:00
|
|
|
drained.push(array.remove(x));
|
|
|
|
} else {
|
|
|
|
x += 1;
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
2021-04-10 09:00:03 +02:00
|
|
|
|
|
|
|
i += 1;
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(drained)
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
#[rhai_fn(name = "drain")]
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn drain_range(array: &mut Array, start: INT, len: INT) -> Array {
|
2020-10-18 15:47:34 +02:00
|
|
|
let start = if start < 0 {
|
2021-04-10 09:00:03 +02:00
|
|
|
let arr_len = array.len();
|
|
|
|
start
|
|
|
|
.checked_abs()
|
|
|
|
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if start as usize >= array.len() {
|
2021-04-10 09:00:03 +02:00
|
|
|
return Default::default();
|
2020-10-18 15:47:34 +02:00
|
|
|
} else {
|
|
|
|
start as usize
|
|
|
|
};
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
let len = if len < 0 {
|
|
|
|
0
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if len as usize > array.len() - start {
|
|
|
|
array.len() - start
|
2020-10-18 15:47:34 +02:00
|
|
|
} else {
|
|
|
|
len as usize
|
|
|
|
};
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2021-04-10 09:00:03 +02:00
|
|
|
array.drain(start..start + len).collect()
|
2020-10-18 07:18:12 +02:00
|
|
|
}
|
2020-10-18 15:47:34 +02:00
|
|
|
#[rhai_fn(return_raw)]
|
|
|
|
pub fn retain(
|
2020-11-02 04:04:45 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
2020-10-18 15:47:34 +02:00
|
|
|
filter: FnPtr,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<Array, Box<EvalAltResult>> {
|
2021-03-23 05:13:53 +01:00
|
|
|
let mut drained = Array::new();
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2021-04-10 09:00:03 +02:00
|
|
|
let mut i = 0;
|
|
|
|
let mut x = 0;
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2021-04-10 09:00:03 +02:00
|
|
|
while x < array.len() {
|
2020-10-18 15:47:34 +02:00
|
|
|
if !filter
|
2021-04-10 09:00:03 +02:00
|
|
|
.call_dynamic(&ctx, None, [array[x].clone()])
|
2020-10-18 15:47:34 +02:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
|
|
|
|
if fn_sig.starts_with(filter.fn_name()) =>
|
|
|
|
{
|
2021-04-10 09:00:03 +02:00
|
|
|
filter.call_dynamic(&ctx, None, [array[x].clone(), (i as INT).into()])
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorInFunctionCall(
|
2020-11-11 06:25:45 +01:00
|
|
|
"retain".to_string(),
|
2020-12-30 14:12:51 +01:00
|
|
|
ctx.source().unwrap_or("").to_string(),
|
2020-10-18 15:47:34 +02:00
|
|
|
err,
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-18 15:47:34 +02:00
|
|
|
))
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-04-10 09:00:03 +02:00
|
|
|
drained.push(array.remove(x));
|
|
|
|
} else {
|
|
|
|
x += 1;
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
2021-04-10 09:00:03 +02:00
|
|
|
|
|
|
|
i += 1;
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(drained)
|
2020-10-18 15:47:34 +02:00
|
|
|
}
|
|
|
|
#[rhai_fn(name = "retain")]
|
2021-02-19 16:13:53 +01:00
|
|
|
pub fn retain_range(array: &mut Array, start: INT, len: INT) -> Array {
|
2020-10-18 15:47:34 +02:00
|
|
|
let start = if start < 0 {
|
2021-04-10 09:00:03 +02:00
|
|
|
let arr_len = array.len();
|
|
|
|
start
|
|
|
|
.checked_abs()
|
|
|
|
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if start as usize >= array.len() {
|
2021-04-10 09:00:03 +02:00
|
|
|
return mem::take(array);
|
2020-10-18 15:47:34 +02:00
|
|
|
} else {
|
|
|
|
start as usize
|
|
|
|
};
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
let len = if len < 0 {
|
|
|
|
0
|
2021-02-19 16:13:53 +01:00
|
|
|
} else if len as usize > array.len() - start {
|
|
|
|
array.len() - start
|
2020-10-18 15:47:34 +02:00
|
|
|
} else {
|
|
|
|
len as usize
|
|
|
|
};
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2021-05-29 12:33:29 +02:00
|
|
|
let mut drained: Array = array.drain(..start).collect();
|
2021-04-10 09:00:03 +02:00
|
|
|
drained.extend(array.drain(len..));
|
2020-10-18 07:18:12 +02:00
|
|
|
|
2020-10-18 15:47:34 +02:00
|
|
|
drained
|
2020-10-18 07:18:12 +02:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(name = "==", return_raw, pure)]
|
2020-11-08 16:00:37 +01:00
|
|
|
pub fn equals(
|
2020-11-30 04:20:51 +01:00
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
|
|
|
mut array2: Array,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<bool, Box<EvalAltResult>> {
|
2021-02-19 16:13:53 +01:00
|
|
|
if array.len() != array2.len() {
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(false);
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
2021-02-19 16:13:53 +01:00
|
|
|
if array.is_empty() {
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(true);
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
for (a1, a2) in array.iter_mut().zip(array2.iter_mut()) {
|
2021-03-09 06:16:05 +01:00
|
|
|
if !ctx
|
2021-03-01 08:39:49 +01:00
|
|
|
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [a1, a2])
|
2021-03-09 06:16:05 +01:00
|
|
|
.or_else(|err| match *err {
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
|
|
|
|
if fn_sig.starts_with(OP_EQUALS) =>
|
|
|
|
{
|
|
|
|
if a1.type_id() == a2.type_id() {
|
|
|
|
// No default when comparing same type
|
|
|
|
Err(err)
|
|
|
|
} else {
|
|
|
|
Ok(Dynamic::FALSE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
})?
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-03-22 04:18:09 +01:00
|
|
|
return Ok(false);
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:18:09 +01:00
|
|
|
Ok(true)
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
2021-03-09 06:16:05 +01:00
|
|
|
#[rhai_fn(name = "!=", return_raw, pure)]
|
2020-11-08 16:00:37 +01:00
|
|
|
pub fn not_equals(
|
|
|
|
ctx: NativeCallContext,
|
2021-02-19 16:13:53 +01:00
|
|
|
array: &mut Array,
|
|
|
|
array2: Array,
|
2021-03-22 04:18:09 +01:00
|
|
|
) -> Result<bool, Box<EvalAltResult>> {
|
|
|
|
equals(ctx, array, array2).map(|r| !r)
|
2020-11-08 16:00:37 +01:00
|
|
|
}
|
2020-10-18 07:18:12 +02:00
|
|
|
}
|