Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 09c9cd4

Browse files
wada314topecongiro
authored andcommitted
1 parent 84702bc commit 09c9cd4

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/string.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn rewrite_string<'a>(
105105
// All the input starting at cur_start fits on the current line
106106
if graphemes.len() - cur_start <= cur_max_chars {
107107
for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
108-
if is_line_feed(grapheme) {
108+
if is_new_line(grapheme) {
109109
// take care of blank lines
110110
result = trim_end_but_line_feed(fmt.trim_end, result);
111111
result.push_str("\n");
@@ -223,7 +223,7 @@ enum SnippetState {
223223
}
224224

225225
fn not_whitespace_except_line_feed(g: &str) -> bool {
226-
is_line_feed(g) || !is_whitespace(g)
226+
is_new_line(g) || !is_whitespace(g)
227227
}
228228

229229
/// 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]
240240
// line. If there is one, then text after it could be rewritten in a way that the available
241241
// space is fully used.
242242
for (i, grapheme) in input[0..=index].iter().enumerate() {
243-
if is_line_feed(grapheme) {
243+
if is_new_line(grapheme) {
244244
if i <= index_minus_ws {
245245
let mut line = &input[0..i].concat()[..];
246246
if trim_end {
@@ -254,7 +254,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
254254

255255
let mut index_plus_ws = index;
256256
for (i, grapheme) in input[index + 1..].iter().enumerate() {
257-
if !trim_end && is_line_feed(grapheme) {
257+
if !trim_end && is_new_line(grapheme) {
258258
return SnippetState::EndWithLineFeed(
259259
input[0..=index + 1 + i].concat(),
260260
index + 2 + i,
@@ -325,8 +325,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
325325
}
326326
}
327327

328-
fn is_line_feed(grapheme: &str) -> bool {
329-
grapheme.as_bytes()[0] == b'\n'
328+
fn is_new_line(grapheme: &str) -> bool {
329+
let bytes = grapheme.as_bytes();
330+
bytes.starts_with(b"\n") || bytes.starts_with(b"\r\n")
330331
}
331332

332333
fn is_whitespace(grapheme: &str) -> bool {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// rustfmt-format_strings: true
2+
// rustfmt-newline_style: Windows
3+
4+
#[test]
5+
fn compile_empty_program() {
6+
let result = get_result();
7+
let expected = "; ModuleID = \'foo\'
8+
9+
; Function Attrs: nounwind
10+
declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i32, i1) #0
11+
12+
declare i32 @write(i32, i8*, i32)
13+
14+
declare i32 @putchar(i32)
15+
16+
declare i32 @getchar()
17+
18+
define i32 @main() {
19+
entry:
20+
ret i32 0
21+
}
22+
23+
attributes #0 = { nounwind }
24+
";
25+
assert_eq!(result, CString::new(expected).unwrap());
26+
}

0 commit comments

Comments
 (0)