I need to iterate over lines in a string, but keep the newlines at the end in the strings that are yielded.
There is str.lines()
, but the strings it returns have the newline characters chopped off:
let result: Vec<_> = "foo\nbar\n".lines().collect();
assert_eq!(result, vec!["foo", "bar"]);
Here's what I need:
assert_eq!(lines("foo\nbar\n"), vec!["foo\n", "bar\n"]);
More test cases:
assert!(lines("").is_empty());
assert_eq!(lines("f"), vec!["f"]);
assert_eq!(lines("foo"), vec!["foo"]);
assert_eq!(lines("foo\n"), vec!["foo\n"]);
assert_eq!(lines("foo\nbar"), vec!["foo\n", "bar"]);
assert_eq!(lines("foo\r\nbar"), vec!["foo\r\n", "bar"]);
assert_eq!(lines("foo\r\nbar\r\n"), vec!["foo\r\n", "bar\r\n"]);
assert_eq!(lines("\nfoo"), vec!["\n", "foo"]);
assert_eq!(lines("\n\n\n"), vec!["\n", "\n", "\n"]);
I have a solution that basically calls find
in a loop, but I'm wondering if there's something more elegant.
This is similar to Split a string keeping the separators, but in that case, the characters are returned as separate items, but I want to keep them as part of the string:
["hello\n", "world\n"]; // This
["hello", "\n", "world", "\n"]; // Not this
The solution I currently have looks like this:
/// Iterator yielding every line in a string. The line includes newline character(s).
pub struct LinesWithEndings<'a> {
input: &'a str,
}
impl<'a> LinesWithEndings<'a> {
pub fn from(input: &'a str) -> LinesWithEndings<'a> {
LinesWithEndings {
input: input,
}
}
}
impl<'a> Iterator for LinesWithEndings<'a> {
type Item = &'a str;
#[inline]
fn next(&mut self) -> Option<&'a str> {
if self.input.is_empty() {
return None;
}
let split = self.input.find('\n').map(|i| i + 1).unwrap_or(self.input.len());
let (line, rest) = self.input.split_at(split);
self.input = rest;
Some(line)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With