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

Commit 1f09d94

Browse files
author
Ruben Schmidmeister
committed
Make sure windows newlines don't gain an extra carriage return
1 parent f54fc2f commit 1f09d94

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

src/formatting/newline_style.rs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ fn native_newline_style() -> EffectiveNewlineStyle {
6565

6666
fn convert_to_windows_newlines(formatted_text: &String) -> String {
6767
let mut transformed = String::with_capacity(2 * formatted_text.capacity());
68-
for c in formatted_text.chars() {
69-
match c {
68+
let mut chars = formatted_text.chars().peekable();
69+
while let Some(current_char) = chars.next() {
70+
let next_char = chars.peek();
71+
match current_char {
7072
LINE_FEED => transformed.push_str(WINDOWS_NEWLINE),
71-
c => transformed.push(c),
73+
CARRIAGE_RETURN if next_char == Some(&LINE_FEED) => {}
74+
current_char => transformed.push(current_char),
7275
}
7376
}
7477
transformed
@@ -162,34 +165,63 @@ mod tests {
162165

163166
#[test]
164167
fn applies_unix_newlines() {
165-
let formatted_text = "One\r\nTwo\nThree";
166-
let raw_input_text = formatted_text;
168+
test_newlines_are_applied_correctly(
169+
"One\r\nTwo\nThree",
170+
"One\nTwo\nThree",
171+
NewlineStyle::Unix,
172+
);
173+
}
167174

168-
let mut out = String::from(formatted_text);
169-
apply_newline_style(NewlineStyle::Unix, &mut out, raw_input_text);
175+
#[test]
176+
fn applying_windows_newlines_changes_nothing_for_windows_newlines() {
177+
let formatted_text = "One\r\nTwo\r\nThree";
170178

171-
assert_eq!("One\nTwo\nThree", &out);
179+
test_newlines_are_applied_correctly(formatted_text, formatted_text, NewlineStyle::Windows);
172180
}
173181

174182
#[test]
175-
fn preserves_standalone_carriage_returns_when_applying_windows_newlines() {
176-
let formatted_text = "One\nTwo\nThree\rDrei";
177-
let raw_input_text = "One\nTwo\nThree\rDrei";
178-
179-
let mut out = String::from(formatted_text);
180-
apply_newline_style(NewlineStyle::Windows, &mut out, raw_input_text);
183+
fn keeps_carriage_returns_when_applying_windows_newlines_to_str_with_unix_newlines() {
184+
test_newlines_are_applied_correctly(
185+
"One\nTwo\nThree\rDrei",
186+
"One\r\nTwo\r\nThree\rDrei",
187+
NewlineStyle::Windows,
188+
);
189+
}
181190

182-
assert_eq!("One\r\nTwo\r\nThree\rDrei", &out);
191+
#[test]
192+
fn keeps_carriage_returns_when_applying_unix_newlines_to_str_with_unix_newlines() {
193+
test_newlines_are_applied_correctly(
194+
"One\nTwo\nThree\rDrei",
195+
"One\nTwo\nThree\rDrei",
196+
NewlineStyle::Unix,
197+
);
183198
}
184199

185200
#[test]
186-
fn preserves_standalone_carriage_returns_when_applying_unix_newlines() {
187-
let formatted_text = "One\nTwo\nThree\rDrei";
188-
let raw_input_text = "One\nTwo\nThree\rDrei";
201+
fn keeps_carriage_returns_when_applying_windows_newlines_to_str_with_windows_newlines() {
202+
test_newlines_are_applied_correctly(
203+
"One\r\nTwo\r\nThree\rDrei",
204+
"One\r\nTwo\r\nThree\rDrei",
205+
NewlineStyle::Windows,
206+
);
207+
}
189208

190-
let mut out = String::from(formatted_text);
191-
apply_newline_style(NewlineStyle::Unix, &mut out, raw_input_text);
209+
#[test]
210+
fn keeps_carriage_returns_when_applying_unix_newlines_to_str_with_windows_newlines() {
211+
test_newlines_are_applied_correctly(
212+
"One\r\nTwo\r\nThree\rDrei",
213+
"One\nTwo\nThree\rDrei",
214+
NewlineStyle::Unix,
215+
);
216+
}
192217

193-
assert_eq!("One\nTwo\nThree\rDrei", &out);
218+
fn test_newlines_are_applied_correctly(
219+
input: &str,
220+
expected: &str,
221+
newline_style: NewlineStyle,
222+
) {
223+
let mut out = String::from(input);
224+
apply_newline_style(newline_style, &mut out, input);
225+
assert_eq!(expected, &out);
194226
}
195227
}

0 commit comments

Comments
 (0)