Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve last newline even if not indented #55

Merged
merged 2 commits into from Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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