(&self, f: &mut Formatter<'_>)
| 334 | } |
| 335 | |
| 336 | fn fmt_compacted_string(&self, f: &mut Formatter<'_>) -> Result { |
| 337 | let token = self.token(); |
| 338 | let inner = &self.source[1..(token.len() as usize) - token.has_close_quote() as usize]; |
| 339 | let quote = match token.quote_style() { |
| 340 | QuoteStyle::Single => '\'', |
| 341 | QuoteStyle::Double => '"', |
| 342 | QuoteStyle::None => unreachable!(), |
| 343 | }; |
| 344 | f.write_char(quote)?; |
| 345 | // Decode escape sequences |
| 346 | let mut chars = inner.chars().peekable(); |
| 347 | let mut i = 0; |
| 348 | while let Some(c) = chars.next() { |
| 349 | if c == '\0' { |
| 350 | write!(f, "{}", REPLACEMENT_CHARACTER)?; |
| 351 | i += 1; |
| 352 | } else if c == '\\' { |
| 353 | i += 1; |
| 354 | let (ch, n) = inner[i..].chars().parse_escape_sequence(); |
| 355 | i += n as usize; |
| 356 | chars = inner[i..].chars().peekable(); |
| 357 | let ch = if ch == '\0' { REPLACEMENT_CHARACTER } else { ch }; |
| 358 | if is_newline(ch) || ch == quote || ch == '\\' { |
| 359 | write!(f, "\\{:x}", ch as u32)?; |
| 360 | // Trailing space needed if next char is a hex digit. |
| 361 | let next_char = chars.peek().copied(); |
| 362 | if next_char.is_some_and(|nc| nc.is_ascii_hexdigit() || nc == ' ' || nc == '\t') { |
| 363 | f.write_char(' ')?; |
| 364 | } |
| 365 | } else { |
| 366 | write!(f, "{}", ch)?; |
| 367 | } |
| 368 | } else { |
| 369 | write!(f, "{}", c)?; |
| 370 | i += c.len_utf8(); |
| 371 | } |
| 372 | } |
| 373 | f.write_char(quote)?; |
| 374 | Ok(()) |
| 375 | } |
| 376 | |
| 377 | #[cfg(feature = "egg")] |
| 378 | fn fmt_expanded(&self, f: &mut Formatter<'_>) -> Result { |
no test coverage detected