Namespace string builtins into "str::".

This commit is contained in:
Quest 2019-04-12 23:13:37 +02:00
parent d72334c246
commit dbf3949af6
3 changed files with 32 additions and 31 deletions

View File

@ -214,12 +214,12 @@ This crate offers a set of builtin functions.
|------------|-----------------|-------------| |------------|-----------------|-------------|
| min | >= 1 | Returns the minimum of the arguments | | min | >= 1 | Returns the minimum of the arguments |
| max | >= 1 | Returns the maximum of the arguments | | max | >= 1 | Returns the maximum of the arguments |
| to_lowercase | 1 | Returns lower-case version of string |
| len | 1 | Return the character length of string argument | | len | 1 | Return the character length of string argument |
| regex_matches | 2 | Returns true if first string argument matches regex in second | | str::regex_matches | 2 | Returns true if first string argument matches regex in second |
| regex_replace | 3 | Returns string with matches replaced by third argument | | str::regex_replace | 3 | Returns string with matches replaced by third argument |
| trim | 1 | Strips whitespace from start and end of string | | str::to_lowercase | 1 | Returns lower-case version of string |
| to_uppercase | 1 | Returns upper-case version of string | | str::to_uppercase | 1 | Returns upper-case version of string |
| str::trim | 1 | Strips whitespace from start and end of string |
The `min` and `max` functions can deal with a mixture of integer and floating point arguments. The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
They return the result as the type it was passed into the function. The regex functions require They return the result as the type it was passed into the function. The regex functions require

View File

@ -58,15 +58,6 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
}), }),
)), )),
// string functions
"to_lowercase" => Some(Function::new(
Some(1),
Box::new(|arguments| {
let subject = expect_string(&arguments[0])?;
Ok(Value::from(subject.to_lowercase()))
}),
)),
"len" => Some(Function::new( "len" => Some(Function::new(
Some(1), Some(1),
Box::new(|arguments| { Box::new(|arguments| {
@ -74,8 +65,11 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
Ok(Value::from(subject.len() as i64)) Ok(Value::from(subject.len() as i64))
}), }),
)), )),
// string functions
#[cfg(feature = "regex_support")] #[cfg(feature = "regex_support")]
"regex_matches" => Some(Function::new( "str::regex_matches" => Some(Function::new(
Some(2), Some(2),
Box::new(|arguments| { Box::new(|arguments| {
let subject = expect_string(&arguments[0])?; let subject = expect_string(&arguments[0])?;
@ -87,7 +81,7 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
}), }),
)), )),
#[cfg(feature = "regex_support")] #[cfg(feature = "regex_support")]
"regex_replace" => Some(Function::new( "str::regex_replace" => Some(Function::new(
Some(3), Some(3),
Box::new(|arguments| { Box::new(|arguments| {
let subject = expect_string(&arguments[0])?; let subject = expect_string(&arguments[0])?;
@ -99,20 +93,27 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
} }
}), }),
)), )),
"trim" => Some(Function::new( "str::to_lowercase" => Some(Function::new(
Some(1), Some(1),
Box::new(|arguments| { Box::new(|arguments| {
let subject = expect_string(&arguments[0])?; let subject = expect_string(&arguments[0])?;
Ok(Value::from(subject.trim())) Ok(Value::from(subject.to_lowercase()))
}), }),
)), )),
"to_uppercase" => Some(Function::new( "str::to_uppercase" => Some(Function::new(
Some(1), Some(1),
Box::new(|arguments| { Box::new(|arguments| {
let subject = expect_string(&arguments[0])?; let subject = expect_string(&arguments[0])?;
Ok(Value::from(subject.to_uppercase())) Ok(Value::from(subject.to_uppercase()))
}), }),
)), )),
"str::trim" => Some(Function::new(
Some(1),
Box::new(|arguments| {
let subject = expect_string(&arguments[0])?;
Ok(Value::from(subject.trim()))
}),
)),
_ => None, _ => None,
} }
} }

View File

@ -287,36 +287,36 @@ fn test_builtin_functions() {
eval("max(4.0, 3)"), eval("max(4.0, 3)"),
Ok(Value::Float(4.0)) Ok(Value::Float(4.0))
); );
assert_eq!(
eval("to_lowercase(\"FOOBAR\")"),
Ok(Value::from("foobar"))
);
assert_eq!( assert_eq!(
eval("len(\"foobar\")"), eval("len(\"foobar\")"),
Ok(Value::Int(6)) Ok(Value::Int(6))
); );
assert_eq!( assert_eq!(
eval("trim(\" foo bar \")"), eval("str::to_lowercase(\"FOOBAR\")"),
Ok(Value::from("foobar")) Ok(Value::from("foobar"))
); );
assert_eq!( assert_eq!(
eval("to_uppercase(\"foobar\")"), eval("str::to_uppercase(\"foobar\")"),
Ok(Value::from("FOOBAR")) Ok(Value::from("FOOBAR"))
); );
assert_eq!(
eval("str::trim(\" foo bar \")"),
Ok(Value::from("foo bar"))
);
} }
#[test] #[test]
#[cfg(feature = "regex_support")] #[cfg(feature = "regex_support")]
fn test_regex_functions() { fn test_regex_functions() {
assert_eq!( assert_eq!(
eval("regex_matches(\"foobar\", \"[ob]{3}\")"), eval("str::regex_matches(\"foobar\", \"[ob]{3}\")"),
Ok(Value::Boolean(true)) Ok(Value::Boolean(true))
); );
assert_eq!( assert_eq!(
eval("regex_matches(\"gazonk\", \"[ob]{3}\")"), eval("str::regex_matches(\"gazonk\", \"[ob]{3}\")"),
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
); );
match eval("regex_matches(\"foo\", \"[\")") { match eval("str::regex_matches(\"foo\", \"[\")") {
Err(EvalexprError::InvalidRegex{ regex, message }) => { Err(EvalexprError::InvalidRegex{ regex, message }) => {
assert_eq!(regex, "["); assert_eq!(regex, "[");
assert!(message.contains("unclosed character class")); assert!(message.contains("unclosed character class"));
@ -324,11 +324,11 @@ fn test_regex_functions() {
v => panic!(v), v => panic!(v),
}; };
assert_eq!( assert_eq!(
eval("regex_replace(\"foobar\", \".*?(o+)\", \"b$1\")"), eval("str::regex_replace(\"foobar\", \".*?(o+)\", \"b$1\")"),
Ok(Value::String("boobar".to_owned())) Ok(Value::String("boobar".to_owned()))
); );
assert_eq!( assert_eq!(
eval("regex_replace(\"foobar\", \".*?(i+)\", \"b$1\")"), eval("str::regex_replace(\"foobar\", \".*?(i+)\", \"b$1\")"),
Ok(Value::String("foobar".to_owned())) Ok(Value::String("foobar".to_owned()))
); );
} }