1
0

Finish adding string functions

This commit is contained in:
Jeff 2024-01-01 13:39:29 -05:00
parent 96f323979e
commit f5306be3dd

View File

@ -515,8 +515,34 @@ impl StringFunction {
Value::string(trimmed) Value::string(trimmed)
} }
StringFunction::TrimStartMatches => todo!(), StringFunction::TrimStartMatches => {
StringFunction::Truncate => todo!(), 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::<Vec<char>>();
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) Ok(value)