///|
/// Returns escaped text from exactly the supplied UTF-16 view.
///
/// Each matching ASCII code unit is replaced once. Non-ASCII code units,
/// surrogate pairs, NUL, and all other unmatched input are preserved without
/// UTF-8 conversion or normalization. The input and escaper are never mutated.
/// When nothing changes, `to_owned()` can reuse a full view's immutable string;
/// a changed result or partial view is materialized. Empty input returns `""`.
/// Allocation limits follow the backend's String/Int32 capacity. `algorithm`
/// controls scanning only and cannot change output or introduce typed errors.
///
/// ```mbt check
/// test {
/// assert_eq(@escape.html5.escape("<😀 & />"), "<😀 & />")
/// assert_eq(@escape.json.escape("a\n\"b"), "a\\n\\\"b")
/// }
/// ```
pub fn Escaper::escape(
self : Escaper,
input : StringView,
algorithm~ : Algorithm = Auto,
) -> String {
let first = if use_simd(algorithm) {
find_text_vector(self, input)
} else {
find_text_scalar(self, input)
}
if first == input.length() {
return input.to_owned()
}
let output = StringBuilder(capacity=input.length())
output.write_view(input[:first])
self.escape_string(input[first:], output, algorithm~)
output.to_string()
}
///|
/// Appends escaped text to `output`, retaining its existing prefix.
///
/// This is the counterpart of upstream `escape_string`. The builder is mutable
/// and has reference semantics: aliases observe appended text. It is never
/// cleared. Empty input leaves it unchanged. Calls on separate chunks concatenate
/// to the whole-input result when StringView boundaries are valid. The escaper
/// and input remain immutable. Non-ASCII code units and replacement strings
/// are written verbatim; replacements are not rescanned. Backend allocation
/// limits apply, and `algorithm` has the same fallback rules as `escape`.
pub fn Escaper::escape_string(
self : Escaper,
input : StringView,
output : StringBuilder,
algorithm~ : Algorithm = Auto,
) -> Unit {
write_text(self, input, output, algorithm)
}
///|
/// Returns escaped bytes, preserving every unmatched byte including invalid UTF-8.
///
/// Replacements use their cached UTF-8 bytes. Input is not decoded, normalized,
/// or mutated, and the escaper retains no input state. Empty input returns empty
/// bytes; an unchanged full view
/// can reuse its immutable backing bytes via `to_owned()`. Changed output and
/// partial views are materialized. Results share no mutable output buffer.
/// `algorithm` selects scanning as for `escape`; backend Int32 allocation
/// limits apply and this method does not raise typed errors.
pub fn Escaper::to_bytes(
self : Escaper,
input : BytesView,
algorithm~ : Algorithm = Auto,
) -> Bytes {
let first = if use_simd(algorithm) {
find_bytes_vector(self, input)
} else {
find_bytes_scalar(self, input)
}
if first == input.length() {
return input.to_owned()
}
let output = @buffer.Buffer(capacity=input.length())
output.write_bytesview(input[:first])
self.escape_bytes(input[first:], output, algorithm~)
output.to_bytes()
}
///|
/// Appends escaped arbitrary bytes to `output`, retaining its existing prefix.
///
/// This matches upstream `escape_bytes`. Every unmatched byte, including NUL
/// and invalid UTF-8, is forwarded unchanged; matching ASCII bytes emit their
/// cached UTF-8 replacement. Replacement output is not rescanned. The mutable
/// Buffer has reference semantics, is never cleared, and aliases observe the
/// append. Empty input leaves it unchanged. Arbitrary byte chunk boundaries
/// produce the same concatenated result as one call. Input and escaper remain
/// immutable. Backend allocation limits and `algorithm` fallbacks still apply.
pub fn Escaper::escape_bytes(
self : Escaper,
input : BytesView,
output : @buffer.Buffer,
algorithm~ : Algorithm = Auto,
) -> Unit {
if use_simd(algorithm) {
write_bytes_vector(self, input, output)
} else {
write_bytes_scalar(self, input, output)
}
}
///|
/// Creates a lazy escaped view for string interpolation or another `Show` sink.
///
/// Construction performs no scan and allocates no escaped text. It retains the
/// immutable input view and escaper. Rendering uses the selected `algorithm`,
/// writes directly to the logger, and never consumes or mutates the value.
/// Empty input renders empty text. Rendering follows the same substitutions,
/// UTF-16 preservation, and allocation limits as `escape`; logger output cannot
/// report the formatter errors exposed by Rust's `Display` API.
///
/// ```mbt check
/// test {
/// let escaped = @escape.html5.escape_fmt("")
/// assert_eq("value: \{escaped}", "value: <a>")
/// assert_eq(escaped.to_string(), "<a>")
/// }
/// ```
pub fn Escaper::escape_fmt(
self : Escaper,
input : StringView,
algorithm~ : Algorithm = Auto,
) -> Escaped {
{ escaper: self, input, algorithm, }
}
///|
/// Renders a lazy value directly into the receiving logger without temporary escaped text.
pub impl Show for Escaped with fn output(self, logger) {
write_text(self.escaper, self.input, logger, self.algorithm)
}
///|
/// Materializes this lazy view as a String through the standard Show renderer.
///
/// Each call independently scans the retained view using its selected
/// algorithm and returns the same escaped content, including empty output.
/// Neither the view nor the escaper is mutated or consumed. Rendering allocates
/// a String; backend allocation limits apply and there are no typed errors.
pub extend Escaped with Show::{to_string}
///|
/// Dispatches text output without converting UTF-16 input to UTF-8.
fn write_text(
escaper : Escaper,
input : StringView,
output : &Logger,
algorithm : Algorithm,
) -> Unit {
if use_simd(algorithm) {
write_text_vector(escaper, input, output)
} else {
write_text_scalar(escaper, input, output)
}
}