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

Method fmt_compacted_ident

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

Source from the content-addressed store, hash-verified

261 }
262
263 fn fmt_compacted_ident(&self, f: &mut Formatter<'_>) -> Result {
264 let token = self.token();
265 let start = token.leading_len() as usize;
266 let end = self.source.len() - token.trailing_len() as usize;
267 let source = &self.source[start..end];
268
269 match token.kind() {
270 Kind::AtKeyword => f.write_str("@")?,
271 Kind::Hash => f.write_str("#")?,
272 _ => {}
273 }
274
275 let mut chars = source.chars().peekable();
276 let mut i = 0;
277 let mut char_i = 0;
278 while let Some(c) = chars.next() {
279 if c == '\0' {
280 write!(f, "{}", REPLACEMENT_CHARACTER)?;
281 i += 1;
282 } else if c == '\\' {
283 i += 1;
284 let (ch, n) = source[i..].chars().parse_escape_sequence();
285 i += n as usize;
286 chars = source[i..].chars().peekable();
287 let ch = if ch == '\0' { REPLACEMENT_CHARACTER } else { ch };
288 // Check if the decoded character is valid at this position unescaped.
289 let valid_unescaped = if ch == '-' && char_i == 0 {
290 true
291 } else if char_i == 0 || (char_i == 1 && source.starts_with('-')) {
292 is_ident_start(ch)
293 } else {
294 is_ident(ch)
295 };
296 if valid_unescaped {
297 write!(f, "{}", ch)?;
298 } else if !ch.is_ascii_hexdigit() && !ch.is_ascii_whitespace() && !is_newline(ch) {
299 write!(f, "\\{}", ch)?;
300 } else {
301 write!(f, "\\{:x}", ch as u32)?;
302 // A trailing space is needed if the next character is a hex digit
303 // or whitespace, to prevent it from being consumed as part of the escape.
304 let next_char = chars.peek().copied();
305 if next_char.is_some_and(|nc| nc.is_ascii_hexdigit() || nc == ' ' || nc == '\t') {
306 f.write_char(' ')?;
307 }
308 }
309 } else {
310 write!(f, "{}", c)?;
311 i += c.len_utf8();
312 }
313 char_i += 1;
314 }
315
316 if token.kind() == Kind::Function {
317 f.write_str("(")?;
318 }
319
320 Ok(())

Callers 1

fmt_compactedMethod · 0.80

Calls 12

is_ident_startFunction · 0.85
is_identFunction · 0.85
is_newlineFunction · 0.85
leading_lenMethod · 0.80
trailing_lenMethod · 0.80
write_strMethod · 0.80
tokenMethod · 0.45
lenMethod · 0.45
kindMethod · 0.45
nextMethod · 0.45
parse_escape_sequenceMethod · 0.45
peekMethod · 0.45

Tested by

no test coverage detected