Build in array/blob concat functions.

This commit is contained in:
Stephen Chung
2021-12-22 19:59:48 +08:00
parent 422db4269e
commit 3751b6d018
4 changed files with 55 additions and 49 deletions

View File

@@ -24,7 +24,7 @@ def_package! {
}
#[export_module]
mod array_functions {
pub mod array_functions {
#[rhai_fn(name = "len", get = "len", pure)]
pub fn len(array: &mut Array) -> INT {
array.len() as INT
@@ -33,26 +33,29 @@ mod array_functions {
pub fn push(array: &mut Array, item: Dynamic) {
array.push(item);
}
#[rhai_fn(name = "append", name = "+=")]
pub fn append(array: &mut Array, y: Array) {
if !y.is_empty() {
if array.is_empty() {
*array = y;
#[rhai_fn(name = "append")]
pub fn append(array1: &mut Array, array2: Array) {
if !array2.is_empty() {
if array1.is_empty() {
*array1 = array2;
} else {
array.extend(y);
array1.extend(array2);
}
}
}
#[rhai_fn(name = "+")]
pub fn concat(mut array: Array, y: Array) -> Array {
if !y.is_empty() {
if array.is_empty() {
array = y;
pub fn concat(array1: Array, array2: Array) -> Array {
if !array2.is_empty() {
if array1.is_empty() {
array2
} else {
array.extend(y);
let mut array = array1;
array.extend(array2);
array
}
} else {
array1
}
array
}
pub fn insert(array: &mut Array, position: INT, item: Dynamic) {
if array.is_empty() {

View File

@@ -26,7 +26,7 @@ def_package! {
}
#[export_module]
mod blob_functions {
pub mod blob_functions {
pub const fn blob() -> Blob {
Blob::new()
}
@@ -64,12 +64,10 @@ mod blob_functions {
pub fn len(blob: &mut Blob) -> INT {
blob.len() as INT
}
#[rhai_fn(name = "push")]
pub fn push(blob: &mut Blob, item: INT) {
let item = (item & 0x000000ff) as u8;
blob.push(item);
}
#[rhai_fn(name = "append")]
pub fn append(blob: &mut Blob, y: Blob) {
if !y.is_empty() {
if blob.is_empty() {
@@ -79,6 +77,20 @@ mod blob_functions {
}
}
}
#[rhai_fn(name = "+")]
pub fn concat(blob1: Blob, blob2: Blob) -> Blob {
if !blob2.is_empty() {
if blob1.is_empty() {
blob2
} else {
let mut blob = blob1;
blob.extend(blob2);
blob
}
} else {
blob1
}
}
pub fn insert(blob: &mut Blob, position: INT, item: INT) {
let item = (item & 0x000000ff) as u8;

View File

@@ -3,8 +3,8 @@
use crate::{Module, Shared};
pub(crate) mod arithmetic;
mod array_basic;
mod blob_basic;
pub(crate) mod array_basic;
pub(crate) mod blob_basic;
mod fn_basic;
mod iter_basic;
mod lang_core;