1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! UTF-8 counters for [wz]
//!
//! [wz]: https://crates.io/crates/wz
#![no_std]
use wz_core::*;

/// Byte counter for UTF-8 encoded byte slices
///
/// ```
/// use wz_core::Counter;
/// use wz_utf8::Bytes;
///
/// let counter = Bytes::default();
/// ```
#[derive(Clone, Debug, Default)]
pub struct Bytes {
    n: usize,
}

impl<T> Counter<T> for Bytes
where
    T: BytesCollector,
{
    fn count(&mut self, bytes: &[u8]) {
        self.n += bytes.len();
    }

    fn output(&self, collector: &mut T) {
        collector.collect(self.n)
    }
}

/// Character counter for UTF-8 encoded byte slices
///
/// ```
/// use wz_core::Counter;
/// use wz_utf8::Chars;
///
/// let counter = Chars::default();
/// ```
#[derive(Clone, Debug, Default)]
pub struct Chars {
    n: usize,
}

impl<T> Counter<T> for Chars
where
    T: CharsCollector,
{
    fn count(&mut self, bytes: &[u8]) {
        self.n += bytecount::num_chars(bytes)
    }

    fn output(&self, count: &mut T) {
        count.collect(self.n);
    }
}

/// Line counter for UTF-8 encoded byte slices
///
/// ```
/// use wz_core::Counter;
/// use wz_utf8::Lines;
///
/// let counter = Lines::with_linebreak(b'\n');
/// ```
#[derive(Clone, Debug)]
pub struct Lines {
    n: usize,
    line_break: u8,
}

impl Lines {
    /// Creates a new Lines counter that counts `line_break` bytes
    pub fn with_linebreak(line_break: u8) -> Self {
        Self { n: 0, line_break }
    }
    /// Creates a new Lines counter that counts `\n` bytes
    pub fn line_feed() -> Self {
        Self::with_linebreak(b'\n')
    }
    /// Creates a new Lines counter that counts `\r` bytes
    pub fn carriage_return() -> Self {
        Self::with_linebreak(b'\r')
    }
}

impl<T> Counter<T> for Lines
where
    T: LinesCollector,
{
    fn count(&mut self, bytes: &[u8]) {
        self.n += bytecount::count(bytes, self.line_break)
    }

    fn output(&self, collector: &mut T) {
        collector.collect(self.n);
    }
}
/// Word counter for UTF-8 encoded byte slices
///
/// A word boundary is defined in `isspace(3)`
///
/// ```
/// use wz_core::Counter;
/// use wz_utf8::Words;
///
/// let counter = Words::default();
/// ```
#[derive(Clone, Debug, Default)]
pub struct Words {
    n: usize,
    on_word: bool,
}

impl<T> Counter<T> for Words
where
    T: WordsCollector,
{
    fn count(&mut self, bytes: &[u8]) {
        *self = bytes.iter().fold(self.clone(), |acc, next| {
            // matches!(x, 0x20 | 0x09 | 0x0A..=0x0D) == ISSPACE
            let on_word = !matches!(next, 0x20 | 0x09 | 0x0A..=0x0D);
            let n = acc.n + usize::from(acc.on_word && !on_word);
            Self { n, on_word }
        });
    }

    fn output(&self, counter: &mut T) {
        counter.collect(self.n + usize::from(self.on_word));
    }
}

/// Max line length counter for UTF-8 encoded byte slices
///
/// ```
/// use wz_core::Counter;
/// use wz_utf8::MaxLineLength;
///
/// let counter = MaxLineLength::with_linebreak(b'\n');
/// ```
#[derive(Clone, Debug)]
pub struct MaxLineLength {
    max: usize,
    character_counter: Chars,
    line_break: u8,
}

impl MaxLineLength {
    /// Creates a new MaxLineLength counter that looks for line_break bytes
    pub fn with_linebreak(line_break: u8) -> Self {
        Self {
            max: 0,
            line_break,
            character_counter: Default::default(),
        }
    }
    /// Creates a new MaxLineLength counter that looks  for '\n'
    pub fn line_feed() -> Self {
        Self::with_linebreak(b'\n')
    }
    /// Creates a new MaxLineLength counter that looks for '\r'
    pub fn carriage_return() -> Self {
        Self::with_linebreak(b'\r')
    }
}

impl<T> Counter<T> for MaxLineLength
where
    T: MaxLineLengthCollector,
{
    fn count(&mut self, input: &[u8]) {
        let mut index = 0;

        while let Some(offset_index) = memchr::memchr(self.line_break, &input[index..]) {
            Counter::<usize>::count(
                &mut self.character_counter,
                &input[index..offset_index + index],
            );
            let mut chars = 0;
            self.character_counter.output(&mut chars);
            index += offset_index + 1;
            self.max = core::cmp::max(self.max, chars);
            self.character_counter = Default::default();
        }
        Counter::<usize>::count(&mut self.character_counter, &input[index..]);
    }

    fn output(&self, collector: &mut T) {
        let mut chars = 0;
        self.character_counter.output(&mut chars);
        let count = core::cmp::max(self.max, chars);
        collector.collect(count)
    }
}