chadtmiller.com/knotless

Git hosted in your atproto account, on any stock PDS, fast enough for agents.

git clone knotless://chadtmiller.com/knotless

needs the knotless git helper. set it up

30 lines · 674 B Raw History
//! Counting, kept apart from reading so it can be tested.

/// What a text holds.
#[derive(Debug, Default, PartialEq)]
pub struct Counts {
    pub lines: usize,
    pub words: usize,
    pub bytes: usize,
}

/// Counts `text` in one pass.
pub fn count(text: &str) -> Counts {
    let mut counts = Counts { bytes: text.len(), ..Counts::default() };
    for line in text.lines() {
        counts.lines += 1;
        counts.words += line.split_whitespace().count();
    }
    counts
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counts() {
        let c = count("one two\nthree\n");
        assert_eq!(c, Counts { lines: 2, words: 3, bytes: 14 });
    }
}