memchr

    Portable SIMD byte and substring search, double-ended byte matches, and reusable Two-Way finders ported from memchr

    memchr
    memmem
    substring
    search
    simd
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    49 minutes ago
    Downloads
    1

    No dependencies

    #Checked byte-search examples

    ///|
    test "byte search and view-relative offsets" {
    assert_eq(@memchr.memchr(b'o', b"foo bar foo"), Some(1))
    assert_eq(@memchr.memrchr(b'o', b"foo bar foo"), Some(10))
    assert_eq(@memchr.memchr2(b' ', b'!', b"foo bar"), Some(3))
    assert_eq(@memchr.memchr3(b'x', b'y', b'z', b"xyz"[1:]), Some(0))
    }

    ///|
    test "mixed cursor consumption and independent copies" {
    let matches = @memchr.memchr_iter(b'o', b"foo bar foo")
    assert_eq(matches.next(), Some(1))
    let copy = matches.copy()
    assert_eq(matches.next_back(), Some(10))
    assert_eq(matches.count(), 2)
    assert_eq(copy.iter().to_array(), [2, 9, 10])
    }

    Matches

    pub struct Matches {
    // private fields
    }

    A lazy double-ended byte-match cursor retaining an immutable haystack view. Assignment aliases cursor state; copy() gives independent consumption. Matches are unique even with duplicate needles. Exhaustion is permanent.

    Matches::copy

    fn Matches::copy(self : Matches) -> Matches

    Copies the current progress into an independent mutable cursor. Immutable haystack storage is safely shared; consuming either cursor does not affect the other. Needle values, direction, and SIMD preference are retained.

    Matches::count

    fn Matches::count(self : Matches) -> Int

    Counts and consumes all remaining unique matching byte positions. The result is at most the remaining byte length. Complete vectors or exact SWAR masks count several matches at once; this exhausts all cursor aliases.

    Matches::iter

    fn Matches::iter(self : Matches) -> Iter[Int]

    Returns an iterator that consumes this cursor in its construction direction. Iterator and cursor aliases share progress. Call copy().iter() to iterate independently. The haystack view is retained safely and never modified.

    Matches::next

    fn Matches::next(self : Matches) -> Int?

    Returns and consumes the next match in this cursor's construction direction. Offsets refer to the original view, even after mixed calls to next_back(). Returns None when exhausted and mutates shared aliases; input stays intact.

    Matches::next_back

    fn Matches::next_back(self : Matches) -> Int?

    Returns and consumes a match from the opposite end of this cursor. Mixed front/back consumption cannot repeat a match. Returns None permanently after exhaustion; assignment aliases the same mutable cursor state.

    memchr

    fn memchr(needle : Byte, haystack : BytesView, simd? : Bool) -> Int?

    Returns the first byte offset of needle in haystack, or None if absent. Empty input has no match. Offsets are relative to the supplied view; arbitrary bytes, NUL, and unaligned views are supported without mutation or decoding. simd=true uses complete sixteen-byte vectors on native/linear-memory Wasm; shorter input and Wasm GC use SWAR/tails; JavaScript uses byte comparisons.

    test {
    assert_eq(@memchr.memchr(b'o', b"foo bar"), Some(1))
    assert_eq(@memchr.memchr(b'x', b"foo bar"), None)
    }

    memchr2

    fn memchr2(needle1 : Byte, needle2 : Byte, haystack : BytesView, simd? : Bool) -> Int?

    Returns the first offset matching either byte, or None for absent/empty input. Needle order and duplicates do not affect results. Offsets are relative to haystack; input is never mutated. simd has the same meaning as in memchr.

    memchr2_iter

    fn memchr2_iter(needle1 : Byte, needle2 : Byte, haystack : BytesView, simd? : Bool) -> Matches

    Creates a forward double-ended cursor matching either byte exactly once. Needle order/duplicates are accepted. Empty input has no matches; retained input is immutable and offsets are view-relative. simd follows memchr.

    memchr3

    fn memchr3(needle1 : Byte, needle2 : Byte, needle3 : Byte, haystack : BytesView, simd? : Bool) -> Int?

    Returns the first offset matching any of three bytes, or None if absent. Empty input has no match; duplicates and arbitrary byte values are accepted. Input is read without mutation or overreads. simd follows memchr dispatch.

    memchr3_iter

    fn memchr3_iter(needle1 : Byte, needle2 : Byte, needle3 : Byte, haystack : BytesView, simd? : Bool) -> Matches

    Creates a forward double-ended cursor matching any of three bytes once. Duplicate values never repeat a position. Empty input has no matches; bytes and view-relative offsets are retained without mutation. simd follows memchr.

    memchr_iter

    fn memchr_iter(needle : Byte, haystack : BytesView, simd? : Bool) -> Matches

    Creates a forward double-ended cursor over all occurrences of needle. Empty input yields no matches. Offsets are bytes relative to haystack. Input is retained without copying/mutation; simd follows memchr dispatch.

    memrchr

    fn memrchr(needle : Byte, haystack : BytesView, simd? : Bool) -> Int?

    Returns the last byte offset of needle in haystack, or None if absent. Empty input has no match. Input is read without mutation, decoding, or view overreads. simd requests the same bounded vectors and fallbacks as memchr.

    memrchr2

    fn memrchr2(needle1 : Byte, needle2 : Byte, haystack : BytesView, simd? : Bool) -> Int?

    Returns the last offset matching either byte, or None for absent/empty input. Duplicates are accepted. Only the supplied view is read and offsets are byte offsets within it. simd selects the same vectors/fallbacks as memchr.

    memrchr2_iter

    fn memrchr2_iter(needle1 : Byte, needle2 : Byte, haystack : BytesView, simd? : Bool) -> Matches

    Creates a reverse double-ended cursor matching either byte exactly once. next_back() consumes the forward end. Empty input and duplicate needles follow memchr2_iter; simd requests the same bounded search implementation.

    memrchr3

    fn memrchr3(needle1 : Byte, needle2 : Byte, needle3 : Byte, haystack : BytesView, simd? : Bool) -> Int?

    Returns the last offset matching any of three bytes, or None if absent. Empty input has no match. Duplicate needles produce one match per byte. Offsets are view-relative bytes; simd follows the bounded memchr dispatch.

    memrchr3_iter

    fn memrchr3_iter(needle1 : Byte, needle2 : Byte, needle3 : Byte, haystack : BytesView, simd? : Bool) -> Matches

    Creates a reverse double-ended cursor matching any of three bytes once. next_back() consumes the forward end; duplicates, empty input, safe view retention, and simd dispatch follow memchr3_iter.

    memrchr_iter

    fn memrchr_iter(needle : Byte, haystack : BytesView, simd? : Bool) -> Matches

    Creates a reverse double-ended cursor over all occurrences of needle. next() moves backward; next_back() moves forward. Empty input has no matches. View retention, byte offsets, and simd follow memchr_iter.