MCPcopy Create free account
hub / github.com/astral-sh/ruff / raw

Method raw

crates/ruff_python_formatter/src/string/normalize.rs:578–620  ·  view source on GitHub ↗

Computes if a raw string uses the preferred quote. If it does, then it's not possible to change the quote style because it would require escaping which isn't possible in raw strings.

(text: &str, preferred: Quote, triple_quotes: TripleQuotes)

Source from the content-addressed store, hash-verified

576 /// Computes if a raw string uses the preferred quote. If it does, then it's not possible
577 /// to change the quote style because it would require escaping which isn't possible in raw strings.
578 fn raw(text: &str, preferred: Quote, triple_quotes: TripleQuotes) -> Self {
579 let mut chars = text.chars().peekable();
580 let preferred_quote_char = preferred.as_char();
581
582 let contains_unescaped_configured_quotes = loop {
583 match chars.next() {
584 Some('\\') => {
585 // Ignore escaped characters
586 chars.next();
587 }
588 // `"` or `'`
589 Some(c) if c == preferred_quote_char => {
590 if triple_quotes.is_no() {
591 break true;
592 }
593
594 match chars.peek() {
595 // We can't turn `r'''\""'''` into `r"""\"""""`, this would confuse the parser
596 // about where the closing triple quotes start
597 None => break true,
598 Some(next) if *next == preferred_quote_char => {
599 // `""` or `''`
600 chars.next();
601
602 // We can't turn `r'''""'''` into `r""""""""`, nor can we have
603 // `"""` or `'''` respectively inside the string
604 if chars.peek().is_none() || chars.peek() == Some(&preferred_quote_char)
605 {
606 break true;
607 }
608 }
609 _ => {}
610 }
611 }
612 Some(_) => continue,
613 None => break false,
614 }
615 };
616
617 Self::Raw {
618 contains_preferred: contains_unescaped_configured_quotes,
619 }
620 }
621}
622
623#[derive(Debug)]

Callers 2

updateRangesFunction · 0.80
assert_exampleFunction · 0.80

Calls 6

charsMethod · 0.45
as_charMethod · 0.45
nextMethod · 0.45
is_noMethod · 0.45
peekMethod · 0.45
is_noneMethod · 0.45

Tested by 1

assert_exampleFunction · 0.64