From 1fb9bf75c874f9de9c9e506126293b676ea14283 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy <mmstickman@gmail.com> Date: Wed, 11 Oct 2017 15:45:38 -0400 Subject: [PATCH] Improved method argument parsing --- examples/methods.ion | 6 +++++- examples/methods.out | 1 + src/parser/shell_expand/words/methods/strings.rs | 2 +- src/parser/shell_expand/words/mod.rs | 11 ++++++++++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/methods.ion b/examples/methods.ion index 768dfa97..00f9df4d 100644 --- a/examples/methods.ion +++ b/examples/methods.ion @@ -35,4 +35,8 @@ echo $replace($join([one two three], "\n"), "\n" "\t") let a = "applesauce" let pos = $find(a, "s") let array = [@split_at(a, $pos)] -echo $join(array, "\n") \ No newline at end of file +echo $join(array, "\n") + +let a = [1 2 3 4 5] +let a = "1 2 3 4 5" +echo $join(@split(a, " "), $join(a, " ")) diff --git a/examples/methods.out b/examples/methods.out index b6422295..dfb76765 100644 --- a/examples/methods.out +++ b/examples/methods.out @@ -65,3 +65,4 @@ one\ntwo\nthree one two three apple sauce +11 2 3 4 521 2 3 4 531 2 3 4 541 2 3 4 55 diff --git a/src/parser/shell_expand/words/methods/strings.rs b/src/parser/shell_expand/words/methods/strings.rs index 7135d44c..dfb8abb0 100644 --- a/src/parser/shell_expand/words/methods/strings.rs +++ b/src/parser/shell_expand/words/methods/strings.rs @@ -148,7 +148,7 @@ impl<'a> StringMethod<'a> { ); } } - "len" => if variable.starts_with('@') || variable.starts_with('[') { + "len" => if variable.starts_with('@') || is_array(variable) { let expanded = expand_string(variable, expand, false); output.push_str(&expanded.len().to_string()); } else if let Some(value) = expand.variable(variable, false) { diff --git a/src/parser/shell_expand/words/mod.rs b/src/parser/shell_expand/words/mod.rs index 0b6f6626..d1dc3323 100644 --- a/src/parser/shell_expand/words/mod.rs +++ b/src/parser/shell_expand/words/mod.rs @@ -124,8 +124,12 @@ impl<'a, E: Expander + 'a> WordIterator<'a, E> { start = self.read; while let Some(character) = iterator.next() { if character == b')' { - let pattern = &self.data[start..self.read].trim(); self.read += 1; + if depth != 0 { + depth -= 1; + continue; + } + let pattern = &self.data[start..self.read - 1].trim(); return if let Some(&b'[') = self.data.as_bytes().get(self.read) { @@ -144,6 +148,11 @@ impl<'a, E: Expander + 'a> WordIterator<'a, E> { selection: Select::All, }) }; + } else if character == b'(' { + depth += 1; + } else if character == b'\\' { + self.read += 1; + let _ = iterator.next(); } self.read += 1; } -- GitLab