File tree Expand file tree Collapse file tree 1 file changed +24
-6
lines changed Expand file tree Collapse file tree 1 file changed +24
-6
lines changed Original file line number Diff line number Diff line change @@ -331,18 +331,18 @@ pub fn unindent(s: &str) -> String {
331
331
min_indent
332
332
} else {
333
333
saw_first_line = true ;
334
- let mut spaces = 0 ;
334
+ let mut whitespace = 0 ;
335
335
line. chars ( ) . all ( |char| {
336
- // Only comparing against space because I wouldn't
337
- // know what to do with mixed whitespace chars
338
- if char == ' ' {
339
- spaces += 1 ;
336
+ // Compare against either space or tab, ignoring whether they
337
+ // are mixed or not
338
+ if char == ' ' || char == '\t' {
339
+ whitespace += 1 ;
340
340
true
341
341
} else {
342
342
false
343
343
}
344
344
} ) ;
345
- cmp:: min ( min_indent, spaces )
345
+ cmp:: min ( min_indent, whitespace )
346
346
}
347
347
} ) ;
348
348
@@ -407,4 +407,22 @@ mod unindent_tests {
407
407
let r = unindent ( & s) ;
408
408
assert_eq ! ( r, "line1\n \n line2" ) ;
409
409
}
410
+
411
+ #[ test]
412
+ fn should_unindent_tabs ( ) {
413
+ let s = "\t line1\n \t line2" . to_string ( ) ;
414
+ let r = unindent ( & s) ;
415
+ assert_eq ! ( r, "line1\n line2" ) ;
416
+ }
417
+
418
+ #[ test]
419
+ fn should_trim_mixed_indentation ( ) {
420
+ let s = "\t line1\n \t line2" . to_string ( ) ;
421
+ let r = unindent ( & s) ;
422
+ assert_eq ! ( r, "line1\n line2" ) ;
423
+
424
+ let s = " \t line1\n \t line2" . to_string ( ) ;
425
+ let r = unindent ( & s) ;
426
+ assert_eq ! ( r, "line1\n line2" ) ;
427
+ }
410
428
}
You can’t perform that action at this time.
0 commit comments