MCPcopy Create free account
hub / github.com/Stranger6667/css-inline / write_escaped

Method write_escaped

css-inline/src/html/serializer.rs:348–373  ·  view source on GitHub ↗
(&mut self, text: &str)

Source from the content-addressed store, hash-verified

346 }
347
348 fn write_escaped(&mut self, text: &str) -> Result<(), InlineError> {
349 let mut last_end = 0;
350 for (start, part) in text.match_indices(['&', '\u{00A0}', '<', '>']) {
351 self.writer.write_all(
352 text.get(last_end..start)
353 .expect("Invalid substring")
354 .as_bytes(),
355 )?;
356 // This is slightly faster than matching on `char`
357 // Notably, this approach does not work in `write_attributes` below
358 match (part.as_bytes()[0] & 0b0000_1110) >> 1 {
359 1 => self.writer.write_all(b"&nbsp;")?,
360 3 => self.writer.write_all(b"&amp;")?,
361 6 => self.writer.write_all(b"&lt;")?,
362 7 => self.writer.write_all(b"&gt;")?,
363 _ => unreachable!(),
364 }
365 last_end = start.checked_add(part.len()).expect("Size overflow");
366 }
367 self.writer.write_all(
368 text.get(last_end..text.len())
369 .expect("Invalid substring")
370 .as_bytes(),
371 )?;
372 Ok(())
373 }
374
375 #[allow(clippy::arithmetic_side_effects)]
376 fn write_attributes(&mut self, text: &str) -> Result<(), InlineError> {

Callers 1

write_textMethod · 0.80

Calls 2

as_bytesMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected