///|
/// Finds the first exact text match with bounded eight-code-unit loads and a scalar tail.
#cfg(any(target="native", target="wasm"))
fn find_text_vector(escaper : Escaper, input : StringView) -> Int32 {
  let data = input.data()
  let start = input.start_offset()
  let length = input.length()
  let offset = for offset = 0; length - offset >= 8; offset = offset + 8 {
    // The guard proves start + offset + 8 <= the actual view's end.
    let block = @simd.v128_load_i16x8(data, start + offset)
    for mask = text_mask(escaper, block); mask != 0; mask = mask & (mask - 1) {
      let index = offset + mask.ctz()
      // Every candidate is <=126; the fixed 128-slot table access is in range.
      if escaper.table[input[index].to_int()] is Some(_) {
        return index
      }
    }
  } nobreak {
    offset
  }
  // offset can fall between surrogate halves. Keep original code-unit indexing:
  // constructing input[offset:] would snap inward and change the returned offset.
  find_text_tail(escaper, input, offset)
}

///|
/// Finds the first exact byte match with bounded sixteen-byte loads and a scalar tail.
#cfg(any(target="native", target="wasm"))
fn find_bytes_vector(escaper : Escaper, input : BytesView) -> Int32 {
  let data = input.data()
  let start = input.start_offset()
  let length = input.length()
  let offset = for offset = 0; length - offset >= 16; offset = offset + 16 {
    // "aligned" names a byte offset; no address-alignment assumption is made.
    // The view-length guard bounds all sixteen bytes, including offset views.
    let block = data.unsafe_extract_v128_aligned(start + offset)
    for mask = byte_mask(escaper, block); mask != 0; mask = mask & (mask - 1) {
      let index = offset + mask.ctz()
      if escaper.table[input[index].to_int()] is Some(_) {
        return index
      }
    }
  } nobreak {
    offset
  }
  offset + find_bytes_scalar(escaper, input[offset:])
}

///|
/// Writes all exact text matches in each vector once, preserving UTF-16 span boundaries.
#cfg(any(target="native", target="wasm"))
fn write_text_vector(
  escaper : Escaper,
  input : StringView,
  output : &Logger,
) -> Unit {
  let data = input.data()
  let start = input.start_offset()
  let length = input.length()
  let (offset, written) = for offset = 0, written = 0; length - offset >= 8; {
    // The guard also bounds views whose prefix/suffix contain escape sentinels.
    let block = @simd.v128_load_i16x8(data, start + offset)
    let written = for mask = text_mask(escaper, block), written = written; mask !=
                     0; {
      let index = offset + mask.ctz()
      let written = match escaper.table[input[index].to_int()] {
        Some(replacement) =>
          write_text_match(input, output, written, index, replacement)
        None => written // Bounding-range and HTML OR-mask false positive.
      }
      continue mask & (mask - 1), written
    } nobreak {
      written
    }
    continue offset + 8, written
  } nobreak {
    (offset, written)
  }
  // The unmatched span can begin before this tail; do not lose it at a block boundary.
  write_text_tail(escaper, input, output, offset, written)
}

///|
/// Writes all exact byte matches in each bounded vector once, then emits the scalar tail.
#cfg(any(target="native", target="wasm"))
fn write_bytes_vector(
  escaper : Escaper,
  input : BytesView,
  output : @buffer.Buffer,
) -> Unit {
  let data = input.data()
  let start = input.start_offset()
  let length = input.length()
  let (offset, written) = for offset = 0, written = 0; length - offset >= 16; {
    let block = data.unsafe_extract_v128_aligned(start + offset)
    let written = for mask = byte_mask(escaper, block), written = written; mask !=
                     0; {
      let index = offset + mask.ctz()
      let written = match escaper.table[input[index].to_int()] {
        Some(replacement) =>
          write_bytes_match(input, output, written, index, replacement)
        None => written
      }
      continue mask & (mask - 1), written
    } nobreak {
      written
    }
    continue offset + 16, written
  } nobreak {
    (offset, written)
  }
  write_bytes_tail(escaper, input, output, offset, written)
}

///|
/// Preserves text search behavior on managed backends without vector declarations.
#cfg(not(any(target="native", target="wasm")))
fn find_text_vector(escaper : Escaper, input : StringView) -> Int32 {
  find_text_scalar(escaper, input)
}

///|
/// Preserves arbitrary-byte search behavior on managed backends.
#cfg(not(any(target="native", target="wasm")))
fn find_bytes_vector(escaper : Escaper, input : BytesView) -> Int32 {
  find_bytes_scalar(escaper, input)
}

///|
/// Preserves text append behavior when a vector request falls back to scalar code.
#cfg(not(any(target="native", target="wasm")))
fn write_text_vector(
  escaper : Escaper,
  input : StringView,
  output : &Logger,
) -> Unit {
  write_text_scalar(escaper, input, output)
}

///|
/// Preserves byte append behavior when a vector request falls back to scalar code.
#cfg(not(any(target="native", target="wasm")))
fn write_bytes_vector(
  escaper : Escaper,
  input : BytesView,
  output : @buffer.Buffer,
) -> Unit {
  write_bytes_scalar(escaper, input, output)
}