wc

    Portable SIMD and streaming line, word, and byte counts adapted from fastlwc

    wc
    word-count
    simd
    streaming
    Download zip
    Author
    Version
    0.1.1
    License
    MIT
    Last updated
    2 hours ago
    Downloads
    10

    No dependencies

    #Word counts

    ///|
    test "count lines, words, and bytes" {
    let counts = @wc.count(b"one\ttwo\nthree")
    assert_eq((counts.lines, counts.words, counts.bytes), (1UL, 3UL, 13UL))
    }

    ///|
    test "count a word across chunks" {
    let counter = @wc.Counter::new()
    counter.update(b"par")
    counter.update(b"tial\nline")
    assert_eq(counter.counts(), @wc.count(b"partial\nline"))
    }

    Counter

    type Counter

    Mutable counts for successive views of a single byte stream. Assignment shares the state. Use copy() to fork it and counts() for a read-only snapshot. The counter retains only totals, dispatch, and the trailing whitespace classification.

    Counter::copy

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

    Creates a new counter with the current totals, word-boundary state, and SIMD preference. Supply different suffixes to the returned counter and self to count streams with a shared prefix. Each object can then be updated or reset independently.

    Counter::counts

    fn Counter::counts(self : Counter) -> Counts

    Returns the totals for the concatenation of all supplied views, including a word in progress. The snapshot owns its counts and stays valid across later updates and resets. This read leaves the counter usable for further input.

    test {
    let stream = @wc.Counter::new()
    stream.update(b"par")
    let snapshot = stream.counts()
    stream.update(b"tial\n")
    assert_eq(snapshot.bytes, 3UL)
    assert_eq(stream.counts(), @wc.count(b"partial\n"))
    }

    Counter::new

    fn Counter::new(simd? : Bool) -> Counter

    Creates an independent counter with all totals at zero and the first byte treated as the start of a stream. simd defaults to true and selects the same backend behavior as count() for each supplied view.

    Counter::reset

    fn Counter::reset(self : Counter) -> Unit

    Resets every total to zero and restores the start-of-stream classification. The same object can receive a new independent byte stream using its original SIMD preference. All aliases of self observe the reset.

    Counter::update

    fn Counter::update(self : Counter, data : BytesView) -> Unit

    Appends data to the logical byte stream represented by self. The previous view's trailing whitespace state carries across updates, so one word can span several chunks. Empty views leave the counter as-is. Aliases observe the update. The input is borrowed for the duration of the call. Panics before mutation if the byte total would overflow UInt64.

    Counts

    pub struct Counts {
    lines : UInt64
    words : UInt64
    bytes : UInt64
    } derive(Eq,
    Debug
    )

    Read-only totals returned by count() or snapshotted by Counter::counts(). Fields are independent UInt64 counts. All three are zero for empty input.

    count

    fn count(data : BytesView, simd? : Bool) -> Counts

    Counts LF bytes, ASCII-delimited words, and bytes in data without copying the view. Any byte outside ASCII space and 0x09..0x0D is part of a word, including embedded NUL and high-bit bytes. An unterminated final word counts once, and the empty view returns three zeros.

    The default simd=true requests vectors on native and linear-memory Wasm for views containing at least 16 bytes. Shorter views, Wasm GC, JavaScript, and an explicit simd=false use the scalar implementation.