Bring more functions into modules in plugins via rhai_fn(name) attribute.
This commit is contained in:
@@ -11,9 +11,6 @@ use crate::utils::StaticVec;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
use crate::{result::EvalAltResult, token::Position};
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use crate::engine::make_getter;
|
||||
|
||||
use crate::stdlib::{
|
||||
any::TypeId, boxed::Box, fmt::Display, format, mem, string::String, string::ToString, vec::Vec,
|
||||
};
|
||||
@@ -61,29 +58,12 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
reg_functions!(lib += float; f32, f64);
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
{
|
||||
set_exported_fn!(lib, "+", string_funcs_array::append_array);
|
||||
set_exported_fn!(lib, "+", string_funcs_array::prepend_array);
|
||||
}
|
||||
lib.combine(exported_module!(index_functions));
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
lib.combine(exported_module!(object_functions));
|
||||
|
||||
lib.combine(exported_module!(string_functions));
|
||||
set_exported_fn!(lib, "contains", string_funcs::contains_char);
|
||||
set_exported_fn!(lib, "contains", string_funcs::contains_string);
|
||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_char);
|
||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_char_starting_from);
|
||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_string);
|
||||
set_exported_fn!(lib, "index_of", string_funcs::index_of_string_starting_from);
|
||||
set_exported_fn!(lib, "append", string_funcs::append_char);
|
||||
set_exported_fn!(lib, "+=", string_funcs::append_char);
|
||||
set_exported_fn!(lib, "append", string_funcs::append_string);
|
||||
set_exported_fn!(lib, "sub_string", string_funcs::sub_string);
|
||||
set_exported_fn!(lib, "sub_string", string_funcs::sub_string_starting_from);
|
||||
set_exported_fn!(lib, "crop", string_funcs::crop_string);
|
||||
set_exported_fn!(lib, "crop", string_funcs::crop_string_starting_from);
|
||||
set_exported_fn!(lib, "replace", string_funcs::replace_string);
|
||||
set_exported_fn!(lib, "replace", string_funcs::replace_char);
|
||||
set_exported_fn!(lib, "replace", string_funcs::replace_string_with_char);
|
||||
set_exported_fn!(lib, "replace", string_funcs::replace_char_with_string);
|
||||
|
||||
lib.set_raw_fn(
|
||||
"pad",
|
||||
@@ -131,9 +111,6 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
},
|
||||
);
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
set_exported_fn!(lib, make_getter("len"), string_funcs::len);
|
||||
|
||||
// Register string iterator
|
||||
lib.set_iter(
|
||||
TypeId::of::<ImmutableString>(),
|
||||
@@ -166,9 +143,27 @@ gen_concat_functions!(float => f32, f64);
|
||||
|
||||
#[export_module]
|
||||
mod string_functions {
|
||||
pub fn len(s: &mut ImmutableString) -> INT {
|
||||
string_funcs::len(s)
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn append_unit(s: ImmutableString, _x: ()) -> ImmutableString {
|
||||
s
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn prepend_unit(_x: (), s: ImmutableString) -> ImmutableString {
|
||||
s
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn append_char(s: &mut ImmutableString, ch: char) {
|
||||
*s += ch;
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn append_string(s: &mut ImmutableString, add: ImmutableString) {
|
||||
*s += &add;
|
||||
}
|
||||
|
||||
pub fn len(s: &mut ImmutableString) -> INT {
|
||||
s.chars().count() as INT
|
||||
}
|
||||
|
||||
pub fn clear(s: &mut ImmutableString) {
|
||||
s.make_mut().clear();
|
||||
}
|
||||
@@ -189,52 +184,17 @@ mod string_functions {
|
||||
*s = trimmed.to_string().into();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
mod string_funcs_array {
|
||||
use crate::engine::Array;
|
||||
use crate::plugin::*;
|
||||
use crate::stdlib::string::String;
|
||||
use crate::utils::ImmutableString;
|
||||
|
||||
#[export_fn]
|
||||
pub fn append_array(x: &mut ImmutableString, y: Array) -> String {
|
||||
format!("{}{:?}", x, y)
|
||||
}
|
||||
#[export_fn]
|
||||
pub fn prepend_array(x: &mut Array, y: ImmutableString) -> String {
|
||||
format!("{:?}{}", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
mod string_funcs {
|
||||
use crate::parser::INT;
|
||||
use crate::plugin::*;
|
||||
use crate::stdlib::string::{String, ToString};
|
||||
use crate::utils::{ImmutableString, StaticVec};
|
||||
|
||||
#[export_fn]
|
||||
pub fn append_unit(s: ImmutableString, _x: ()) -> ImmutableString {
|
||||
s
|
||||
}
|
||||
#[export_fn]
|
||||
pub fn prepend_unit(_x: (), s: ImmutableString) -> ImmutableString {
|
||||
s
|
||||
}
|
||||
#[export_fn]
|
||||
pub fn len(s: &mut ImmutableString) -> INT {
|
||||
s.chars().count() as INT
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "contains")]
|
||||
pub fn contains_char(s: &mut ImmutableString, ch: char) -> bool {
|
||||
s.contains(ch)
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "contains")]
|
||||
pub fn contains_string(s: &mut ImmutableString, find: ImmutableString) -> bool {
|
||||
s.contains(find.as_str())
|
||||
}
|
||||
#[export_fn]
|
||||
|
||||
#[rhai_fn(name = "index_of")]
|
||||
pub fn index_of_char_starting_from(s: &mut ImmutableString, ch: char, start: INT) -> INT {
|
||||
let start = if start < 0 {
|
||||
0
|
||||
@@ -249,13 +209,13 @@ mod string_funcs {
|
||||
.map(|index| s[0..start + index].chars().count() as INT)
|
||||
.unwrap_or(-1 as INT)
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "index_of")]
|
||||
pub fn index_of_char(s: &mut ImmutableString, ch: char) -> INT {
|
||||
s.find(ch)
|
||||
.map(|index| s[0..index].chars().count() as INT)
|
||||
.unwrap_or(-1 as INT)
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "index_of")]
|
||||
pub fn index_of_string_starting_from(
|
||||
s: &mut ImmutableString,
|
||||
find: ImmutableString,
|
||||
@@ -274,21 +234,14 @@ mod string_funcs {
|
||||
.map(|index| s[0..start + index].chars().count() as INT)
|
||||
.unwrap_or(-1 as INT)
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "index_of")]
|
||||
pub fn index_of_string(s: &mut ImmutableString, find: ImmutableString) -> INT {
|
||||
s.find(find.as_str())
|
||||
.map(|index| s[0..index].chars().count() as INT)
|
||||
.unwrap_or(-1 as INT)
|
||||
}
|
||||
#[export_fn]
|
||||
pub fn append_char(s: &mut ImmutableString, ch: char) {
|
||||
*s += ch;
|
||||
}
|
||||
#[export_fn]
|
||||
pub fn append_string(s: &mut ImmutableString, add: ImmutableString) {
|
||||
*s += &add;
|
||||
}
|
||||
#[export_fn]
|
||||
|
||||
#[rhai_fn(name = "sub_string")]
|
||||
pub fn sub_string(s: ImmutableString, start: INT, len: INT) -> ImmutableString {
|
||||
let offset = if s.is_empty() || len <= 0 {
|
||||
return "".to_string().into();
|
||||
@@ -316,12 +269,13 @@ mod string_funcs {
|
||||
.collect::<String>()
|
||||
.into()
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "sub_string")]
|
||||
pub fn sub_string_starting_from(s: ImmutableString, start: INT) -> ImmutableString {
|
||||
let len = s.len() as INT;
|
||||
sub_string(s, start, len)
|
||||
}
|
||||
#[export_fn]
|
||||
|
||||
#[rhai_fn(name = "crop")]
|
||||
fn crop_string(s: &mut ImmutableString, start: INT, len: INT) {
|
||||
let offset = if s.is_empty() || len <= 0 {
|
||||
s.make_mut().clear();
|
||||
@@ -347,24 +301,49 @@ mod string_funcs {
|
||||
copy.clear();
|
||||
copy.extend(chars.iter().skip(offset).take(len));
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "crop")]
|
||||
pub fn crop_string_starting_from(s: &mut ImmutableString, start: INT) {
|
||||
crop_string(s, start, s.len() as INT);
|
||||
}
|
||||
#[export_fn]
|
||||
|
||||
#[rhai_fn(name = "replace")]
|
||||
pub fn replace_string(s: &mut ImmutableString, find: ImmutableString, sub: ImmutableString) {
|
||||
*s = s.replace(find.as_str(), sub.as_str()).into();
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "replace")]
|
||||
pub fn replace_string_with_char(s: &mut ImmutableString, find: ImmutableString, sub: char) {
|
||||
*s = s.replace(find.as_str(), &sub.to_string()).into();
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "replace")]
|
||||
pub fn replace_char_with_string(s: &mut ImmutableString, find: char, sub: ImmutableString) {
|
||||
*s = s.replace(&find.to_string(), sub.as_str()).into();
|
||||
}
|
||||
#[export_fn]
|
||||
#[rhai_fn(name = "replace")]
|
||||
pub fn replace_char(s: &mut ImmutableString, find: char, sub: char) {
|
||||
*s = s.replace(&find.to_string(), &sub.to_string()).into();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
#[export_module]
|
||||
mod index_functions {
|
||||
use crate::engine::Array;
|
||||
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn append(x: &mut ImmutableString, y: Array) -> String {
|
||||
format!("{}{:?}", x, y)
|
||||
}
|
||||
#[rhai_fn(name = "+")]
|
||||
pub fn prepend(x: &mut Array, y: ImmutableString) -> String {
|
||||
format!("{:?}{}", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
#[export_module]
|
||||
mod object_functions {
|
||||
#[rhai_fn(name = "get$len")]
|
||||
pub fn len(s: &mut ImmutableString) -> INT {
|
||||
string_functions::len(s)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user