rhai/src/packages/string_more.rs

396 lines
12 KiB
Rust
Raw Normal View History

2020-08-14 18:04:10 +02:00
#![allow(non_snake_case)]
use crate::plugin::*;
2020-04-24 06:39:24 +02:00
use crate::stdlib::{
any::TypeId, boxed::Box, format, mem, string::String, string::ToString, vec::Vec,
2020-04-24 06:39:24 +02:00
};
2020-11-16 16:10:14 +01:00
use crate::{def_package, Dynamic, FnPtr, ImmutableString, StaticVec, INT};
2020-08-14 18:04:10 +02:00
macro_rules! gen_concat_functions {
($root:ident => $($arg_type:ident),+ ) => {
2020-09-22 13:17:50 +02:00
pub mod $root { $( pub mod $arg_type {
use super::super::*;
2020-05-25 11:01:39 +02:00
2020-09-22 13:17:50 +02:00
#[export_module]
pub mod functions {
#[rhai_fn(name = "+")]
pub fn append_func(string: &str, arg: $arg_type) -> String {
format!("{}{}", string, arg)
2020-09-22 13:17:50 +02:00
}
#[rhai_fn(name = "+", pure)]
pub fn prepend_func(arg: &mut $arg_type, string: &str) -> String {
format!("{}{}", arg, string)
2020-09-22 13:17:50 +02:00
}
}
2020-09-22 13:17:50 +02:00
} )* }
2020-08-14 18:04:10 +02:00
}
}
2020-08-14 18:04:10 +02:00
macro_rules! reg_functions {
2020-08-24 16:37:44 +02:00
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
combine_with_exported_module!($mod_name, "strings_concat", $root::$arg_type::functions);
2020-08-24 16:37:44 +02:00
)* }
}
2020-04-22 08:55:40 +02:00
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
2020-10-10 16:13:55 +02:00
reg_functions!(lib += basic; INT, bool, FnPtr);
2020-08-14 18:04:10 +02:00
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
reg_functions!(lib += numbers; i8, u8, i16, u16, i32, i64, u32, u64);
2020-06-17 10:50:46 +02:00
2021-02-19 08:50:48 +01:00
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
2020-08-14 18:04:10 +02:00
reg_functions!(lib += num_128; i128, u128);
2020-04-21 17:01:10 +02:00
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_float"))]
2020-08-14 18:04:10 +02:00
reg_functions!(lib += float; f32, f64);
2020-09-13 16:12:11 +02:00
combine_with_exported_module!(lib, "string", string_functions);
2020-06-16 03:34:30 +02:00
// Register string iterator
lib.set_iter(
TypeId::of::<ImmutableString>(),
|string: Dynamic| Box::new(string.cast::<ImmutableString>().chars().collect::<Vec<_>>().into_iter().map(Into::into))
2020-06-16 03:34:30 +02:00
);
2020-04-21 17:01:10 +02:00
});
2020-08-14 18:04:10 +02:00
gen_concat_functions!(basic => INT, bool, char, FnPtr);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
gen_concat_functions!(numbers => i8, u8, i16, u16, i32, i64, u32, u64);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
2021-02-19 08:50:48 +01:00
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
2020-08-14 18:04:10 +02:00
gen_concat_functions!(num_128 => i128, u128);
#[cfg(not(feature = "no_float"))]
gen_concat_functions!(float => f32, f64);
#[export_module]
mod string_functions {
use crate::ImmutableString;
#[rhai_fn(name = "+")]
pub fn add_append_unit(string: ImmutableString, _x: ()) -> ImmutableString {
string
}
#[rhai_fn(name = "+")]
pub fn add_prepend_unit(_x: (), string: ImmutableString) -> ImmutableString {
string
}
#[rhai_fn(name = "len", get = "len")]
pub fn len(string: &str) -> INT {
string.chars().count() as INT
2020-08-14 18:04:10 +02:00
}
pub fn clear(string: &mut ImmutableString) {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
}
pub fn truncate(string: &mut ImmutableString, len: INT) {
2020-08-14 18:04:10 +02:00
if len > 0 {
let chars: StaticVec<_> = string.chars().collect();
let copy = string.make_mut();
2020-08-14 18:04:10 +02:00
copy.clear();
copy.extend(chars.into_iter().take(len as usize));
} else {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
}
}
pub fn trim(string: &mut ImmutableString) {
let trimmed = string.trim();
2020-08-14 18:04:10 +02:00
if trimmed.len() < string.len() {
*string = trimmed.to_string().into();
2020-08-14 18:04:10 +02:00
}
}
#[rhai_fn(name = "contains")]
pub fn contains_char(string: &str, ch: char) -> bool {
string.contains(ch)
2020-08-14 18:04:10 +02:00
}
pub fn contains(string: &str, find_string: &str) -> bool {
string.contains(find_string)
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "index_of")]
pub fn index_of_char_starting_from(string: &str, ch: char, start: INT) -> INT {
2020-08-14 18:04:10 +02:00
let start = if start < 0 {
0
} else if start as usize >= string.chars().count() {
2020-08-14 18:04:10 +02:00
return -1 as INT;
} else {
string
.chars()
.take(start as usize)
.collect::<String>()
.len()
2020-08-14 18:04:10 +02:00
};
string[start..]
2020-08-14 18:04:10 +02:00
.find(ch)
.map(|index| string[0..start + index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
#[rhai_fn(name = "index_of")]
pub fn index_of_char(string: &str, ch: char) -> INT {
string
.find(ch)
.map(|index| string[0..index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
#[rhai_fn(name = "index_of")]
pub fn index_of_string_starting_from(string: &str, find_string: &str, start: INT) -> INT {
2020-08-14 18:04:10 +02:00
let start = if start < 0 {
0
} else if start as usize >= string.chars().count() {
2020-08-14 18:04:10 +02:00
return -1 as INT;
} else {
string
.chars()
.take(start as usize)
.collect::<String>()
.len()
2020-08-14 18:04:10 +02:00
};
string[start..]
.find(find_string)
.map(|index| string[0..start + index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
#[rhai_fn(name = "index_of")]
pub fn index_of(string: &str, find_string: &str) -> INT {
string
.find(find_string)
.map(|index| string[0..index].chars().count() as INT)
2020-08-14 18:04:10 +02:00
.unwrap_or(-1 as INT)
}
pub fn sub_string(string: &str, start: INT, len: INT) -> ImmutableString {
let offset = if string.is_empty() || len <= 0 {
2020-08-14 18:04:10 +02:00
return "".to_string().into();
} else if start < 0 {
0
} else if start as usize >= string.chars().count() {
2020-08-14 18:04:10 +02:00
return "".to_string().into();
} else {
start as usize
};
let chars: StaticVec<_> = string.chars().collect();
2020-08-14 18:04:10 +02:00
2020-10-18 16:36:58 +02:00
let len = if offset + len as usize > chars.len() {
2020-08-14 18:04:10 +02:00
chars.len() - offset
} else {
len as usize
};
chars
.iter()
.skip(offset)
.take(len)
.cloned()
.collect::<String>()
.into()
}
#[rhai_fn(name = "sub_string")]
pub fn sub_string_starting_from(string: &str, start: INT) -> ImmutableString {
let len = string.len() as INT;
sub_string(string, start, len)
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "crop")]
pub fn crop(string: &mut ImmutableString, start: INT, len: INT) {
let offset = if string.is_empty() || len <= 0 {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
return;
} else if start < 0 {
0
} else if start as usize >= string.chars().count() {
string.make_mut().clear();
2020-08-14 18:04:10 +02:00
return;
} else {
start as usize
};
let chars: StaticVec<_> = string.chars().collect();
2020-08-14 18:04:10 +02:00
2020-10-18 16:36:58 +02:00
let len = if offset + len as usize > chars.len() {
2020-08-14 18:04:10 +02:00
chars.len() - offset
} else {
len as usize
};
let copy = string.make_mut();
2020-08-14 18:04:10 +02:00
copy.clear();
copy.extend(chars.iter().skip(offset).take(len));
}
#[rhai_fn(name = "crop")]
pub fn crop_string_starting_from(string: &mut ImmutableString, start: INT) {
crop(string, start, string.len() as INT);
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace(string: &mut ImmutableString, find_string: &str, substitute_string: &str) {
*string = string.replace(find_string, substitute_string).into();
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace_string_with_char(
string: &mut ImmutableString,
find_string: &str,
substitute_char: char,
) {
*string = string
.replace(find_string, &substitute_char.to_string())
.into();
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace_char_with_string(
string: &mut ImmutableString,
find_char: char,
substitute_string: &str,
) {
*string = string
.replace(&find_char.to_string(), substitute_string)
.into();
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "replace")]
pub fn replace_char(string: &mut ImmutableString, find_char: char, substitute_char: char) {
*string = string
.replace(&find_char.to_string(), &substitute_char.to_string())
.into();
2020-08-14 18:04:10 +02:00
}
2020-10-19 17:49:01 +02:00
2020-10-18 16:36:58 +02:00
#[rhai_fn(return_raw)]
pub fn pad(
2020-11-02 04:04:45 +01:00
_ctx: NativeCallContext,
string: &mut ImmutableString,
2020-10-18 16:36:58 +02:00
len: INT,
ch: char,
2020-11-16 09:28:04 +01:00
) -> Result<Dynamic, Box<crate::EvalAltResult>> {
2020-10-18 16:36:58 +02:00
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
2020-11-02 04:04:45 +01:00
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-11-16 09:28:04 +01:00
)
.into();
2020-10-18 16:36:58 +02:00
}
if len > 0 {
let orig_len = string.chars().count();
2020-10-18 16:36:58 +02:00
if len as usize > orig_len {
let p = string.make_mut();
2020-10-18 16:36:58 +02:00
for _ in 0..(len as usize - orig_len) {
p.push(ch);
}
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0
&& string.len() > _ctx.engine().max_string_size()
2020-10-18 16:36:58 +02:00
{
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
2020-10-18 16:36:58 +02:00
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-10-18 16:36:58 +02:00
)
.into();
}
}
}
2020-11-15 16:14:29 +01:00
Ok(Dynamic::UNIT)
2020-10-18 16:36:58 +02:00
}
#[rhai_fn(name = "pad", return_raw)]
pub fn pad_with_string(
2020-11-02 04:04:45 +01:00
_ctx: NativeCallContext,
string: &mut ImmutableString,
2020-10-18 16:36:58 +02:00
len: INT,
padding: &str,
2020-11-16 09:28:04 +01:00
) -> Result<Dynamic, Box<crate::EvalAltResult>> {
2020-10-18 16:36:58 +02:00
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
2020-11-02 04:04:45 +01:00
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-11-16 09:28:04 +01:00
)
.into();
2020-10-18 16:36:58 +02:00
}
if len > 0 {
let mut str_len = string.chars().count();
2020-10-18 16:36:58 +02:00
let padding_len = padding.chars().count();
if len as usize > str_len {
let p = string.make_mut();
2020-10-18 16:36:58 +02:00
while str_len < len as usize {
if str_len + padding_len <= len as usize {
p.push_str(padding);
str_len += padding_len;
} else {
p.extend(padding.chars().take(len as usize - str_len));
str_len = len as usize;
}
2020-10-18 16:36:58 +02:00
}
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0
&& string.len() > _ctx.engine().max_string_size()
2020-10-18 16:36:58 +02:00
{
2020-11-16 09:28:04 +01:00
return crate::EvalAltResult::ErrorDataTooLarge(
2020-10-18 16:36:58 +02:00
"Length of string".to_string(),
2020-11-20 09:52:28 +01:00
crate::Position::NONE,
2020-10-18 16:36:58 +02:00
)
.into();
}
}
}
2020-11-15 16:14:29 +01:00
Ok(Dynamic::UNIT)
2020-10-18 16:36:58 +02:00
}
2020-08-22 16:26:49 +02:00
#[cfg(not(feature = "no_index"))]
pub mod arrays {
2020-11-16 09:28:04 +01:00
use crate::Array;
2020-08-22 16:26:49 +02:00
#[rhai_fn(name = "+")]
pub fn append(string: &str, array: Array) -> String {
format!("{}{:?}", string, array)
2020-08-22 16:26:49 +02:00
}
#[rhai_fn(name = "+", pure)]
pub fn prepend(array: &mut Array, string: &str) -> String {
format!("{:?}{}", array, string)
2020-08-22 16:26:49 +02:00
}
pub fn split(string: &str, delimiter: &str) -> Array {
string.split(delimiter).map(Into::<Dynamic>::into).collect()
2020-09-25 17:02:49 +02:00
}
#[rhai_fn(name = "split")]
pub fn split_char(string: &str, delimiter: char) -> Array {
string.split(delimiter).map(Into::<Dynamic>::into).collect()
2020-09-25 17:02:49 +02:00
}
}
2020-09-22 16:45:11 +02:00
#[cfg(not(feature = "no_object"))]
pub mod maps {
2020-11-16 09:28:04 +01:00
use crate::Map;
2020-09-22 16:45:11 +02:00
#[rhai_fn(name = "+")]
pub fn append(string: &str, map: Map) -> String {
format!("{}#{:?}", string, map)
2020-09-22 16:45:11 +02:00
}
#[rhai_fn(name = "+", pure)]
pub fn prepend(map: &mut Map, string: &str) -> String {
format!("#{:?}{}", map, string)
2020-09-22 16:45:11 +02:00
}
}
}