Skip to content

Commit

Permalink
Merge pull request #55 from dtolnay/trailing
Browse files Browse the repository at this point in the history
Preserve last newline even if not indented
  • Loading branch information
dtolnay committed Jan 29, 2023
2 parents ac7b06c + 31694b5 commit 1f70529
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
30 changes: 3 additions & 27 deletions src/unindent.rs
@@ -1,4 +1,3 @@
use std::iter::Peekable;
use std::slice::Split;

pub fn unindent(s: &str) -> String {
Expand Down Expand Up @@ -90,11 +89,11 @@ fn count_spaces(line: &[u8]) -> Option<usize> {

// Based on core::str::StrExt.
trait BytesExt {
fn lines(&self) -> Lines;
fn lines(&self) -> Split<u8, fn(&u8) -> bool>;
}

impl BytesExt for [u8] {
fn lines(&self) -> Lines {
fn lines(&self) -> Split<u8, fn(&u8) -> bool> {
fn is_newline(b: &u8) -> bool {
*b == b'\n'
}
Expand All @@ -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<Split<'a, u8, fn(&u8) -> bool>>,
}

impl<'a> Iterator for Lines<'a> {
type Item = &'a [u8];

fn next(&mut self) -> Option<Self::Item> {
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)
}
}
10 changes: 10 additions & 0 deletions 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"
Expand Down

0 comments on commit 1f70529

Please sign in to comment.