Skip to content

Commit 03fdc5c

Browse files
committed
feat: add strings.repeat builtin
Adds strings.repeat(s, count) which repeats string s count times. Example: strings.repeat("ab", 3) returns "ababab"
1 parent cf49681 commit 03fdc5c

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/builtins/strings.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub fn register(m: &mut builtins::BuiltinsMap<&'static str, builtins::BuiltinFcn
3636
m.insert("strings.any_suffix_match", (any_suffix_match, 2));
3737
m.insert("strings.count", (strings_count, 2));
3838
m.insert("strings.replace_n", (replace_n, 2));
39+
m.insert("strings.repeat", (repeat, 2));
3940
m.insert("strings.reverse", (reverse, 1));
4041
m.insert("substring", (substring, 3));
4142
m.insert("trim", (trim, 2));
@@ -591,6 +592,25 @@ fn reverse(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) ->
591592
Ok(Value::String(s.chars().rev().collect::<String>().into()))
592593
}
593594

595+
fn repeat(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Result<Value> {
596+
let name = "strings.repeat";
597+
ensure_args_count(span, name, params, args, 2)?;
598+
599+
let s = ensure_string(name, &params[0], &args[0])?;
600+
let count = args[1].as_f64().unwrap() as usize;
601+
602+
if count == 0 {
603+
return Ok(Value::String("".into()));
604+
}
605+
606+
let mut result = String::new();
607+
for _ in 0..count {
608+
result.push_str(&s);
609+
}
610+
611+
Ok(Value::String(result.into()))
612+
}
613+
594614
fn substring(span: &Span, params: &[Ref<Expr>], args: &[Value], strict: bool) -> Result<Value> {
595615
let name = "substring";
596616
ensure_args_count(span, name, params, args, 3)?;

0 commit comments

Comments
 (0)