@@ -470,24 +470,33 @@ pub(crate) fn split_paths<T: AsRef<std::ffi::OsStr> + ?Sized>(
470470 } )
471471}
472472
473- ///like textwrap.dedent; for processing -c argument
473+ /// Remove common whitespace prefix from all lines in a string.
474+ ///
475+ /// This is like textwrap.dedent, and is used to process -c's code
476+ /// argument. It's different from ruff's dedent, which does not
477+ /// distinguish between tab and space characters when dedenting.
474478fn dedent ( input : & str ) -> String {
475479 let mut prefix: Option < String > = None ;
476480 let isspace = |c| c == ' ' || c == '\t' ;
477481
478- //all -whitespace lines become empty
482+ // All -whitespace lines become empty.
479483 let deblanked: Vec < & str > = input
480484 . lines ( )
481485 . map ( |line| if line. chars ( ) . all ( isspace) { "" } else { line } )
482486 . collect ( ) ;
483487
484- //find maximum common whitespace prefix, if any
488+ // Find maximum common whitespace prefix, if any.
485489 for line in deblanked. iter ( ) {
486490 if line. is_empty ( ) {
487491 continue ;
488492 }
489493 if let Some ( ref mut pstr) = prefix {
490494 for ( i, ( c, pc) ) in line. chars ( ) . zip ( pstr. chars ( ) ) . enumerate ( ) {
495+ // It's okay if `line` is shorter than `pstr`. At
496+ // least one char in `line` must be non-whitespace,
497+ // and if `line` is shorter than `pstr`, this
498+ // non-whitespace char will be compared to a
499+ // whitespace char, and loop will terminate.
491500 if c != pc {
492501 pstr. truncate ( i) ;
493502 break ;
@@ -508,22 +517,22 @@ fn dedent(input: &str) -> String {
508517 }
509518
510519 if let Some ( pstr) = prefix {
511- //strip prefix
520+ // Strip common prefix.
512521 deblanked
513522 . iter ( )
514523 . map ( |line| {
515524 if line. is_empty ( ) {
516525 String :: from ( "" )
517526 } else {
518- //all non-empty lines start with pstr, must be at
519- // least pstr.len() long
527+ // All non-empty lines start with pstr, must be at
528+ // least pstr.len() long.
520529 String :: from ( line. get ( pstr. len ( ) ..) . unwrap ( ) )
521530 }
522531 } )
523532 . collect :: < Vec < _ > > ( )
524533 . join ( "\n " )
525534 } else {
526- //no prefix: all lines blank
535+ // No prefix found : all lines blank.
527536 deblanked. join ( "\n " )
528537 }
529538}
0 commit comments