From cdb07add47056bc808f7708cd0e9d2efbf5e721e Mon Sep 17 00:00:00 2001
From: Michael Aaron Murphy <mmstickman@gmail.com>
Date: Fri, 28 Jul 2017 18:54:06 -0400
Subject: [PATCH] Implement $repeat()

---
 examples/methods.ion             |  3 ++-
 examples/methods.out             |  1 +
 src/parser/shell_expand/words.rs | 16 ++++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/examples/methods.ion b/examples/methods.ion
index 4b1545de..333379a9 100644
--- a/examples/methods.ion
+++ b/examples/methods.ion
@@ -25,4 +25,5 @@ echo $starts_with("one two", two)
 echo $ends_with("one two", one)
 echo $ends_with("one two", two)
 echo $contains("one two three", two)
-echo $contains("one two three", four)
\ No newline at end of file
+echo $contains("one two three", four)
+echo $repeat("one ", 5)
\ No newline at end of file
diff --git a/examples/methods.out b/examples/methods.out
index 7a385707..b53f57f3 100644
--- a/examples/methods.out
+++ b/examples/methods.out
@@ -56,3 +56,4 @@ e
 1
 1
 0
+one one one one one 
diff --git a/src/parser/shell_expand/words.rs b/src/parser/shell_expand/words.rs
index 89bc9d00..645a7529 100644
--- a/src/parser/shell_expand/words.rs
+++ b/src/parser/shell_expand/words.rs
@@ -493,6 +493,22 @@ impl<'a> StringMethod<'a> {
             "parent"       => path_eval!(parent),
             "to_lowercase" => string_case!(to_lowercase),
             "to_uppercase" => string_case!(to_uppercase),
+            "repeat" => {
+                let pattern = expand_string(pattern, expand, false).join(" ");
+                match pattern.parse::<usize>() {
+                    Ok(repeat) => {
+                        if let Some(value) = expand.vars.get_var(variable) {
+                            output.push_str(&value.repeat(repeat));
+                        } else if is_expression(variable) {
+                            let value = expand_string(variable, &expand, false).join(" ");
+                            output.push_str(&value.repeat(repeat));
+                        }
+                    },
+                    Err(_) => {
+                        eprintln!("ion: value supplied to $repeat() is not a valid number");
+                    }
+                }
+            }
             "replace" => {
                 let pattern = ArgumentSplitter::new(pattern)
                     .map(|x| expand_string(x, expand, false).join(" "))
-- 
GitLab