From 2f2b20f56c73aae98ba52ff7c995b03834c4fb0e Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Thu, 18 Jan 2018 17:37:02 +0100 Subject: [PATCH 1/5] Add support for inputs with line breaks --- src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ef14d551..d0a2cb23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -585,7 +585,27 @@ impl<'a> WrapIterImpl<'a> { while let Some((idx, ch)) = self.char_indices.next() { let char_width = ch.width().unwrap_or(0); let char_len = ch.len_utf8(); - if is_whitespace(ch) { + + if ch == '\n' { + self.split = idx; + self.split_len = char_len; + self.line_width_at_split = self.line_width; + self.in_whitespace = false; + + // If this is not the final line, return the current line. Otherwise, + // we will return the line with its line break after exiting the loop + if self.split + self.split_len < self.source.len() { + let mut line = self.create_result_line(wrapper); + cow_add_assign(&mut line, &self.source[self.start..self.split]); + + self.start = self.split + self.split_len; + self.line_width += wrapper.subsequent_indent.width(); + self.line_width -= self.line_width_at_split; + + return Some(line); + } + } + else if is_whitespace(ch) { // Extend the previous split or create a new one. if self.in_whitespace { self.split_len += char_len; @@ -1223,4 +1243,24 @@ mod tests { " bar"]; assert_eq!(dedent(&add_nl(&x)), add_nl(&y)); } + + #[test] + fn multiline() { + assert_eq!(fill("1 3 5 7\n1 3 5 7",11),"1 3 5 7\n1 3 5 7"); + assert_eq!(fill("1 3 5 7\n1 3 5 7",5),"1 3 5\n7\n1 3 5\n7"); + assert_eq!(fill("1 3 5 7\n1 3 5 7",6),"1 3 5\n7\n1 3 5\n7"); + assert_eq!(fill("1 3 5 7\nabcdefghi k",5),"1 3 5\n7\nabcde\nfghi\nk"); + assert_eq!(fill("abcdefgh\ni k",5),"abcde\nfgh\ni k"); + assert_eq!(fill("abcdefgh\nijklmnop",5),"abcde\nfgh\nijklm\nnop"); + + assert_eq!(fill("test\n",11),"test\n"); + assert_eq!(fill("test\na",11),"test\na"); + assert_eq!(fill("test\na\n",11),"test\na\n"); + assert_eq!(fill("test\n\na\n\n",11),"test\n\na\n\n"); + assert_eq!(fill("test\n\nabcdefghijk\n\nabcdefghijkm",11),"test\n\nabcdefghijk\n\nabcdefghijk\nm"); + assert_eq!(fill("test\nabcdefghi\n",11),"test\nabcdefghi\n"); + assert_eq!(fill("test\nabcdefghi\n\n",11),"test\nabcdefghi\n\n"); + assert_eq!(fill("test\nabcdefghijklmnopq\n\n",11),"test\nabcdefghijk\nlmnopq\n\n"); + assert_eq!(fill("test\nabcdefghijklmnopq abcdefghijk\n\n",11),"test\nabcdefghijk\nlmnopq\nabcdefghijk\n\n"); + } } From 025a71b86c8c7852205773b5bec018b71d97760c Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Mon, 19 Feb 2018 16:11:20 +0100 Subject: [PATCH 2/5] Run rustfmt --- src/lib.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d0a2cb23..e98f9f3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -604,8 +604,7 @@ impl<'a> WrapIterImpl<'a> { return Some(line); } - } - else if is_whitespace(ch) { + } else if is_whitespace(ch) { // Extend the previous split or create a new one. if self.in_whitespace { self.split_len += char_len; @@ -1246,21 +1245,24 @@ mod tests { #[test] fn multiline() { - assert_eq!(fill("1 3 5 7\n1 3 5 7",11),"1 3 5 7\n1 3 5 7"); - assert_eq!(fill("1 3 5 7\n1 3 5 7",5),"1 3 5\n7\n1 3 5\n7"); - assert_eq!(fill("1 3 5 7\n1 3 5 7",6),"1 3 5\n7\n1 3 5\n7"); - assert_eq!(fill("1 3 5 7\nabcdefghi k",5),"1 3 5\n7\nabcde\nfghi\nk"); - assert_eq!(fill("abcdefgh\ni k",5),"abcde\nfgh\ni k"); - assert_eq!(fill("abcdefgh\nijklmnop",5),"abcde\nfgh\nijklm\nnop"); - - assert_eq!(fill("test\n",11),"test\n"); - assert_eq!(fill("test\na",11),"test\na"); - assert_eq!(fill("test\na\n",11),"test\na\n"); - assert_eq!(fill("test\n\na\n\n",11),"test\n\na\n\n"); - assert_eq!(fill("test\n\nabcdefghijk\n\nabcdefghijkm",11),"test\n\nabcdefghijk\n\nabcdefghijk\nm"); - assert_eq!(fill("test\nabcdefghi\n",11),"test\nabcdefghi\n"); - assert_eq!(fill("test\nabcdefghi\n\n",11),"test\nabcdefghi\n\n"); - assert_eq!(fill("test\nabcdefghijklmnopq\n\n",11),"test\nabcdefghijk\nlmnopq\n\n"); - assert_eq!(fill("test\nabcdefghijklmnopq abcdefghijk\n\n",11),"test\nabcdefghijk\nlmnopq\nabcdefghijk\n\n"); + assert_eq!(fill("1 3 5 7\n1 3 5 7", 11), "1 3 5 7\n1 3 5 7"); + assert_eq!(fill("1 3 5 7\n1 3 5 7", 5), "1 3 5\n7\n1 3 5\n7"); + assert_eq!(fill("1 3 5 7\n1 3 5 7", 6), "1 3 5\n7\n1 3 5\n7"); + assert_eq!(fill("1 3 5 7\nabcdefghi k", 5), "1 3 5\n7\nabcde\nfghi\nk"); + assert_eq!(fill("abcdefgh\ni k", 5), "abcde\nfgh\ni k"); + assert_eq!(fill("abcdefgh\nijklmnop", 5), "abcde\nfgh\nijklm\nnop"); + + assert_eq!(fill("test\n", 11), "test\n"); + assert_eq!(fill("test\na", 11), "test\na"); + assert_eq!(fill("test\na\n", 11), "test\na\n"); + assert_eq!(fill("test\n\na\n\n", 11), "test\n\na\n\n"); + assert_eq!(fill("test\n\nabcdefghijk\n\nabcdefghijkm", 11), + "test\n\nabcdefghijk\n\nabcdefghijk\nm"); + assert_eq!(fill("test\nabcdefghi\n", 11), "test\nabcdefghi\n"); + assert_eq!(fill("test\nabcdefghi\n\n", 11), "test\nabcdefghi\n\n"); + assert_eq!(fill("test\nabcdefghijklmnopq\n\n", 11), + "test\nabcdefghijk\nlmnopq\n\n"); + assert_eq!(fill("test\nabcdefghijklmnopq abcdefghijk\n\n", 11), + "test\nabcdefghijk\nlmnopq\nabcdefghijk\n\n"); } } From 5fc1e20388539f7666ec94c00850322d70f74b6b Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Mon, 19 Feb 2018 16:29:39 +0100 Subject: [PATCH 3/5] Remove cow_add_assign --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e98f9f3d..29db862e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -596,7 +596,7 @@ impl<'a> WrapIterImpl<'a> { // we will return the line with its line break after exiting the loop if self.split + self.split_len < self.source.len() { let mut line = self.create_result_line(wrapper); - cow_add_assign(&mut line, &self.source[self.start..self.split]); + line += &self.source[self.start..self.split]; self.start = self.split + self.split_len; self.line_width += wrapper.subsequent_indent.width(); From 158db92612e83bf2c7fd15c8e075b90e3b3d7a40 Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Mon, 19 Feb 2018 16:32:03 +0100 Subject: [PATCH 4/5] Remove redundant assignment --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 29db862e..a0b11f26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -599,8 +599,7 @@ impl<'a> WrapIterImpl<'a> { line += &self.source[self.start..self.split]; self.start = self.split + self.split_len; - self.line_width += wrapper.subsequent_indent.width(); - self.line_width -= self.line_width_at_split; + self.line_width = wrapper.subsequent_indent.width(); return Some(line); } From 2a122d90d7d0ed1319a1bb8c447a55b4cf84f607 Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Mon, 19 Feb 2018 17:25:53 +0100 Subject: [PATCH 5/5] Split up tests --- src/lib.rs | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a0b11f26..73480134 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1153,6 +1153,24 @@ mod tests { assert_eq!(wrap("foobar", 0), vec!["f", "o", "o", "b", "a", "r"]); } + #[test] + fn break_words_line_breaks() { + assert_eq!(fill("ab\ncdefghijkl", 5), "ab\ncdefg\nhijkl"); + assert_eq!(fill("abcdefgh\nijkl", 5), "abcde\nfgh\nijkl"); + } + + #[test] + fn preserve_line_breaks() { + assert_eq!(fill("test\n", 11), "test\n"); + assert_eq!(fill("test\n\na\n\n", 11), "test\n\na\n\n"); + assert_eq!(fill("1 3 5 7\n1 3 5 7", 7), "1 3 5 7\n1 3 5 7"); + } + + #[test] + fn wrap_preserve_line_breaks() { + assert_eq!(fill("1 3 5 7\n1 3 5 7", 5), "1 3 5\n7\n1 3 5\n7"); + } + #[test] fn non_breaking_space() { let wrapper = Wrapper::new(5).break_words(false); @@ -1241,27 +1259,4 @@ mod tests { " bar"]; assert_eq!(dedent(&add_nl(&x)), add_nl(&y)); } - - #[test] - fn multiline() { - assert_eq!(fill("1 3 5 7\n1 3 5 7", 11), "1 3 5 7\n1 3 5 7"); - assert_eq!(fill("1 3 5 7\n1 3 5 7", 5), "1 3 5\n7\n1 3 5\n7"); - assert_eq!(fill("1 3 5 7\n1 3 5 7", 6), "1 3 5\n7\n1 3 5\n7"); - assert_eq!(fill("1 3 5 7\nabcdefghi k", 5), "1 3 5\n7\nabcde\nfghi\nk"); - assert_eq!(fill("abcdefgh\ni k", 5), "abcde\nfgh\ni k"); - assert_eq!(fill("abcdefgh\nijklmnop", 5), "abcde\nfgh\nijklm\nnop"); - - assert_eq!(fill("test\n", 11), "test\n"); - assert_eq!(fill("test\na", 11), "test\na"); - assert_eq!(fill("test\na\n", 11), "test\na\n"); - assert_eq!(fill("test\n\na\n\n", 11), "test\n\na\n\n"); - assert_eq!(fill("test\n\nabcdefghijk\n\nabcdefghijkm", 11), - "test\n\nabcdefghijk\n\nabcdefghijk\nm"); - assert_eq!(fill("test\nabcdefghi\n", 11), "test\nabcdefghi\n"); - assert_eq!(fill("test\nabcdefghi\n\n", 11), "test\nabcdefghi\n\n"); - assert_eq!(fill("test\nabcdefghijklmnopq\n\n", 11), - "test\nabcdefghijk\nlmnopq\n\n"); - assert_eq!(fill("test\nabcdefghijklmnopq abcdefghijk\n\n", 11), - "test\nabcdefghijk\nlmnopq\nabcdefghijk\n\n"); - } }