From 065551ce9e797160429fb5b734b3e63701f34e04 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 29 Jan 2023 13:59:36 -0800 Subject: [PATCH] Preserve last newline even if not indented --- src/unindent.rs | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/src/unindent.rs b/src/unindent.rs index 11d19d2..ed637b1 100644 --- a/src/unindent.rs +++ b/src/unindent.rs @@ -1,4 +1,3 @@ -use std::iter::Peekable; use std::slice::Split; pub fn unindent(s: &str) -> String { @@ -90,11 +89,11 @@ fn count_spaces(line: &[u8]) -> Option { // Based on core::str::StrExt. trait BytesExt { - fn lines(&self) -> Lines; + fn lines(&self) -> Split bool>; } impl BytesExt for [u8] { - fn lines(&self) -> Lines { + fn lines(&self) -> Split bool> { fn is_newline(b: &u8) -> bool { *b == b'\n' } @@ -103,29 +102,6 @@ impl BytesExt for [u8] { } else { self }; - Lines { - split: bytestring.split(is_newline as fn(&u8) -> bool).peekable(), - } - } -} - -struct Lines<'a> { - split: Peekable bool>>, -} - -impl<'a> Iterator for Lines<'a> { - type Item = &'a [u8]; - - fn next(&mut self) -> Option { - match self.split.next() { - None => None, - Some(fragment) => { - if fragment.is_empty() && self.split.peek().is_none() { - None - } else { - Some(fragment) - } - } - } + bytestring.split(is_newline as fn(&u8) -> bool) } }