Skip to main content

logos_rt_memmem

Function logos_rt_memmem 

Source
pub unsafe extern "C" fn logos_rt_memmem(
    haystack_ptr: i64,
    haystack_len: i64,
    needle_ptr: i64,
    needle_buf_len: i64,
    needle_len: i64,
    start: i64,
    _bound_unused: i64,
) -> i64
Expand description

Count OVERLAPPING occurrences of an ASCII needle in an ASCII haystack, reproducing the LOGOS naive-search nest BIT-IDENTICALLY (the string_search benchmark idiom). The recognizer in adapt_region collapses the per-byte nested-loop region to a single call to this helper.

Parameters mirror the nest’s live values at region entry:

  • haystack_ptr / haystack_len: the pinned text byte buffer (char index == byte index for the ASCII pin) and length of text.
  • needle_ptr / needle_buf_len: the pinned needle byte buffer and its actual length (the pinned len slot).
  • needle_len: the PROGRAM’s needleLen value (the inner loop bound), which may differ from needle_buf_len.
  • start: the 1-based outer index i at region entry.

Semantics, matching the nest while i <= textLen - needleLen + 1 { … }:

  • the outer bound is haystack_len - needle_len + 1 (recomputed here from the SAME inputs the nest used, so it agrees exactly);
  • a position i (1-based) counts iff haystack[i-1 + j] == needle[j] for all j in [0, needle_len) (0-based byte compare);
  • an empty needle (needle_len == 0) matches at EVERY outer position;
  • a needle longer than the haystack (bound < start) yields zero;
  • if a needle index j would read past needle_buf_len (i.e. needle_len > needle_buf_len, the nest’s CHECKED Index on needle), the helper returns LOGOS_MEMMEM_DEOPT WITHOUT scanning — the caller side-exits and bytecode reproduces the exact index out of bounds error. (Text accesses are guaranteed in-bounds by the bound, exactly as the nest’s IndexUnchecked on text relies on.)

Counting is via a first-byte scan + window verify (memchr-style) which is bit-identical to the nested loop: it visits the same positions and applies the same byte equality, just without per-op bytecode dispatch.

§Safety

haystack_ptr must point to haystack_len readable bytes and needle_ptr to needle_buf_len readable bytes, both pinned (borrowed) for the call.