#[inline]
pub fn is_interchange_invalid(c: char) -> bool {
let u = c as u32;
if u <= 0x1F {
return u != 0x09 && u != 0x0A && u != 0x0C && u != 0x0D;
}
if matches!(u, 0x7F..=0x9F) {
return true;
}
if matches!(u, 0xFDD0..=0xFDEF) {
return true;
}
let masked = u & 0xFFFF;
masked == 0xFFFE || masked == 0xFFFF
}
const fn build_scan_table(include_html_specials: bool) -> [bool; 256] {
let mut table = [false; 256];
let mut b = 0usize;
while b < 256 {
table[b] = match b {
0x09 | 0x0A | 0x0C | 0x0D => false, 0x00..=0x1F | 0x7F => true, 0x22 | 0x26 | 0x27 | 0x3C | 0x3E => include_html_specials, 0x80..=0xFF => true, _ => false,
};
b += 1;
}
table
}
static HTML_SCAN: [bool; 256] = build_scan_table(true);
static INTERCHANGE_SCAN: [bool; 256] = build_scan_table(false);
const fn build_json_scan_table() -> [bool; 256] {
let mut table = [false; 256];
table[0x3C] = true; table[0x3E] = true; table[0x26] = true; table[0xE2] = true; table
}
static JSON_SCAN: [bool; 256] = build_json_scan_table();
#[cfg(not(feature = "simd"))]
mod chunkscan {
pub const CHUNK: usize = core::mem::size_of::<usize>();
const fn repeat_byte(b: u8) -> usize {
usize::from_ne_bytes([b; CHUNK])
}
const LSB: usize = repeat_byte(0x01);
const MSB: usize = repeat_byte(0x80);
#[inline(always)]
const fn zero_byte_mask(x: usize) -> usize {
x.wrapping_sub(LSB) & !x & MSB
}
#[inline(always)]
const fn eq_mask<const B: u8>(w: usize) -> usize {
zero_byte_mask(w ^ repeat_byte(B))
}
#[inline(always)]
const fn lt_mask<const N: u8>(w: usize) -> usize {
w.wrapping_sub(repeat_byte(N)) & !w & MSB
}
#[inline(always)]
pub fn html_may_flag(chunk: &[u8; CHUNK]) -> bool {
let w = usize::from_ne_bytes(*chunk);
(lt_mask::<0x20>(w)
| (w & MSB)
| eq_mask::<0x7F>(w)
| eq_mask::<b'&'>(w)
| eq_mask::<b'<'>(w)
| eq_mask::<b'>'>(w)
| eq_mask::<b'"'>(w)
| eq_mask::<b'\''>(w))
!= 0
}
#[inline(always)]
pub fn interchange_may_flag(chunk: &[u8; CHUNK]) -> bool {
let w = usize::from_ne_bytes(*chunk);
(lt_mask::<0x20>(w) | (w & MSB) | eq_mask::<0x7F>(w)) != 0
}
#[inline(always)]
pub fn json_may_flag(chunk: &[u8; CHUNK]) -> bool {
let w = usize::from_ne_bytes(*chunk);
(eq_mask::<b'<'>(w) | eq_mask::<b'>'>(w) | eq_mask::<b'&'>(w) | eq_mask::<0xE2>(w)) != 0
}
}
#[cfg(feature = "simd")]
mod chunkscan {
use core::simd::Simd;
use core::simd::cmp::{SimdPartialEq, SimdPartialOrd};
pub const CHUNK: usize = 16;
type V = Simd<u8, CHUNK>;
#[inline(always)]
pub fn html_may_flag(chunk: &[u8; CHUNK]) -> bool {
let v = V::from_array(*chunk);
(v.simd_lt(V::splat(0x20))
| v.simd_ge(V::splat(0x7F))
| v.simd_eq(V::splat(b'&'))
| v.simd_eq(V::splat(b'<'))
| v.simd_eq(V::splat(b'>'))
| v.simd_eq(V::splat(b'"'))
| v.simd_eq(V::splat(b'\'')))
.any()
}
#[inline(always)]
pub fn interchange_may_flag(chunk: &[u8; CHUNK]) -> bool {
let v = V::from_array(*chunk);
(v.simd_lt(V::splat(0x20)) | v.simd_ge(V::splat(0x7F))).any()
}
#[inline(always)]
pub fn json_may_flag(chunk: &[u8; CHUNK]) -> bool {
let v = V::from_array(*chunk);
(v.simd_eq(V::splat(b'<'))
| v.simd_eq(V::splat(b'>'))
| v.simd_eq(V::splat(b'&'))
| v.simd_eq(V::splat(0xE2)))
.any()
}
}
#[inline(always)]
fn find_flagged_chunked<const N: usize>(
bytes: &[u8],
from: usize,
table: &[bool; 256],
chunk_may_flag: impl Fn(&[u8; N]) -> bool,
) -> Option<usize> {
match bytes.get(from) {
Some(&b) if table[b as usize] => return Some(from),
Some(_) => {}
None => return None,
}
let mut i = from + 1;
while let Some(chunk) = bytes.get(i..i + N) {
let Ok(arr) = <&[u8; N]>::try_from(chunk) else {
break;
};
if chunk_may_flag(arr) {
if let Some(p) = chunk.iter().position(|&b| table[b as usize]) {
return Some(i + p);
}
}
i += N;
}
bytes
.get(i..)?
.iter()
.position(|&b| table[b as usize])
.map(|p| i + p)
}
#[inline]
fn find_html_flagged(bytes: &[u8], from: usize) -> Option<usize> {
find_flagged_chunked::<{ chunkscan::CHUNK }>(bytes, from, &HTML_SCAN, chunkscan::html_may_flag)
}
#[inline]
fn find_interchange_flagged(bytes: &[u8], from: usize) -> Option<usize> {
find_flagged_chunked::<{ chunkscan::CHUNK }>(
bytes,
from,
&INTERCHANGE_SCAN,
chunkscan::interchange_may_flag,
)
}
#[inline]
fn find_json_flagged(bytes: &[u8], from: usize) -> Option<usize> {
find_flagged_chunked::<{ chunkscan::CHUNK }>(bytes, from, &JSON_SCAN, chunkscan::json_may_flag)
}
pub fn coerce_to_interchange_valid_to<W: core::fmt::Write>(
text: &str,
mut w: W,
) -> core::fmt::Result {
let bytes = text.as_bytes();
let mut last = 0;
let mut idx = 0;
while let Some(i) = find_interchange_flagged(bytes, idx) {
idx = i;
match bytes.get(idx) {
Some(&b) if b < 0x80 => {
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
w.write_char('\u{FFFD}')?;
idx += 1;
last = idx;
}
Some(_) => {
let Some(c) = text.get(idx..).and_then(|s| s.chars().next()) else {
break;
};
if is_interchange_invalid(c) {
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
w.write_char('\u{FFFD}')?;
idx += c.len_utf8();
last = idx;
} else {
idx += c.len_utf8();
}
}
None => break,
}
}
if last < bytes.len() {
w.write_str(text.get(last..).unwrap_or(""))?;
}
Ok(())
}
pub fn escape_html_and_coerce_to<W: core::fmt::Write>(text: &str, mut w: W) -> core::fmt::Result {
let bytes = text.as_bytes();
let mut last = 0;
let mut idx = 0;
while let Some(i) = find_html_flagged(bytes, idx) {
idx = i;
match bytes.get(idx) {
Some(&b) if b < 0x80 => {
let rep = match b {
b'&' => "&",
b'<' => "<",
b'>' => ">",
b'"' => """,
b'\'' => "'",
_ => "\u{FFFD}",
};
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
w.write_str(rep)?;
idx += 1;
last = idx;
}
Some(_) => {
let Some(c) = text.get(idx..).and_then(|s| s.chars().next()) else {
break;
};
if is_interchange_invalid(c) {
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
w.write_str("\u{FFFD}")?;
idx += c.len_utf8();
last = idx;
} else {
idx += c.len_utf8();
}
}
None => break,
}
}
if last < bytes.len() {
w.write_str(text.get(last..).unwrap_or(""))?;
}
Ok(())
}
pub fn coerce_bytes_to_interchange_valid_to<W: core::fmt::Write>(
bytes: &[u8],
mut w: W,
) -> core::fmt::Result {
for chunk in bytes.utf8_chunks() {
coerce_to_interchange_valid_to(chunk.valid(), &mut w)?;
if !chunk.invalid().is_empty() {
w.write_char('\u{FFFD}')?;
}
}
Ok(())
}
pub fn escape_html_bytes_and_coerce_to<W: core::fmt::Write>(
bytes: &[u8],
mut w: W,
) -> core::fmt::Result {
for chunk in bytes.utf8_chunks() {
escape_html_and_coerce_to(chunk.valid(), &mut w)?;
if !chunk.invalid().is_empty() {
w.write_char('\u{FFFD}')?;
}
}
Ok(())
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn coerce_bytes_to_interchange_valid(bytes: &[u8]) -> alloc::string::String {
let mut s = alloc::string::String::with_capacity(bytes.len());
let _ = coerce_bytes_to_interchange_valid_to(bytes, &mut s);
s
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn escape_html_bytes(bytes: &[u8]) -> alloc::string::String {
let mut s = alloc::string::String::with_capacity(bytes.len());
let _ = escape_html_bytes_and_coerce_to(bytes, &mut s);
s
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn escape_json_for_script(json: &str) -> alloc::string::String {
let mut s = alloc::string::String::with_capacity(json.len());
let _ = escape_json_for_script_to(json, &mut s);
s
}
pub fn escape_allow_character_references_to<W: core::fmt::Write>(
mut text: &str,
mut w: W,
) -> core::fmt::Result {
while let Some(amp_idx) = text.find('&') {
escape_html_and_coerce_to(text.get(..amp_idx).unwrap_or(""), &mut w)?;
text = text.get(amp_idx..).unwrap_or("");
let chars = text.chars().skip(1);
let mut len = 1; let mut has_allowed = false;
let mut terminated = false;
for c in chars {
if c.is_ascii_alphanumeric() || c == '#' {
len += c.len_utf8();
has_allowed = true;
} else if c == ';' {
len += 1;
terminated = true;
break;
} else {
break;
}
}
if terminated && has_allowed {
w.write_str(text.get(..len).unwrap_or(""))?;
text = text.get(len..).unwrap_or("");
} else {
escape_html_and_coerce_to(text.get(..len).unwrap_or(""), &mut w)?;
text = text.get(len..).unwrap_or("");
}
}
escape_html_and_coerce_to(text, &mut w)?;
Ok(())
}
pub fn escape_json_for_script_to<W: core::fmt::Write>(json: &str, mut w: W) -> core::fmt::Result {
let bytes = json.as_bytes();
let mut last = 0;
let mut idx = 0;
while let Some(i) = find_json_flagged(bytes, idx) {
idx = i;
let (rep, skip) = match bytes.get(idx) {
Some(b'<') => ("\\u003c", 1),
Some(b'>') => ("\\u003e", 1),
Some(b'&') => ("\\u0026", 1),
Some(0xE2) => match bytes.get(idx..idx + 3) {
Some([0xE2, 0x80, 0xA8]) => ("\\u2028", 3),
Some([0xE2, 0x80, 0xA9]) => ("\\u2029", 3),
_ => {
idx += 1;
continue;
}
},
_ => break,
};
if idx > last {
w.write_str(json.get(last..idx).unwrap_or(""))?;
}
w.write_str(rep)?;
idx += skip;
last = idx;
}
if last < bytes.len() {
w.write_str(json.get(last..).unwrap_or(""))?;
}
Ok(())
}
#[derive(Debug, Clone, Copy)]
pub struct HtmlEscaped<'a>(&'a str);
pub fn html_escaped(text: &str) -> HtmlEscaped<'_> {
HtmlEscaped(text)
}
impl core::fmt::Display for HtmlEscaped<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
escape_html_and_coerce_to(self.0, f)
}
}
const HEX_LOWER: &[u8; 16] = b"0123456789abcdef";
#[inline]
fn linear_find_flagged(bytes: &[u8], from: usize, table: &[bool; 256]) -> Option<usize> {
bytes
.get(from..)?
.iter()
.position(|&b| table[b as usize])
.map(|p| from + p)
}
const fn build_js_string_scan_table() -> [bool; 256] {
let mut table = [false; 256];
let mut b = 0usize;
while b < 256 {
table[b] = matches!(
b,
0x00..=0x1F | 0x7F | 0x22 | 0x27 | 0x3C | 0x3E | 0x5C | 0x60
);
b += 1;
}
table[0xE2] = true; table
}
static JS_STRING_SCAN: [bool; 256] = build_js_string_scan_table();
#[inline]
fn write_js_hex_escape<W: core::fmt::Write>(w: &mut W, b: u8) -> core::fmt::Result {
w.write_str("\\u00")?;
w.write_char(HEX_LOWER[(b >> 4) as usize] as char)?;
w.write_char(HEX_LOWER[(b & 0x0F) as usize] as char)
}
pub fn escape_js_string_to<W: core::fmt::Write>(text: &str, mut w: W) -> core::fmt::Result {
let bytes = text.as_bytes();
let mut last = 0;
let mut idx = 0;
while let Some(i) = linear_find_flagged(bytes, idx, &JS_STRING_SCAN) {
idx = i;
match bytes.get(idx) {
Some(&b) if b < 0x80 => {
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
match b {
b'\\' => w.write_str("\\\\")?,
b'\n' => w.write_str("\\n")?,
b'\r' => w.write_str("\\r")?,
b'\t' => w.write_str("\\t")?,
_ => write_js_hex_escape(&mut w, b)?,
}
idx += 1;
last = idx;
}
Some(_) => match bytes.get(idx..idx + 3) {
Some([0xE2, 0x80, 0xA8]) | Some([0xE2, 0x80, 0xA9]) => {
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
let Some(&third) = bytes.get(idx + 2) else {
break;
};
w.write_str(if third == 0xA8 { "\\u2028" } else { "\\u2029" })?;
idx += 3;
last = idx;
}
_ => {
idx += 1;
}
},
None => break,
}
}
if last < bytes.len() {
w.write_str(text.get(last..).unwrap_or(""))?;
}
Ok(())
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn escape_js_string(text: &str) -> alloc::string::String {
let mut s = alloc::string::String::with_capacity(text.len());
let _ = escape_js_string_to(text, &mut s);
s
}
#[derive(Debug, Clone, Copy)]
pub struct JsStringEscaped<'a>(&'a str);
pub fn js_string_escaped(text: &str) -> JsStringEscaped<'_> {
JsStringEscaped(text)
}
impl core::fmt::Display for JsStringEscaped<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
escape_js_string_to(self.0, f)
}
}
const fn build_unquoted_attr_scan_table() -> [bool; 256] {
let mut table = build_scan_table(true);
table[0x09] = true; table[0x0A] = true; table[0x0C] = true; table[0x0D] = true; table[0x20] = true; table[0x3D] = true; table[0x60] = true; table
}
static UNQUOTED_ATTR_SCAN: [bool; 256] = build_unquoted_attr_scan_table();
pub fn escape_html_unquoted_attribute_to<W: core::fmt::Write>(
text: &str,
mut w: W,
) -> core::fmt::Result {
let bytes = text.as_bytes();
let mut last = 0;
let mut idx = 0;
while let Some(i) = linear_find_flagged(bytes, idx, &UNQUOTED_ATTR_SCAN) {
idx = i;
match bytes.get(idx) {
Some(&b) if b < 0x80 => {
let rep = match b {
b'&' => "&",
b'<' => "<",
b'>' => ">",
b'"' => """,
b'\'' => "'",
b' ' => " ",
b'\t' => "	",
b'\n' => " ",
0x0C => "",
b'\r' => " ",
b'=' => "=",
b'`' => "`",
_ => "\u{FFFD}",
};
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
w.write_str(rep)?;
idx += 1;
last = idx;
}
Some(_) => {
let Some(c) = text.get(idx..).and_then(|s| s.chars().next()) else {
break;
};
if is_interchange_invalid(c) {
if idx > last {
w.write_str(text.get(last..idx).unwrap_or(""))?;
}
w.write_str("\u{FFFD}")?;
idx += c.len_utf8();
last = idx;
} else {
idx += c.len_utf8();
}
}
None => break,
}
}
if last < bytes.len() {
w.write_str(text.get(last..).unwrap_or(""))?;
}
Ok(())
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn escape_html_unquoted_attribute(text: &str) -> alloc::string::String {
let mut s = alloc::string::String::with_capacity(text.len());
let _ = escape_html_unquoted_attribute_to(text, &mut s);
s
}
#[cfg(test)]
mod tests {
use super::*;
const CHUNK: usize = chunkscan::CHUNK;
type ChunkPredicate = fn(&[u8; CHUNK]) -> bool;
type Finder = fn(&[u8], usize) -> Option<usize>;
#[test]
fn prefilters_cover_tables() {
let pairs: [(&[bool; 256], ChunkPredicate, &str); 3] = [
(&HTML_SCAN, chunkscan::html_may_flag, "html"),
(
&INTERCHANGE_SCAN,
chunkscan::interchange_may_flag,
"interchange",
),
(&JSON_SCAN, chunkscan::json_may_flag, "json"),
];
for (table, prefilter, name) in pairs {
for b in 0..=255u8 {
if !table[b as usize] {
continue;
}
for lane in 0..CHUNK {
let mut chunk = [b'A'; CHUNK];
chunk[lane] = b;
assert!(
prefilter(&chunk),
"{name} prefilter missed flagged byte {b:#04x} in lane {lane}"
);
}
}
}
}
#[test]
fn scan_tables_rebuild_and_match_char_classification() {
assert_eq!(build_scan_table(true), HTML_SCAN);
assert_eq!(build_scan_table(false), INTERCHANGE_SCAN);
assert_eq!(build_json_scan_table(), JSON_SCAN);
assert_eq!(build_js_string_scan_table(), JS_STRING_SCAN);
assert_eq!(build_unquoted_attr_scan_table(), UNQUOTED_ATTR_SCAN);
for b in 0..256 {
assert!(!HTML_SCAN[b] || UNQUOTED_ATTR_SCAN[b]);
}
for b in 0..=0x7Fu8 {
let c = b as char;
let is_special = matches!(c, '&' | '<' | '>' | '"' | '\'');
assert_eq!(
INTERCHANGE_SCAN[b as usize],
is_interchange_invalid(c),
"interchange table disagrees at {b:#04x}"
);
assert_eq!(
HTML_SCAN[b as usize],
is_interchange_invalid(c) || is_special,
"html table disagrees at {b:#04x}"
);
}
for b in 0x80..=0xFFu8 {
assert!(HTML_SCAN[b as usize] && INTERCHANGE_SCAN[b as usize]);
}
for b in 0..=0xFFu8 {
assert_eq!(
JSON_SCAN[b as usize],
matches!(b, b'<' | b'>' | b'&' | 0xE2),
"json table disagrees at {b:#04x}"
);
}
}
#[test]
fn swar_scan_matches_naive_scan() {
let sample = b"plain text, <b>&</b> \x00 ctl \xE2\x80\xA8 unicode \xC3\xBC tail<";
let tables: [(&[bool; 256], Finder); 3] = [
(&HTML_SCAN, find_html_flagged),
(&INTERCHANGE_SCAN, find_interchange_flagged),
(&JSON_SCAN, find_json_flagged),
];
for (table, finder) in tables {
for from in 0..=sample.len() + 2 {
let naive = sample
.get(from..)
.and_then(|t| t.iter().position(|&b| table[b as usize]).map(|p| from + p));
assert_eq!(finder(sample, from), naive, "mismatch at offset {from}");
}
}
}
}