///|
/// Tests exact preset membership or a bounded custom lookup during the initial scan.
#inline
fn matches_scalar(escaper : Escaper, code : Int32) -> Bool {
match escaper.kind {
Html => code is (34 | 38 | 39 | 47 | 60 | 62)
Html5 => code is (34 | 38 | 39 | 60 | 62)
Json => code is (0..<=31 | 34 | 92)
Latex => code is (35..<=38 | 60 | 62 | 92 | 94 | 95 | 123 | 125 | 126)
Custom => code is (0..<=126) && escaper.table[code] is Some(_)
}
}
///|
/// Finds the first mapped code unit without confusing Unicode with its low byte.
fn find_text_scalar(escaper : Escaper, input : StringView) -> Int32 {
find_text_tail(escaper, input, 0)
}
///|
/// Scans an original view's remaining code units without clamping a split surrogate boundary.
fn find_text_tail(
escaper : Escaper,
input : StringView,
start : Int32,
) -> Int32 {
for index in start.. Int32 {
for index in 0.. Int32 {
if written < index {
// index names an ASCII code unit, so this span ends at a valid boundary.
output.write_view(input[written:index])
}
output.write_string(replacement.text)
index + 1
}
///|
/// Writes one byte replacement and its preceding unchanged span.
#inline
fn write_bytes_match(
input : BytesView,
output : @buffer.Buffer,
written : Int32,
index : Int32,
replacement : Replacement,
) -> Int32 {
if written < index {
output.write_bytesview(input[written:index])
}
output.write_bytesview(replacement.bytes)
index + 1
}
///|
/// Escapes a scalar text scan while copying unmatched runs rather than individual characters.
fn write_text_scalar(
escaper : Escaper,
input : StringView,
output : &Logger,
) -> Unit {
write_text_tail(escaper, input, output, 0, 0)
}
///|
/// Completes scalar text output while retaining an unchanged span from a vector prefix.
fn write_text_tail(
escaper : Escaper,
input : StringView,
output : &Logger,
start : Int32,
written : Int32,
) -> Unit {
let written = for index = start, written = written; index < input.length(); {
let unit = input[index].to_int()
if unit < 127 && escaper.table[unit] is Some(replacement) {
continue index + 1,
write_text_match(input, output, written, index, replacement)
} else {
continue index + 1, written
}
} nobreak {
written
}
if written < input.length() {
output.write_view(input[written:])
}
}
///|
/// Escapes arbitrary bytes with a bounded scan and unchanged-run writes.
fn write_bytes_scalar(
escaper : Escaper,
input : BytesView,
output : @buffer.Buffer,
) -> Unit {
write_bytes_tail(escaper, input, output, 0, 0)
}
///|
/// Completes scalar byte output while retaining an unchanged span from a vector prefix.
fn write_bytes_tail(
escaper : Escaper,
input : BytesView,
output : @buffer.Buffer,
start : Int32,
written : Int32,
) -> Unit {
let written = for index = start, written = written; index < input.length(); {
let byte = input[index].to_int()
if byte < 127 && escaper.table[byte] is Some(replacement) {
continue index + 1,
write_bytes_match(input, output, written, index, replacement)
} else {
continue index + 1, written
}
} nobreak {
written
}
if written < input.length() {
output.write_bytesview(input[written:])
}
}