///|
/// Selects the implementation used by an escape operation.
///
/// `Scalar` scans individual bytes or UTF-16 code units. `Simd` requests
/// portable V128 scanning on native and linear-memory Wasm; short remainders,
/// Wasm GC, and JavaScript use scalar code. `Auto` uses the scalar default
/// until a complete cross-platform benchmark snapshot establishes a policy.
#derive(Eq, Debug)
pub(all) enum Algorithm {
Auto
Scalar
Simd
}
///|
/// Reports an invalid custom escape table before constructing any public state.
///
/// `EmptyTable` rejects an empty mapping. `InvalidSource` identifies a byte
/// outside 0..126, matching the pinned upstream macro's actual accepted range.
/// `DuplicateSource` identifies a repeated source byte, even when its two
/// replacements are equal. `InvalidReplacement` identifies an unpaired
/// surrogate's UTF-16 offset so construction rejects it equally on all backends.
/// Empty and well-formed Unicode replacement strings are allowed.
#derive(Eq, Debug)
pub(all) suberror EscapeError {
EmptyTable
InvalidSource(Byte)
DuplicateSource(Byte)
InvalidReplacement(source~ : Byte, code_unit_offset~ : Int32)
}
///|
/// Stores one replacement in both text and UTF-8 form outside the scan loop.
priv struct Replacement {
text : String
bytes : Bytes
}
///|
/// Selects exact scalar predicates and compact vector masks for each table kind.
priv enum MaskKind {
Custom
Html
Html5
Json
Latex
}
///|
/// An immutable, reusable mapping from ASCII source bytes to replacement text.
///
/// Only source bytes are substituted, once, from left to right. Replacement
/// text is never scanned again. Text operations preserve every other UTF-16
/// code unit; byte operations preserve every other byte, including invalid
/// UTF-8. Representation is private. Construction copies the mapping and caches
/// UTF-8 replacements, so changing the caller's array cannot affect it.
/// Assignments safely share this immutable state; no reset or copy is needed.
pub struct Escaper {
priv table : FixedArray[Replacement?]
priv ranges : ReadOnlyArray[(Byte, Byte)]
priv kind : MaskKind
}
///|
/// A lazy escaped text view for interpolation and `Show` output.
///
/// Creating this value retains the immutable escaper and input view without
/// scanning or copying input. Each rendering independently escapes that view
/// into the receiving logger; repeated rendering returns the same text. A
/// partial view can keep its entire backing string alive. There is no mutable
/// cursor, and neither rendering nor assignment consumes the value.
pub struct Escaped {
priv escaper : Escaper
priv input : StringView
priv algorithm : Algorithm
}
///|
/// Reports whether this backend contains the portable V128 escape kernels.
///
/// Returns true on native and linear-memory Wasm. This describes compiled
/// support, not optional CPU feature detection or proof that a particular
/// call executes vectors: text needs eight code units and bytes need sixteen
/// bytes for a complete load. This function has no side effects.
#cfg(any(target="native", target="wasm"))
pub fn simd_available() -> Bool {
true
}
///|
/// Reports compiled SIMD support, which is absent on Wasm GC and JavaScript.
///
/// Returns false without allocating or changing state. All algorithm requests
/// on these backends retain identical scalar escaping behavior.
#cfg(not(any(target="native", target="wasm")))
pub fn simd_available() -> Bool {
false
}
///|
/// Keeps automatic dispatch conservative while allowing explicit vector scans.
#inline
fn use_simd(algorithm : Algorithm) -> Bool {
algorithm == Simd && simd_available()
}