rhai/src/packages/string_more.rs

263 lines
7.5 KiB
Rust
Raw Normal View History

2020-04-21 17:01:10 +02:00
use crate::def_package;
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::utils::StaticVec;
#[cfg(not(feature = "no_index"))]
use crate::engine::Array;
2020-04-24 06:39:24 +02:00
use crate::stdlib::{
fmt::Display,
format,
string::{String, ToString},
};
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"))]
{
reg_op!(lib, "+", append, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128);
reg_op!(lib, "+", prepend, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128);
}
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
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut(
2020-04-21 17:01:10 +02:00
"pad",
2020-05-25 07:44:28 +02:00
|s: &mut ImmutableString, len: INT, ch: char| {
2020-05-26 08:14:03 +02:00
let copy = s.make_mut();
2020-05-25 07:44:28 +02:00
for _ in 0..copy.chars().count() - len as usize {
copy.push(ch);
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-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
},
);
});