MCPcopy Create free account
hub / github.com/csskit/csskit / fmt_expanded_string

Method fmt_expanded_string

crates/css_lexer/src/source_cursor.rs:471–495  ·  view source on GitHub ↗
(&self, f: &mut Formatter<'_>)

Source from the content-addressed store, hash-verified

469
470 #[cfg(feature = "egg")]
471 fn fmt_expanded_string(&self, f: &mut Formatter<'_>) -> Result {
472 let token = self.token();
473 let inner = &self.source[1..(token.len() as usize) - token.has_close_quote() as usize];
474 // Use the opposite quote style to maximize escaping opportunity
475 let (open_quote, close_quote, escape_char) = match token.quote_style() {
476 QuoteStyle::Single => ('"', '"', '"'),
477 QuoteStyle::Double => ('\'', '\'', '\''),
478 QuoteStyle::None => unreachable!(),
479 };
480 f.write_char(open_quote)?;
481 for c in inner.chars() {
482 if c == escape_char {
483 // Escape the quote character
484 write!(f, "\\{:06x} ", c as u32)?;
485 } else if c.is_ascii() && !c.is_ascii_control() {
486 // Escape all printable ASCII as hex
487 write!(f, "\\{:06x} ", c as u32)?;
488 } else {
489 // Non-ASCII or control chars: write as-is or escape
490 write!(f, "\\{:06x} ", c as u32)?;
491 }
492 }
493 f.write_char(close_quote)?;
494 Ok(())
495 }
496
497 pub fn eq_ignore_ascii_case(&self, other: &str) -> bool {
498 debug_assert!(self.token() != Kind::Delim && self.token() != Kind::Url);

Callers 1

fmtMethod · 0.80

Calls 4

has_close_quoteMethod · 0.80
quote_styleMethod · 0.80
tokenMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected