2019-04-07 06:10:36 +00:00
|
|
|
#[cfg(feature = "regex_support")]
|
|
|
|
use regex::Regex;
|
|
|
|
|
|
|
|
use crate::error::*;
|
2019-03-28 07:44:04 +00:00
|
|
|
use value::{FloatType, IntType};
|
2019-03-27 15:33:46 +00:00
|
|
|
use EvalexprError;
|
2019-03-19 18:08:23 +00:00
|
|
|
use Function;
|
|
|
|
use Value;
|
|
|
|
|
|
|
|
pub fn builtin_function(identifier: &str) -> Option<Function> {
|
|
|
|
match identifier {
|
|
|
|
"min" => Some(Function::new(
|
|
|
|
None,
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let mut min_int = IntType::max_value();
|
|
|
|
let mut min_float = 1.0f64 / 0.0f64;
|
|
|
|
debug_assert!(min_float.is_infinite());
|
|
|
|
|
|
|
|
for argument in arguments {
|
|
|
|
if let Value::Float(float) = argument {
|
|
|
|
min_float = min_float.min(*float);
|
|
|
|
} else if let Value::Int(int) = argument {
|
|
|
|
min_int = min_int.min(*int);
|
|
|
|
} else {
|
2019-03-27 15:33:46 +00:00
|
|
|
return Err(EvalexprError::expected_number(argument.clone()));
|
2019-03-19 18:08:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_int as FloatType) < min_float {
|
|
|
|
Ok(Value::Int(min_int))
|
|
|
|
} else {
|
|
|
|
Ok(Value::Float(min_float))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
"max" => Some(Function::new(
|
|
|
|
None,
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let mut max_int = IntType::min_value();
|
|
|
|
let mut max_float = -1.0f64 / 0.0f64;
|
|
|
|
debug_assert!(max_float.is_infinite());
|
|
|
|
|
|
|
|
for argument in arguments {
|
|
|
|
if let Value::Float(float) = argument {
|
|
|
|
max_float = max_float.max(*float);
|
|
|
|
} else if let Value::Int(int) = argument {
|
|
|
|
max_int = max_int.max(*int);
|
|
|
|
} else {
|
2019-03-27 15:33:46 +00:00
|
|
|
return Err(EvalexprError::expected_number(argument.clone()));
|
2019-03-19 18:08:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_int as FloatType) > max_float {
|
|
|
|
Ok(Value::Int(max_int))
|
|
|
|
} else {
|
|
|
|
Ok(Value::Float(max_float))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)),
|
2019-04-07 06:10:36 +00:00
|
|
|
|
|
|
|
"len" => Some(Function::new(
|
|
|
|
Some(1),
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let subject = expect_string(&arguments[0])?;
|
|
|
|
Ok(Value::from(subject.len() as i64))
|
|
|
|
}),
|
|
|
|
)),
|
2019-04-12 21:13:37 +00:00
|
|
|
|
|
|
|
// string functions
|
|
|
|
|
2019-04-07 06:10:36 +00:00
|
|
|
#[cfg(feature = "regex_support")]
|
2019-04-12 21:13:37 +00:00
|
|
|
"str::regex_matches" => Some(Function::new(
|
2019-04-07 06:10:36 +00:00
|
|
|
Some(2),
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let subject = expect_string(&arguments[0])?;
|
|
|
|
let re_str = expect_string(&arguments[1])?;
|
2019-04-12 21:03:13 +00:00
|
|
|
match Regex::new(re_str) {
|
2019-04-07 06:10:36 +00:00
|
|
|
Ok(re) => Ok(Value::Boolean(re.is_match(subject))),
|
2019-04-12 21:03:13 +00:00
|
|
|
Err(err) => Err(EvalexprError::invalid_regex(re_str.to_string(), format!("{}", err)))
|
2019-04-07 06:10:36 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
#[cfg(feature = "regex_support")]
|
2019-04-12 21:13:37 +00:00
|
|
|
"str::regex_replace" => Some(Function::new(
|
2019-04-07 06:10:36 +00:00
|
|
|
Some(3),
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let subject = expect_string(&arguments[0])?;
|
|
|
|
let re_str = expect_string(&arguments[1])?;
|
|
|
|
let repl = expect_string(&arguments[2])?;
|
2019-04-12 21:03:13 +00:00
|
|
|
match Regex::new(re_str) {
|
2019-04-07 06:10:36 +00:00
|
|
|
Ok(re) => Ok(Value::String(re.replace_all(subject, repl).to_string())),
|
2019-04-12 21:03:13 +00:00
|
|
|
Err(err) => Err(EvalexprError::invalid_regex(re_str.to_string(), format!("{}", err))),
|
2019-04-07 06:10:36 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
)),
|
2019-04-12 21:13:37 +00:00
|
|
|
"str::to_lowercase" => Some(Function::new(
|
2019-04-07 06:10:36 +00:00
|
|
|
Some(1),
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let subject = expect_string(&arguments[0])?;
|
2019-04-12 21:13:37 +00:00
|
|
|
Ok(Value::from(subject.to_lowercase()))
|
2019-04-07 06:10:36 +00:00
|
|
|
}),
|
|
|
|
)),
|
2019-04-12 21:13:37 +00:00
|
|
|
"str::to_uppercase" => Some(Function::new(
|
2019-04-07 06:10:36 +00:00
|
|
|
Some(1),
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let subject = expect_string(&arguments[0])?;
|
|
|
|
Ok(Value::from(subject.to_uppercase()))
|
|
|
|
}),
|
|
|
|
)),
|
2019-04-12 21:13:37 +00:00
|
|
|
"str::trim" => Some(Function::new(
|
|
|
|
Some(1),
|
|
|
|
Box::new(|arguments| {
|
|
|
|
let subject = expect_string(&arguments[0])?;
|
|
|
|
Ok(Value::from(subject.trim()))
|
|
|
|
}),
|
|
|
|
)),
|
2019-03-19 18:08:23 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|