@@ -470,24 +470,33 @@ pub(crate) fn split_paths<T: AsRef<std::ffi::OsStr> + ?Sized>( |
470 | 470 | }) |
471 | 471 | } |
472 | 472 |
|
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. |
474 | 478 | fn dedent(input: &str) -> String { |
475 | 479 | let mut prefix: Option<String> = None; |
476 | 480 | let isspace = |c| c == ' ' || c == '\t'; |
477 | 481 |
|
478 | | - //all-whitespace lines become empty |
| 482 | + // All-whitespace lines become empty. |
479 | 483 | let deblanked: Vec<&str> = input |
480 | 484 | .lines() |
481 | 485 | .map(|line| if line.chars().all(isspace) { "" } else { line }) |
482 | 486 | .collect(); |
483 | 487 |
|
484 | | - //find maximum common whitespace prefix, if any |
| 488 | + // Find maximum common whitespace prefix, if any. |
485 | 489 | for line in deblanked.iter() { |
486 | 490 | if line.is_empty() { |
487 | 491 | continue; |
488 | 492 | } |
489 | 493 | if let Some(ref mut pstr) = prefix { |
490 | 494 | 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. |
491 | 500 | if c != pc { |
492 | 501 | pstr.truncate(i); |
493 | 502 | break; |
@@ -508,22 +517,22 @@ fn dedent(input: &str) -> String { |
508 | 517 | } |
509 | 518 |
|
510 | 519 | if let Some(pstr) = prefix { |
511 | | - //strip prefix |
| 520 | + // Strip common prefix. |
512 | 521 | deblanked |
513 | 522 | .iter() |
514 | 523 | .map(|line| { |
515 | 524 | if line.is_empty() { |
516 | 525 | String::from("") |
517 | 526 | } 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. |
520 | 529 | String::from(line.get(pstr.len()..).unwrap()) |
521 | 530 | } |
522 | 531 | }) |
523 | 532 | .collect::<Vec<_>>() |
524 | 533 | .join("\n") |
525 | 534 | } else { |
526 | | - //no prefix: all lines blank |
| 535 | + // No prefix found: all lines blank. |
527 | 536 | deblanked.join("\n") |
528 | 537 | } |
529 | 538 | } |