@@ -65,10 +65,13 @@ fn native_newline_style() -> EffectiveNewlineStyle {
65
65
66
66
fn convert_to_windows_newlines ( formatted_text : & String ) -> String {
67
67
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 {
70
72
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) ,
72
75
}
73
76
}
74
77
transformed
@@ -162,34 +165,63 @@ mod tests {
162
165
163
166
#[ test]
164
167
fn applies_unix_newlines ( ) {
165
- let formatted_text = "One\r \n Two\n Three" ;
166
- let raw_input_text = formatted_text;
168
+ test_newlines_are_applied_correctly (
169
+ "One\r \n Two\n Three" ,
170
+ "One\n Two\n Three" ,
171
+ NewlineStyle :: Unix ,
172
+ ) ;
173
+ }
167
174
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 \n Two\r \n Three" ;
170
178
171
- assert_eq ! ( "One \n Two \n Three" , & out ) ;
179
+ test_newlines_are_applied_correctly ( formatted_text , formatted_text , NewlineStyle :: Windows ) ;
172
180
}
173
181
174
182
#[ test]
175
- fn preserves_standalone_carriage_returns_when_applying_windows_newlines ( ) {
176
- let formatted_text = "One\n Two\n Three\r Drei" ;
177
- let raw_input_text = "One\n Two\n Three\r Drei" ;
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\n Two\n Three\r Drei" ,
186
+ "One\r \n Two\r \n Three\r Drei" ,
187
+ NewlineStyle :: Windows ,
188
+ ) ;
189
+ }
181
190
182
- assert_eq ! ( "One\r \n Two\r \n Three\r Drei" , & 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\n Two\n Three\r Drei" ,
195
+ "One\n Two\n Three\r Drei" ,
196
+ NewlineStyle :: Unix ,
197
+ ) ;
183
198
}
184
199
185
200
#[ test]
186
- fn preserves_standalone_carriage_returns_when_applying_unix_newlines ( ) {
187
- let formatted_text = "One\n Two\n Three\r Drei" ;
188
- let raw_input_text = "One\n Two\n Three\r Drei" ;
201
+ fn keeps_carriage_returns_when_applying_windows_newlines_to_str_with_windows_newlines ( ) {
202
+ test_newlines_are_applied_correctly (
203
+ "One\r \n Two\r \n Three\r Drei" ,
204
+ "One\r \n Two\r \n Three\r Drei" ,
205
+ NewlineStyle :: Windows ,
206
+ ) ;
207
+ }
189
208
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 \n Two\r \n Three\r Drei" ,
213
+ "One\n Two\n Three\r Drei" ,
214
+ NewlineStyle :: Unix ,
215
+ ) ;
216
+ }
192
217
193
- assert_eq ! ( "One\n Two\n Three\r Drei" , & 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) ;
194
226
}
195
227
}
0 commit comments