From 5586b97d79c11fd87341c33990c0871b19c03249 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 29 Jan 2023 13:54:42 -0800 Subject: [PATCH 1/2] Add test of indoc with zero chars in last line --- tests/test_indoc.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_indoc.rs b/tests/test_indoc.rs index e0ead11..c849bfc 100644 --- a/tests/test_indoc.rs +++ b/tests/test_indoc.rs @@ -1,5 +1,15 @@ use indoc::indoc; +const HELP: &str = indoc! {" + Usage: ./foo +"}; + +#[test] +fn test_global() { + let expected = "Usage: ./foo\n"; + assert_eq!(HELP, expected); +} + #[test] fn byte_string() { let indoc = indoc! {b" From 31694b5dcad0f25bf485d50401d0338a07ae2ba2 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 29 Jan 2023 13:59:36 -0800 Subject: [PATCH 2/2] 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) } }