Skip to content
Snippets Groups Projects
Unverified Commit bf1ef66e authored by Michael Aaron Murphy's avatar Michael Aaron Murphy Committed by GitHub
Browse files

Merge pull request #649 from nihathrael/lines

Add @lines() method (#441)
parents 037bfdbd dc271d55
No related branches found
No related tags found
No related merge requests found
...@@ -3,3 +3,4 @@ echo @split_at("onetwoone", "3") ...@@ -3,3 +3,4 @@ echo @split_at("onetwoone", "3")
echo @graphemes("onetwo", "3") echo @graphemes("onetwo", "3")
echo @bytes("onetwo") echo @bytes("onetwo")
echo @chars("onetwo") echo @chars("onetwo")
echo @lines($unescape("firstline\nsecondline"))
...@@ -3,3 +3,4 @@ one twoone ...@@ -3,3 +3,4 @@ one twoone
o n e t w o o n e t w o
111 110 101 116 119 111 111 110 101 116 119 111
o n e t w o o n e t w o
firstline secondline
...@@ -455,12 +455,33 @@ echo $unescape($line) ...@@ -455,12 +455,33 @@ echo $unescape($line)
The following are the currently-supported array methods. The following are the currently-supported array methods.
- [lines](#lines)
- [split](#split) - [split](#split)
- [split_at](#split_at) - [split_at](#split_at)
- [bytes](#bytes) - [bytes](#bytes)
- [chars](#chars) - [chars](#chars)
- [graphemes](#graphemes) - [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 ### split
Defaults to string variables. The supplied string will be split according to a pattern specified Defaults to string variables. The supplied string will be split according to a pattern specified
......
...@@ -34,6 +34,7 @@ impl<'a> ArrayMethod<'a> { ...@@ -34,6 +34,7 @@ impl<'a> ArrayMethod<'a> {
"graphemes" => self.graphemes(expand_func), "graphemes" => self.graphemes(expand_func),
"bytes" => self.bytes(expand_func), "bytes" => self.bytes(expand_func),
"chars" => self.chars(expand_func), "chars" => self.chars(expand_func),
"lines" => self.lines(expand_func),
_ => Err("invalid array method"), _ => Err("invalid array method"),
}; };
...@@ -174,6 +175,11 @@ impl<'a> ArrayMethod<'a> { ...@@ -174,6 +175,11 @@ impl<'a> ArrayMethod<'a> {
.map(|c| c.to_string()) .map(|c| c.to_string())
.select(self.selection.clone(), len)) .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)] #[cfg(test)]
...@@ -190,6 +196,7 @@ mod test { ...@@ -190,6 +196,7 @@ mod test {
match variable { match variable {
"FOO" => Some("FOOBAR".to_owned()), "FOO" => Some("FOOBAR".to_owned()),
"SPACEDFOO" => Some("FOO BAR".to_owned()), "SPACEDFOO" => Some("FOO BAR".to_owned()),
"MULTILINE" => Some("FOO\nBAR".to_owned()),
_ => None, _ => None,
} }
} }
...@@ -413,4 +420,18 @@ mod test { ...@@ -413,4 +420,18 @@ mod test {
array!["F", "O", "O", "B", "A", "R"] 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"]
);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment