From f5306be3dd6e39d71c24b18361d0e8d6c1fa9e6c Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 1 Jan 2024 13:39:29 -0500 Subject: [PATCH] Finish adding string functions --- src/built_in_functions/string.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/built_in_functions/string.rs b/src/built_in_functions/string.rs index 332ae75..3efbc2c 100644 --- a/src/built_in_functions/string.rs +++ b/src/built_in_functions/string.rs @@ -515,8 +515,34 @@ impl StringFunction { Value::string(trimmed) } - StringFunction::TrimStartMatches => todo!(), - StringFunction::Truncate => todo!(), + StringFunction::TrimStartMatches => { + Error::expect_argument_amount(self.name(), 2, arguments.len())?; + + let string = arguments.get(0).unwrap().as_string()?; + let pattern = arguments + .get(1) + .unwrap() + .as_string()? + .chars() + .collect::>(); + let trimmed = string.trim_start_matches(pattern.as_slice()).to_string(); + + Value::string(trimmed) + } + StringFunction::Truncate => { + Error::expect_argument_amount(self.name(), 2, arguments.len())?; + + let mut string = arguments.get(0).unwrap().as_string_mut()?; + let new_length = arguments.get(1).unwrap().as_integer()? as usize; + + *string = string + .chars() + .take(new_length) + .map(|char| char.to_string()) + .collect(); + + Value::none() + } }; Ok(value)