wc

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

    wc
    word-count
    simd
    streaming
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    49 minutes ago
    Downloads
    1

    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

    Forks the current totals, word-boundary state, and dispatch into a counter that can be updated or reset independently of self.

    Counter::counts

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

    Returns the totals so far, including a word in progress. The snapshot stays valid across further updates and resets. Reading it leaves the counter usable.

    Counter::new

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

    Starts an empty counter. simd selects the same backend behavior as count(), applied independently to each supplied view.

    Counter::reset

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

    Clears all totals and the preceding byte classification while retaining the selected dispatch. Shared references observe the reset.

    Counter::update

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

    Appends data to this counter. A word can span any number of updates. Reads only the supplied view and retains no bytes. Empty views leave the state intact. Panics before mutation if the byte total exceeds UInt64.

    Counts

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

    Read-only totals for a byte sequence. lines counts LF bytes, words counts runs of bytes separated by ASCII whitespace, and bytes is the length. All three totals 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 or requiring valid text. A final word counts without a trailing separator. Returns zero totals for an empty view.

    simd=true requests vectors on native and Wasm when a complete 16-byte block is present. Short inputs, Wasm GC, and JavaScript use scalar code.