rhai/src/packages/string_more.rs

312 lines
9.2 KiB
Rust
Raw Normal View History

use crate::any::Dynamic;
2020-04-21 17:01:10 +02:00
use crate::def_package;
use crate::engine::Engine;
2020-05-13 13:21:42 +02:00
use crate::module::FuncReturn;
2020-05-25 07:44:28 +02:00
use crate::parser::{ImmutableString, INT};
use crate::result::EvalAltResult;
use crate::token::Position;
use crate::utils::StaticVec;
#[cfg(not(feature = "no_index"))]
use crate::engine::Array;
2020-04-24 06:39:24 +02:00
use crate::stdlib::{
any::TypeId,
2020-04-24 06:39:24 +02:00
fmt::Display,
format,
string::{String, ToString},
2020-06-16 03:34:30 +02:00
vec::Vec,
2020-04-24 06:39:24 +02:00
};
2020-05-25 07:44:28 +02:00
fn prepend<T: Display>(x: T, y: ImmutableString) -> FuncReturn<ImmutableString> {
Ok(format!("{}{}", x, y).into())
}
2020-05-25 07:44:28 +02:00
fn append<T: Display>(x: ImmutableString, y: T) -> FuncReturn<ImmutableString> {
Ok(format!("{}{}", x, y).into())
}
2020-05-25 07:44:28 +02:00
fn sub_string(s: ImmutableString, start: INT, len: INT) -> FuncReturn<ImmutableString> {
let offset = if s.is_empty() || len <= 0 {
2020-05-25 07:44:28 +02:00
return Ok("".to_string().into());
} else if start < 0 {
0
} else if (start as usize) >= s.chars().count() {
2020-05-25 07:44:28 +02:00
return Ok("".to_string().into());
} else {
start as usize
};
let chars: StaticVec<_> = s.chars().collect();
let len = if offset + (len as usize) > chars.len() {
chars.len() - offset
} else {
len as usize
};
2020-05-25 07:44:28 +02:00
Ok(chars
.iter()
.skip(offset)
.take(len)
.cloned()
.collect::<String>()
.into())
}
2020-05-25 07:44:28 +02:00
fn crop_string(s: &mut ImmutableString, start: INT, len: INT) -> FuncReturn<()> {
2020-05-25 11:01:39 +02:00
let offset = if s.is_empty() || len <= 0 {
2020-05-26 08:14:03 +02:00
s.make_mut().clear();
2020-05-13 13:21:42 +02:00
return Ok(());
} else if start < 0 {
0
2020-05-25 11:01:39 +02:00
} else if (start as usize) >= s.chars().count() {
2020-05-26 08:14:03 +02:00
s.make_mut().clear();
2020-05-13 13:21:42 +02:00
return Ok(());
} else {
start as usize
};
2020-05-25 11:01:39 +02:00
let chars: StaticVec<_> = s.chars().collect();
let len = if offset + (len as usize) > chars.len() {
chars.len() - offset
} else {
len as usize
};
2020-05-26 08:14:03 +02:00
let copy = s.make_mut();
2020-05-25 11:01:39 +02:00
copy.clear();
copy.extend(chars.iter().skip(offset).take(len));
2020-05-13 13:21:42 +02:00
Ok(())
}
2020-05-13 13:21:42 +02:00
macro_rules! reg_op {
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
$( $lib.set_fn_2($op, $func::<$par>); )*
};
}
2020-04-22 08:55:40 +02:00
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
2020-04-21 17:01:10 +02:00
reg_op!(lib, "+", append, INT, bool, char);
2020-05-25 07:44:28 +02:00
lib.set_fn_2( "+", |x: ImmutableString, _: ()| Ok(x));
2020-04-21 17:01:10 +02:00
reg_op!(lib, "+", prepend, INT, bool, char);
2020-05-25 07:44:28 +02:00
lib.set_fn_2("+", |_: (), y: ImmutableString| Ok(y));
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
2020-06-17 10:50:46 +02:00
reg_op!(lib, "+", append, i8, u8, i16, u16, i32, i64, u32, u64);
reg_op!(lib, "+", prepend, i8, u8, i16, u16, i32, i64, u32, u64);
#[cfg(not(target_arch = "wasm32"))]
{
reg_op!(lib, "+", append, i128, u128);
reg_op!(lib, "+", prepend, i128, u128);
}
2020-04-21 17:01:10 +02:00
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_float"))]
{
reg_op!(lib, "+", append, f32, f64);
reg_op!(lib, "+", prepend, f32, f64);
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_index"))]
{
lib.set_fn_2_mut("+", |x: &mut ImmutableString, y: Array| Ok(format!("{}{:?}", x, y)));
2020-05-25 07:44:28 +02:00
lib.set_fn_2_mut("+", |x: &mut Array, y: ImmutableString| Ok(format!("{:?}{}", x, y)));
2020-04-21 17:01:10 +02:00
}
lib.set_fn_1_mut("len", |s: &mut ImmutableString| Ok(s.chars().count() as INT));
#[cfg(not(feature = "no_object"))]
lib.set_getter_fn("len", |s: &mut ImmutableString| Ok(s.chars().count() as INT));
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"contains",
|s: &mut ImmutableString, ch: char| Ok(s.contains(ch)),
2020-04-21 17:01:10 +02:00
);
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"contains",
|s: &mut ImmutableString, find: ImmutableString| Ok(s.contains(find.as_str())),
2020-04-21 17:01:10 +02:00
);
lib.set_fn_3_mut(
2020-04-21 17:01:10 +02:00
"index_of",
|s: &mut ImmutableString, ch: char, start: INT| {
2020-04-21 17:01:10 +02:00
let start = if start < 0 {
0
} else if (start as usize) >= s.chars().count() {
2020-05-13 13:21:42 +02:00
return Ok(-1 as INT);
2020-04-21 17:01:10 +02:00
} else {
s.chars().take(start as usize).collect::<String>().len()
};
2020-05-13 13:21:42 +02:00
Ok(s[start..]
2020-04-21 17:01:10 +02:00
.find(ch)
.map(|index| s[0..start + index].chars().count() as INT)
2020-05-13 13:21:42 +02:00
.unwrap_or(-1 as INT))
2020-04-21 17:01:10 +02:00
},
);
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"index_of",
|s: &mut ImmutableString, ch: char| {
2020-05-13 13:21:42 +02:00
Ok(s.find(ch)
2020-04-21 17:01:10 +02:00
.map(|index| s[0..index].chars().count() as INT)
2020-05-13 13:21:42 +02:00
.unwrap_or(-1 as INT))
2020-04-21 17:01:10 +02:00
},
);
lib.set_fn_3_mut(
2020-04-21 17:01:10 +02:00
"index_of",
|s: &mut ImmutableString, find: ImmutableString, start: INT| {
2020-04-21 17:01:10 +02:00
let start = if start < 0 {
0
} else if (start as usize) >= s.chars().count() {
2020-05-13 13:21:42 +02:00
return Ok(-1 as INT);
2020-04-21 17:01:10 +02:00
} else {
s.chars().take(start as usize).collect::<String>().len()
};
2020-05-13 13:21:42 +02:00
Ok(s[start..]
2020-05-25 07:44:28 +02:00
.find(find.as_str())
2020-04-21 17:01:10 +02:00
.map(|index| s[0..start + index].chars().count() as INT)
2020-05-13 13:21:42 +02:00
.unwrap_or(-1 as INT))
2020-04-21 17:01:10 +02:00
},
);
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"index_of",
|s: &mut ImmutableString, find: ImmutableString| {
2020-05-25 07:44:28 +02:00
Ok(s.find(find.as_str())
2020-04-21 17:01:10 +02:00
.map(|index| s[0..index].chars().count() as INT)
2020-05-13 13:21:42 +02:00
.unwrap_or(-1 as INT))
2020-04-21 17:01:10 +02:00
},
);
2020-05-25 07:44:28 +02:00
lib.set_fn_1_mut("clear", |s: &mut ImmutableString| {
2020-05-26 08:14:03 +02:00
s.make_mut().clear();
2020-05-13 13:21:42 +02:00
Ok(())
});
2020-05-25 07:44:28 +02:00
lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| {
2020-05-26 08:14:03 +02:00
s.make_mut().push(ch);
2020-05-13 13:21:42 +02:00
Ok(())
});
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"append",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, add: ImmutableString| {
2020-05-26 08:14:03 +02:00
s.make_mut().push_str(add.as_str());
2020-05-13 13:21:42 +02:00
Ok(())
}
2020-04-21 17:01:10 +02:00
);
2020-05-25 07:44:28 +02:00
lib.set_fn_3("sub_string", sub_string);
lib.set_fn_2(
2020-04-21 17:01:10 +02:00
"sub_string",
2020-05-25 07:44:28 +02:00
|s: ImmutableString, start: INT| {
let len = s.len() as INT;
sub_string(s, start, len)
},
2020-04-21 17:01:10 +02:00
);
2020-05-25 07:44:28 +02:00
lib.set_fn_3_mut("crop", crop_string);
2020-05-13 13:21:42 +02:00
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"crop",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, start: INT| crop_string(s, start, s.len() as INT),
2020-04-21 17:01:10 +02:00
);
2020-05-13 13:21:42 +02:00
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"truncate",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, len: INT| {
2020-05-25 11:01:39 +02:00
if len > 0 {
let chars: StaticVec<_> = s.chars().collect();
2020-05-26 08:14:03 +02:00
let copy = s.make_mut();
2020-05-25 11:01:39 +02:00
copy.clear();
copy.extend(chars.into_iter().take(len as usize));
2020-04-21 17:01:10 +02:00
} else {
2020-05-26 08:14:03 +02:00
s.make_mut().clear();
2020-04-21 17:01:10 +02:00
}
2020-05-13 13:21:42 +02:00
Ok(())
2020-04-21 17:01:10 +02:00
},
);
lib.set_fn_var_args(
2020-04-21 17:01:10 +02:00
"pad",
&[TypeId::of::<ImmutableString>(), TypeId::of::<INT>(), TypeId::of::<char>()],
|engine: &Engine, args: &mut [&mut Dynamic]| {
let len = *args[1].downcast_ref::< INT>().unwrap();
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
{
if engine.max_string_size > 0 && len > 0 && (len as usize) > engine.max_string_size {
return Err(Box::new(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
engine.max_string_size,
len as usize,
Position::none(),
)));
}
}
let ch = *args[2].downcast_ref::< char>().unwrap();
let s = args[0].downcast_mut::<ImmutableString>().unwrap();
let copy = s.make_mut();
for _ in 0..copy.chars().count() - len as usize {
copy.push(ch);
}
if engine.max_string_size > 0 && copy.len() > engine.max_string_size {
Err(Box::new(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
engine.max_string_size,
copy.len(),
Position::none(),
)))
} else {
Ok(())
2020-04-21 17:01:10 +02:00
}
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut(
2020-04-21 17:01:10 +02:00
"replace",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, find: ImmutableString, sub: ImmutableString| {
*s = s.replace(find.as_str(), sub.as_str()).into();
2020-05-13 13:21:42 +02:00
Ok(())
2020-04-21 17:01:10 +02:00
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut(
2020-05-12 17:30:55 +02:00
"replace",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, find: ImmutableString, sub: char| {
*s = s.replace(find.as_str(), &sub.to_string()).into();
2020-05-13 13:21:42 +02:00
Ok(())
2020-05-12 17:30:55 +02:00
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut(
2020-05-12 17:30:55 +02:00
"replace",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, find: char, sub: ImmutableString| {
*s = s.replace(&find.to_string(), sub.as_str()).into();
2020-05-13 13:21:42 +02:00
Ok(())
2020-05-12 17:30:55 +02:00
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut(
2020-05-12 17:30:55 +02:00
"replace",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, find: char, sub: char| {
*s = s.replace(&find.to_string(), &sub.to_string()).into();
2020-05-13 13:21:42 +02:00
Ok(())
2020-05-12 17:30:55 +02:00
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_1_mut(
2020-04-21 17:01:10 +02:00
"trim",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString| {
2020-04-21 17:01:10 +02:00
let trimmed = s.trim();
if trimmed.len() < s.len() {
2020-05-25 07:44:28 +02:00
*s = trimmed.to_string().into();
2020-04-21 17:01:10 +02:00
}
2020-05-13 13:21:42 +02:00
Ok(())
2020-04-21 17:01:10 +02:00
},
);
2020-06-16 03:34:30 +02:00
// Register string iterator
lib.set_iter(
TypeId::of::<ImmutableString>(),
|arr| Box::new(
arr.cast::<ImmutableString>().chars().collect::<Vec<_>>().into_iter().map(Into::into)
) as Box<dyn Iterator<Item = Dynamic>>,
);
2020-04-21 17:01:10 +02:00
});