// The file is shared across many crates, not all have this feature. // If they don't then the tests won't be compiled in, but that's OK, because they are executed at // least in the crate `askama`. There's no need to run the test multiple times. #![allow(unexpected_cfgs)]
use core::{fmt, str};
usecrate::ascii_str::{AsciiChar, AsciiStr};
#[allow(unused)] pub(crate) fn write_escaped_str(mut dest: impl fmt::Write, src: &str) -> fmt::Result { // This implementation reads one byte after another. // It's not very fast, but should work well enough until portable SIMD gets stabilized.
letmut escaped_buf = ESCAPED_BUF_INIT; letmut last = 0;
for (index, byte) in src.bytes().enumerate() { iflet Some(escaped) = get_escaped(byte) {
[escaped_buf[2], escaped_buf[3]] = escaped;
write_str_if_nonempty(&mut dest, &src[last..index])?;
dest.write_str(AsciiStr::from_slice(&escaped_buf[..ESCAPED_BUF_LEN]))?;
last = index + 1;
}
}
write_str_if_nonempty(&mut dest, &src[last..])
}
#[allow(unused)] pub(crate) fn write_escaped_char(mut dest: impl fmt::Write, c: char) -> fmt::Result { if !c.is_ascii() {
dest.write_char(c)
} elseiflet Some(escaped) = get_escaped(c as u8) { letmut escaped_buf = ESCAPED_BUF_INIT;
[escaped_buf[2], escaped_buf[3]] = escaped;
dest.write_str(AsciiStr::from_slice(&escaped_buf[..ESCAPED_BUF_LEN]))
} else { // RATIONALE: `write_char(c)` gets optimized if it is known that `c.is_ascii()`
dest.write_char(c)
}
}
/// Returns the decimal representation of the codepoint if the character needs HTML escaping. #[inline] fn get_escaped(byte: u8) -> Option<[AsciiChar; 2]> { iflet MIN_CHAR..=MAX_CHAR = byte { let entry = TABLE.0[(byte - MIN_CHAR) as usize];
(entry != UNESCAPED).then_some(entry)
} else {
None
}
}
/// List of characters that need HTML escaping, not necessarily in ordinal order. const CHARS: &[u8] = br#""&'<>"#;
/// The character with the lowest codepoint that needs HTML escaping. const MIN_CHAR: u8 = { letmut v = u8::MAX; letmut i = 0; while i < CHARS.len() { if v > CHARS[i] {
v = CHARS[i];
}
i += 1;
}
v
};
/// The character with the highest codepoint that needs HTML escaping. const MAX_CHAR: u8 = { letmut v = u8::MIN; letmut i = 0; while i < CHARS.len() { if v < CHARS[i] {
v = CHARS[i];
}
i += 1;
}
v
};
/// Number of codepoints between the lowest and highest character that needs escaping, incl. const CHAR_RANGE: usize = (MAX_CHAR - MIN_CHAR + 1) as usize;
/// For characters that need HTML escaping, the codepoint is formatted as decimal digits, /// otherwise `b"\0\0"`. Starting at [`MIN_CHAR`]. const TABLE: &Table = &{ letmut table = Table([UNESCAPED; CHAR_RANGE]); letmut i = 0; while i < CHARS.len() { let c = CHARS[i];
table.0[c as u32 as usize - MIN_CHAR as usize] = AsciiChar::two_digits(c as u32);
i += 1;
}
table
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.