bytecount

    Portable SIMD byte and UTF-8 code point counts adapted from bytecount

    bytecount
    count
    utf8
    simd
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    36 minutes ago
    Downloads
    2

    No dependencies

    #Byte and UTF-8 code point counts

    ///|
    test "count bytes in a view" {
    let input = b"|cat dog fox|"
    assert_eq(@bytecount.count(input[1:12], b' '), 2)
    }

    ///|
    test "count UTF-8 code points" {
    assert_eq(@bytecount.num_chars(b"A\xC3\xA9\xF0\x9F\x98\x80"), 3)
    }

    count

    fn count(data : BytesView, needle : Byte) -> Int

    Counts occurrences of needle in the given byte view.

    The input is borrowed and can include any byte values. The result is zero for an empty view and at most the view length. The implementation selects a vector or scalar kernel according to the backend and length.

    test {
    assert_eq(@bytecount.count(b"one two three", b' '), 2)
    }

    naive_count

    fn naive_count(data : BytesView, needle : Byte) -> Int

    Counts occurrences of needle in a byte view one byte at a time.

    Accepts arbitrary bytes, borrows the view, and returns zero for empty input.

    naive_count_32

    fn naive_count_32(data : BytesView, needle : Byte) -> Int

    Counts occurrences of needle with the same 32-bit loop as naive_count.

    MoonBit view lengths fit in Int32, so the count is exact for every view. The name corresponds to the upstream naive_count_32 entry point.

    naive_num_chars

    fn naive_num_chars(data : BytesView) -> Int

    Counts non-continuation bytes one byte at a time.

    Like num_chars, this works on arbitrary byte views, returns zero for empty input, and counts code points on well-formed UTF-8.

    num_chars

    fn num_chars(data : BytesView) -> Int

    Counts bytes outside the UTF-8 continuation-byte range 0x80..0xBF.

    For well-formed UTF-8, the result is the number of Unicode code points, including a BOM if present. The function reads the view as-is without validation, decoding, or copying. A malformed input still returns the exact number of non-continuation bytes. An empty view returns zero.

    test {
    assert_eq(@bytecount.num_chars(b"A\xC3\xA9\xF0\x9F\x98\x80"), 3)
    }