diff --git a/examples/array_methods.ion b/examples/array_methods.ion index 5d2b36458d35d019be1f63ec9ad5628716226aeb..2256604976cb5699e037e45fe312a7018460eea0 100644 --- a/examples/array_methods.ion +++ b/examples/array_methods.ion @@ -3,3 +3,4 @@ echo @split_at("onetwoone", "3") echo @graphemes("onetwo", "3") echo @bytes("onetwo") echo @chars("onetwo") +echo @lines($unescape("firstline\nsecondline")) diff --git a/examples/array_methods.out b/examples/array_methods.out index e692ef7b395af9c5c458eac7fb361187fff89ac2..dbc0b7e6bdf64398c66825cc311d3ba606f46b3a 100644 --- a/examples/array_methods.out +++ b/examples/array_methods.out @@ -3,3 +3,4 @@ one twoone o n e t w o 111 110 101 116 119 111 o n e t w o +firstline secondline diff --git a/manual/src/ch05-05-method.md b/manual/src/ch05-05-method.md index 51864ab647921f74d59c77ab691c80e01224bb6b..6a4f54253d0fc09e9fdc9a849c387694481b94ca 100644 --- a/manual/src/ch05-05-method.md +++ b/manual/src/ch05-05-method.md @@ -455,12 +455,33 @@ echo $unescape($line) The following are the currently-supported array methods. +- [lines](#lines) - [split](#split) - [split_at](#split_at) - [bytes](#bytes) - [chars](#chars) - [graphemes](#graphemes) +### lines + +Defaults to string variables. The supplied string will be split into one string per line in the input argument. + +#### Examples + +```ion +for line in @lines($unescape("first\nsecond\nthird") + echo $line +end +``` + +#### Output + +``` +first +second +third +``` + ### split Defaults to string variables. The supplied string will be split according to a pattern specified diff --git a/src/parser/shell_expand/words/methods/arrays.rs b/src/parser/shell_expand/words/methods/arrays.rs index 660ac1fd91ca366d6c4fc49e23e48da043444785..0e339208401d312ad3b02a2f398307c1ed048d77 100644 --- a/src/parser/shell_expand/words/methods/arrays.rs +++ b/src/parser/shell_expand/words/methods/arrays.rs @@ -34,6 +34,7 @@ impl<'a> ArrayMethod<'a> { "graphemes" => self.graphemes(expand_func), "bytes" => self.bytes(expand_func), "chars" => self.chars(expand_func), + "lines" => self.lines(expand_func), _ => Err("invalid array method"), }; @@ -174,6 +175,11 @@ impl<'a> ArrayMethod<'a> { .map(|c| c.to_string()) .select(self.selection.clone(), len)) } + + fn lines<E: Expander>(&self, expand_func: &E) -> Result<Array, &'static str> { + let variable = self.resolve_var(expand_func); + Ok(variable.lines().into_iter().map(|line| line.to_string()).collect()) + } } #[cfg(test)] @@ -190,6 +196,7 @@ mod test { match variable { "FOO" => Some("FOOBAR".to_owned()), "SPACEDFOO" => Some("FOO BAR".to_owned()), + "MULTILINE" => Some("FOO\nBAR".to_owned()), _ => None, } } @@ -413,4 +420,18 @@ mod test { array!["F", "O", "O", "B", "A", "R"] ); } + + #[test] + fn test_lines() { + let method = ArrayMethod { + method: "lines", + variable: "$MULTILINE", + pattern: Pattern::StringPattern("3"), + selection: Select::All, + }; + assert_eq!( + method.handle_as_array(&VariableExpander), + array!["FOO", "BAR"] + ); + } }