Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 87 additions & 66 deletions stdlib/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,83 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, error::RuntimeErrorKind, value::RuntimeValue};
use techscript_runtime::RuntimeContext;

fn trim(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.trim().to_string()))
}

fn replace(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let source = args[0].try_into_string()?;
let from = args[1].try_into_string()?;
let to = args[2].try_into_string()?;
Ok(RuntimeValue::Str(source.replace(&from, &to)))
}

fn split(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
let pat = args[1].try_into_string()?;
let parts: Vec<RuntimeValue> = s
.split(&pat)
.map(|p| RuntimeValue::Str(p.to_string()))
.collect();
Ok(RuntimeValue::List {
items: Rc::new(std::cell::RefCell::new(parts)),
is_const: false,
})
}

fn join(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let sep = args[1].try_into_string()?;
if let RuntimeValue::List { items, .. } = &args[0] {
let mut str_parts = Vec::new();
for item in items.borrow().iter() {
str_parts.push(item.try_into_string()?);
}
Ok(RuntimeValue::Str(str_parts.join(&sep)))
} else {
Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "List".to_string(),
found: args[0].runtime_type().to_string(),
},
None,
None,
))
}
}

fn to_lower(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_lowercase()))
}

fn to_upper(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_uppercase()))
}

fn contains(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
let sub = args[1].try_into_string()?;
Ok(RuntimeValue::Bool(s.contains(&sub)))
}

fn from_int(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let val = args[0].try_into_int()?;
Ok(RuntimeValue::Str(val.to_string()))
}

fn from_float(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let val = args[0].try_into_float()?;
Ok(RuntimeValue::Str(val.to_string()))
}

fn from_bool(_ctx: &mut RuntimeContext, args: Vec<RuntimeValue>) -> Result<RuntimeValue, RuntimeError> {
let val = args[0].try_into_bool()?;
Ok(RuntimeValue::Str(val.to_string()))
}

impl StdlibRegistry {
pub fn register_strings(&mut self) {
Expand All @@ -13,10 +90,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "trim".to_string(),
arity: 1,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.trim().to_string()))
},
callback: trim,
}),
);

Expand All @@ -25,12 +99,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "replace".to_string(),
arity: 3,
callback: |_ctx, args| {
let source = args[0].try_into_string()?;
let from = args[1].try_into_string()?;
let to = args[2].try_into_string()?;
Ok(RuntimeValue::Str(source.replace(&from, &to)))
},
callback: replace,
}),
);

Expand All @@ -39,18 +108,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "split".to_string(),
arity: 2,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
let pat = args[1].try_into_string()?;
let parts: Vec<RuntimeValue> = s
.split(&pat)
.map(|p| RuntimeValue::Str(p.to_string()))
.collect();
Ok(RuntimeValue::List {
items: Rc::new(std::cell::RefCell::new(parts)),
is_const: false,
})
},
callback: split,
}),
);

Expand All @@ -59,25 +117,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "join".to_string(),
arity: 2,
callback: |_ctx, args| {
let sep = args[1].try_into_string()?;
if let RuntimeValue::List { items, .. } = &args[0] {
let mut str_parts = Vec::new();
for item in items.borrow().iter() {
str_parts.push(item.try_into_string()?);
}
Ok(RuntimeValue::Str(str_parts.join(&sep)))
} else {
Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "List".to_string(),
found: args[0].runtime_type().to_string(),
},
None,
None,
))
}
},
callback: join,
}),
);

Expand All @@ -86,10 +126,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "to_lower".to_string(),
arity: 1,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_lowercase()))
},
callback: to_lower,
}),
);

Expand All @@ -98,10 +135,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "to_upper".to_string(),
arity: 1,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_uppercase()))
},
callback: to_upper,
}),
);

Expand All @@ -110,11 +144,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "contains".to_string(),
arity: 2,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
let sub = args[1].try_into_string()?;
Ok(RuntimeValue::Bool(s.contains(&sub)))
},
callback: contains,
}),
);

Expand All @@ -123,10 +153,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "from_int".to_string(),
arity: 1,
callback: |_ctx, args| {
let val = args[0].try_into_int()?;
Ok(RuntimeValue::Str(val.to_string()))
},
callback: from_int,
}),
);

Expand All @@ -135,10 +162,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "from_float".to_string(),
arity: 1,
callback: |_ctx, args| {
let val = args[0].try_into_float()?;
Ok(RuntimeValue::Str(val.to_string()))
},
callback: from_float,
}),
);

Expand All @@ -147,10 +171,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "from_bool".to_string(),
arity: 1,
callback: |_ctx, args| {
let val = args[0].try_into_bool()?;
Ok(RuntimeValue::Str(val.to_string()))
},
callback: from_bool,
}),
);

Expand Down
Loading
Loading