]> git.lizzy.rs Git - rust.git/commitdiff
Fix issue #3263 (#3264)
authorwada314 <pc@wada314.jp>
Tue, 25 Dec 2018 09:03:48 +0000 (18:03 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Tue, 25 Dec 2018 09:03:48 +0000 (18:03 +0900)
src/string.rs
tests/target/format_strings/issue-3263.rs [new file with mode: 0644]

index 668324b4d8fe598e43998311a0b5b97e163d720e..c2e136d9938ac650a5f0908983c1000e3feb7fdb 100644 (file)
@@ -105,7 +105,7 @@ pub fn rewrite_string<'a>(
         // All the input starting at cur_start fits on the current line
         if graphemes.len() - cur_start <= cur_max_chars {
             for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
-                if is_line_feed(grapheme) {
+                if is_new_line(grapheme) {
                     // take care of blank lines
                     result = trim_end_but_line_feed(fmt.trim_end, result);
                     result.push_str("\n");
@@ -223,7 +223,7 @@ enum SnippetState {
 }
 
 fn not_whitespace_except_line_feed(g: &str) -> bool {
-    is_line_feed(g) || !is_whitespace(g)
+    is_new_line(g) || !is_whitespace(g)
 }
 
 /// Break the input string at a boundary character around the offset `max_chars`. A boundary
@@ -240,7 +240,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
         // line. If there is one, then text after it could be rewritten in a way that the available
         // space is fully used.
         for (i, grapheme) in input[0..=index].iter().enumerate() {
-            if is_line_feed(grapheme) {
+            if is_new_line(grapheme) {
                 if i <= index_minus_ws {
                     let mut line = &input[0..i].concat()[..];
                     if trim_end {
@@ -254,7 +254,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
 
         let mut index_plus_ws = index;
         for (i, grapheme) in input[index + 1..].iter().enumerate() {
-            if !trim_end && is_line_feed(grapheme) {
+            if !trim_end && is_new_line(grapheme) {
                 return SnippetState::EndWithLineFeed(
                     input[0..=index + 1 + i].concat(),
                     index + 2 + i,
@@ -325,8 +325,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
     }
 }
 
-fn is_line_feed(grapheme: &str) -> bool {
-    grapheme.as_bytes()[0] == b'\n'
+fn is_new_line(grapheme: &str) -> bool {
+    let bytes = grapheme.as_bytes();
+    bytes.starts_with(b"\n") || bytes.starts_with(b"\r\n")
 }
 
 fn is_whitespace(grapheme: &str) -> bool {
diff --git a/tests/target/format_strings/issue-3263.rs b/tests/target/format_strings/issue-3263.rs
new file mode 100644 (file)
index 0000000..72f7e9c
--- /dev/null
@@ -0,0 +1,26 @@
+// rustfmt-format_strings: true
+// rustfmt-newline_style: Windows
+
+#[test]
+fn compile_empty_program() {
+    let result = get_result();
+    let expected = "; ModuleID = \'foo\'
+
+; Function Attrs: nounwind
+declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i32, i1) #0
+
+declare i32 @write(i32, i8*, i32)
+
+declare i32 @putchar(i32)
+
+declare i32 @getchar()
+
+define i32 @main() {
+entry:
+  ret i32 0
+}
+
+attributes #0 = { nounwind }
+";
+    assert_eq!(result, CString::new(expected).unwrap());
+}