rhai/src/packages/string_more.rs

258 lines
6.9 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;
use crate::parser::INT;
#[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},
vec::Vec,
};
2020-05-13 13:21:42 +02:00
fn prepend<T: Display>(x: T, y: String) -> FuncReturn<String> {
Ok(format!("{}{}", x, y))
}
2020-05-13 13:21:42 +02:00
fn append<T: Display>(x: String, y: T) -> FuncReturn<String> {
Ok(format!("{}{}", x, y))
}
2020-05-13 13:21:42 +02:00
fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn<String> {
let offset = if s.is_empty() || len <= 0 {
2020-05-13 13:21:42 +02:00
return Ok("".to_string());
} else if start < 0 {
0
} else if (start as usize) >= s.chars().count() {
2020-05-13 13:21:42 +02:00
return Ok("".to_string());
} else {
start as usize
};
let chars: Vec<_> = s.chars().collect();
let len = if offset + (len as usize) > chars.len() {
chars.len() - offset
} else {
len as usize
};
2020-05-13 13:21:42 +02:00
Ok(chars[offset..][..len].into_iter().collect())
}
2020-05-13 13:21:42 +02:00
fn crop_string(s: &mut String, start: INT, len: INT) -> FuncReturn<()> {
let offset = if s.is_empty() || len <= 0 {
s.clear();
2020-05-13 13:21:42 +02:00
return Ok(());
} else if start < 0 {
0
} else if (start as usize) >= s.chars().count() {
s.clear();
2020-05-13 13:21:42 +02:00
return Ok(());
} else {
start as usize
};
let chars: Vec<_> = s.chars().collect();
let len = if offset + (len as usize) > chars.len() {
chars.len() - offset
} else {
len as usize
};
s.clear();
chars[offset..][..len]
.into_iter()
.for_each(|&ch| s.push(ch));
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-13 13:21:42 +02:00
lib.set_fn_2_mut( "+", |x: &mut String, _: ()| Ok(x.clone()));
2020-04-21 17:01:10 +02:00
reg_op!(lib, "+", prepend, INT, bool, char);
2020-05-13 13:21:42 +02:00
lib.set_fn_2("+", |_: (), y: String| 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"))]
{
2020-05-13 13:21:42 +02:00
lib.set_fn_2("+", |x: String, y: Array| Ok(format!("{}{:?}", x, y)));
lib.set_fn_2("+", |x: Array, y: String| Ok(format!("{:?}{}", x, y)));
2020-04-21 17:01:10 +02:00
}
2020-05-13 13:21:42 +02:00
lib.set_fn_1_mut("len", |s: &mut String| Ok(s.chars().count() as INT));
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"contains",
2020-05-13 13:21:42 +02:00
|s: &mut String, ch: char| Ok(s.contains(ch)),
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
"contains",
2020-05-13 13:21:42 +02:00
|s: &mut String, find: String| Ok(s.contains(&find)),
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
"index_of",
|s: &mut String, ch: char, start: INT| {
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
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"index_of",
|s: &mut String, 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
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut(
2020-04-21 17:01:10 +02:00
"index_of",
|s: &mut String, find: String, start: INT| {
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(&find)
.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
},
);
2020-05-13 13:21:42 +02:00
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"index_of",
|s: &mut String, find: String| {
2020-05-13 13:21:42 +02:00
Ok(s.find(&find)
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-13 13:21:42 +02:00
lib.set_fn_1_mut("clear", |s: &mut String| {
s.clear();
Ok(())
});
lib.set_fn_2_mut( "append", |s: &mut String, ch: char| {
s.push(ch);
Ok(())
});
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"append",
2020-05-13 13:21:42 +02:00
|s: &mut String, add: String| {
s.push_str(&add);
Ok(())
}
2020-04-21 17:01:10 +02:00
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut( "sub_string", sub_string);
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"sub_string",
|s: &mut String, start: INT| sub_string(s, start, s.len() as INT),
);
2020-05-13 13:21:42 +02:00
lib.set_fn_3_mut( "crop", crop_string);
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"crop",
|s: &mut String, start: INT| crop_string(s, start, s.len() as INT),
);
2020-05-13 13:21:42 +02:00
lib.set_fn_2_mut(
2020-04-21 17:01:10 +02:00
"truncate",
|s: &mut String, len: INT| {
if len >= 0 {
let chars: Vec<_> = s.chars().take(len as usize).collect();
s.clear();
2020-04-21 17:01:10 +02:00
chars.into_iter().for_each(|ch| s.push(ch));
} else {
s.clear();
}
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",
|s: &mut String, len: INT, ch: char| {
for _ in 0..s.chars().count() - len as usize {
s.push(ch);
}
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",
|s: &mut String, find: String, sub: String| {
let new_str = s.replace(&find, &sub);
s.clear();
s.push_str(&new_str);
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",
|s: &mut String, find: String, sub: char| {
let new_str = s.replace(&find, &sub.to_string());
s.clear();
s.push_str(&new_str);
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",
|s: &mut String, find: char, sub: String| {
let new_str = s.replace(&find.to_string(), &sub);
s.clear();
s.push_str(&new_str);
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",
|s: &mut String, find: char, sub: char| {
let new_str = s.replace(&find.to_string(), &sub.to_string());
s.clear();
s.push_str(&new_str);
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",
|s: &mut String| {
let trimmed = s.trim();
if trimmed.len() < s.len() {
*s = trimmed.to_string();
}
2020-05-13 13:21:42 +02:00
Ok(())
2020-04-21 17:01:10 +02:00
},
);
});