Auto-detect line ending in `unfill`
@@ -5,8 +5,6 @@ use std::str::FromStr;
/// TODO doc
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LineEnding {
- /// TODO
- CR,
/// TODO
CRLF,
/// TODO
@@ -14,22 +12,12 @@ pub enum LineEnding {
}
impl LineEnding {
- /// TODO
- #[inline]
- pub const fn len_chars(&self) -> usize {
- match self {
- Self::CRLF => 2,
- _ => 1,
- }
- }
-
/// TODO
#[inline]
pub const fn as_str(&self) -> &'static str {
match self {
- Self::CRLF => "\u{000D}\u{000A}",
- Self::LF => "\u{000A}",
- Self::CR => "\u{000D}",
+ Self::CRLF => "\r\n",
+ Self::LF => "\n",
}
}
}
@@ -43,8 +31,37 @@ impl FromStr for LineEnding {
match s {
"\u{000D}\u{000A}" => Result::Ok(LineEnding::CRLF),
"\u{000A}" => Result::Ok(LineEnding::LF),
- "\u{000D}" => Result::Ok(LineEnding::CR),
_ => Result::Err(()),
}
}
}
+
+/// TODO
+#[derive(Debug, Clone, Copy)]
+pub struct NonEmptyLines<'a>(pub &'a str);
+
+impl<'a> Iterator for NonEmptyLines<'a> {
+ type Item = (&'a str, Option<LineEnding>);
+
+ fn next(&mut self) -> Option<Self::Item> {
+ while let Some(lf) = self.0.find('\n') {
+ if lf == 0 || (lf == 1 && self.0.as_bytes()[lf - 1] == b'\r') {
+ self.0 = &self.0[(lf + 1)..];
+ continue;
+ }
+ let trimmed = match self.0.as_bytes()[lf - 1] {
+ b'\r' => (&self.0[..(lf - 1)], Some(LineEnding::CRLF)),
+ _ => (&self.0[..lf], Some(LineEnding::LF)),
+ };
+ self.0 = &self.0[(lf + 1)..];
+ return Some(trimmed);
+ }
+ if self.0.len() > 0 {
+ let result = Some((self.0, None));
+ self.0 = "";
+ return result;
+ } else {
+ return None;
+ }
+ }
+}
