1
0

Implement more string functions

This commit is contained in:
Jeff 2024-01-01 13:12:41 -05:00
parent 20e0ec0e3d
commit 5aa65af3ad
2 changed files with 27 additions and 3 deletions

View File

@ -282,7 +282,27 @@ impl StringFunction {
Value::none() 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::<Vec<char>>();
if index <= chars.len() - 1 {
let removed = chars.remove(index);
let new_string = chars
.iter()
.map(|char| char.to_string())
.collect::<String>();
*string = new_string;
Value::some(Value::string(removed))
} else {
Value::none()
}
}
StringFunction::ReplaceRange => todo!(), StringFunction::ReplaceRange => todo!(),
StringFunction::Retain => todo!(), StringFunction::Retain => todo!(),
StringFunction::Split => { StringFunction::Split => {

View File

@ -47,8 +47,8 @@ impl Default for Value {
} }
impl Value { impl Value {
pub fn string(string: String) -> Self { pub fn string<T: Into<String>>(string: T) -> Self {
Value::String(Arc::new(RwLock::new(string))) Value::String(Arc::new(RwLock::new(string.into())))
} }
pub fn r#type(&self) -> Type { pub fn r#type(&self) -> Type {
@ -96,6 +96,10 @@ impl Value {
Value::Option(None) Value::Option(None)
} }
pub fn some(value: Value) -> Self {
Value::Option(Some(Box::new(value)))
}
pub fn option(option: Option<Value>) -> Self { pub fn option(option: Option<Value>) -> Self {
Value::Option(option.map(|value| Box::new(value))) Value::Option(option.map(|value| Box::new(value)))
} }