Iterate over lines in a string, including the newl

2020-04-08 11:49发布

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

标签: string rust
1条回答
聊天终结者
2楼-- · 2020-04-08 12:38

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)
    }
}
查看更多
登录 后发表回答