///|
/// Marks byte candidates with portable lane operations and no table gather or shuffle.
#cfg(any(target="native", target="wasm"))
#inline
fn byte_mask(escaper : Escaper, block : V128) -> Int32 {
let candidates = match escaper.kind {
Html | Html5 => {
// OR-folding matches ", &, ' (also $ and %) and <, > (also =).
// The exact table recheck below the scan rejects these false positives.
let quoted = @simd.i8x16_eq(
@simd.v128_or_(block, @simd.i8x16_splat(5)),
@simd.i8x16_splat(39),
)
let brackets = @simd.i8x16_eq(
@simd.v128_or_(block, @simd.i8x16_splat(2)),
@simd.i8x16_splat(62),
)
let combined = @simd.v128_or_(quoted, brackets)
match escaper.kind {
Html =>
@simd.v128_or_(combined, @simd.i8x16_eq(block, @simd.i8x16_splat(47)))
_ => combined
}
}
Json =>
@simd.v128_or_(
@simd.i8x16_lt_u(block, @simd.i8x16_splat(32)),
@simd.v128_or_(
@simd.i8x16_eq(block, @simd.i8x16_splat(34)),
@simd.i8x16_eq(block, @simd.i8x16_splat(92)),
),
)
Custom | Latex =>
for (lower, upper) in escaper.ranges; mask = @simd.i8x16_splat(0) {
// Unsigned wrapping subtraction includes exactly lower..upper.
// At most three intervals bound the keys; gaps are rechecked scalarly.
let in_range = @simd.i8x16_le_u(
@simd.i8x16_sub(block, @simd.i8x16_splat(lower)),
@simd.i8x16_splat(upper - lower),
)
continue @simd.v128_or_(mask, in_range)
} nobreak {
mask
}
}
@simd.i8x16_bitmask(candidates)
}
///|
/// Marks UTF-16 candidates without narrowing Unicode code units to bytes.
#cfg(any(target="native", target="wasm"))
#inline
fn text_mask(escaper : Escaper, block : V128) -> Int32 {
let candidates = match escaper.kind {
Html | Html5 => {
let quoted = @simd.i16x8_eq(
@simd.v128_or_(block, @simd.i16x8_splat(5)),
@simd.i16x8_splat(39),
)
let brackets = @simd.i16x8_eq(
@simd.v128_or_(block, @simd.i16x8_splat(2)),
@simd.i16x8_splat(62),
)
let combined = @simd.v128_or_(quoted, brackets)
match escaper.kind {
Html =>
@simd.v128_or_(combined, @simd.i16x8_eq(block, @simd.i16x8_splat(47)))
_ => combined
}
}
Json =>
@simd.v128_or_(
@simd.i16x8_lt_u(block, @simd.i16x8_splat(32)),
@simd.v128_or_(
@simd.i16x8_eq(block, @simd.i16x8_splat(34)),
@simd.i16x8_eq(block, @simd.i16x8_splat(92)),
),
)
Custom | Latex =>
for (lower, upper) in escaper.ranges; mask = @simd.i16x8_splat(0) {
let in_range = @simd.i16x8_le_u(
@simd.i16x8_sub(block, @simd.i16x8_splat(lower.to_uint16())),
@simd.i16x8_splat((upper - lower).to_uint16()),
)
continue @simd.v128_or_(mask, in_range)
} nobreak {
mask
}
}
@simd.i16x8_bitmask(candidates)
}