2020-06-09 06:21:21 +02:00
|
|
|
///! This example registers a variety of functions that operate on strings.
|
|
|
|
///! Remember to use `ImmutableString` or `&str` instead of `String` as parameters.
|
2022-01-11 15:12:46 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, ImmutableString, Scope};
|
2020-06-09 06:21:21 +02:00
|
|
|
use std::io::{stdin, stdout, Write};
|
|
|
|
|
|
|
|
/// Trim whitespace from a string. The original string argument is changed.
|
|
|
|
///
|
|
|
|
/// This version uses `&mut ImmutableString`
|
|
|
|
fn trim_string(s: &mut ImmutableString) {
|
|
|
|
*s = s.trim().into();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Notice this is different from the built-in Rhai 'len' function for strings
|
|
|
|
/// which counts the actual number of Unicode _characters_ in a string.
|
|
|
|
/// This version simply counts the number of _bytes_ in the UTF-8 representation.
|
|
|
|
///
|
|
|
|
/// This version uses `&str`.
|
2022-01-11 15:12:46 +01:00
|
|
|
fn count_string_bytes(s: &str) -> i64 {
|
|
|
|
s.len() as i64
|
2020-06-09 06:21:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This version uses `ImmutableString` and `&str`.
|
2022-01-11 15:12:46 +01:00
|
|
|
fn find_substring(s: ImmutableString, sub: &str) -> i64 {
|
|
|
|
s.find(sub).map(|x| x as i64).unwrap_or(-1)
|
2020-06-09 06:21:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
// Create a `raw` Engine with no built-in string functions.
|
|
|
|
let mut engine = Engine::new_raw();
|
|
|
|
|
2020-10-07 09:55:45 +02:00
|
|
|
engine
|
2022-01-11 15:12:46 +01:00
|
|
|
// Register string functions
|
2020-10-07 09:55:45 +02:00
|
|
|
.register_fn("trim", trim_string)
|
|
|
|
.register_fn("len", count_string_bytes)
|
|
|
|
.register_fn("index_of", find_substring)
|
2021-02-20 16:46:25 +01:00
|
|
|
// Register string functions using closures
|
2022-01-11 15:12:46 +01:00
|
|
|
.register_fn("display", |label: &str, value: i64| {
|
2021-02-20 16:46:25 +01:00
|
|
|
println!("{}: {}", label, value)
|
2020-10-07 09:55:45 +02:00
|
|
|
})
|
2021-02-20 16:46:25 +01:00
|
|
|
.register_fn("display", |label: ImmutableString, value: &str| {
|
|
|
|
println!(r#"{}: "{}""#, label, value) // Quote the input string
|
2020-10-07 09:55:45 +02:00
|
|
|
});
|
2020-06-09 06:21:21 +02:00
|
|
|
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
let mut input = String::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
scope.clear();
|
|
|
|
|
|
|
|
println!("Type something. Press Ctrl-C to exit.");
|
|
|
|
print!("strings> ");
|
|
|
|
stdout().flush().expect("couldn't flush stdout");
|
|
|
|
|
|
|
|
input.clear();
|
|
|
|
|
|
|
|
if let Err(err) = stdin().read_line(&mut input) {
|
|
|
|
panic!("input error: {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.push("x", input.clone());
|
|
|
|
|
|
|
|
println!("Line: {}", input.replace('\r', "\\r").replace('\n', "\\n"));
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
engine.run_with_scope(
|
2020-06-09 06:21:21 +02:00
|
|
|
&mut scope,
|
|
|
|
r#"
|
|
|
|
display("Length", x.len());
|
|
|
|
x.trim();
|
|
|
|
display("Trimmed", x);
|
|
|
|
display("Trimmed Length", x.len());
|
|
|
|
display("Index of \"!!!\"", x.index_of("!!!"));
|
2020-12-26 08:41:41 +01:00
|
|
|
"#,
|
2020-06-09 06:21:21 +02:00
|
|
|
)?;
|
|
|
|
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
}
|