From 5aa65af3adcad2582dcd755da7b382fbd06d72f5 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 1 Jan 2024 13:12:41 -0500 Subject: [PATCH] Implement more string functions --- src/built_in_functions/string.rs | 22 +++++++++++++++++++++- src/value/mod.rs | 8 ++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/built_in_functions/string.rs b/src/built_in_functions/string.rs index f2aaaa1..7107b66 100644 --- a/src/built_in_functions/string.rs +++ b/src/built_in_functions/string.rs @@ -282,7 +282,27 @@ impl StringFunction { Value::none() } - StringFunction::Remove => todo!(), + StringFunction::Remove => { + Error::expect_argument_amount(self.name(), 2, arguments.len())?; + + let mut string = arguments.get(0).unwrap().as_string_mut()?; + let index = arguments.get(1).unwrap().as_integer()? as usize; + let mut chars = string.chars().collect::>(); + + if index <= chars.len() - 1 { + let removed = chars.remove(index); + let new_string = chars + .iter() + .map(|char| char.to_string()) + .collect::(); + + *string = new_string; + + Value::some(Value::string(removed)) + } else { + Value::none() + } + } StringFunction::ReplaceRange => todo!(), StringFunction::Retain => todo!(), StringFunction::Split => { diff --git a/src/value/mod.rs b/src/value/mod.rs index fe69cd7..307bbff 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -47,8 +47,8 @@ impl Default for Value { } impl Value { - pub fn string(string: String) -> Self { - Value::String(Arc::new(RwLock::new(string))) + pub fn string>(string: T) -> Self { + Value::String(Arc::new(RwLock::new(string.into()))) } pub fn r#type(&self) -> Type { @@ -96,6 +96,10 @@ impl Value { Value::Option(None) } + pub fn some(value: Value) -> Self { + Value::Option(Some(Box::new(value))) + } + pub fn option(option: Option) -> Self { Value::Option(option.map(|value| Box::new(value))) }