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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors

//!
//! A utility to scan for tokens in a byte stream
//!

extern crate alloc;
use alloc::collections::VecDeque;
use std::io::{BufReader, Read};

use crate::read::Buffer;

///
/// What characters are considered "quotes"
#[derive(Debug, Copy, Clone, Default)]
pub enum QuotedChars {
    /// Matches " or '
    #[default]
    SingleOrDoubleQuotes,
    /// Matches only "
    DoubleQuotes,
    /// Matches only '
    SingleQuotes,
    /// Matches only the specified character
    Other(u8),
}

///
/// A token represents a searching string in the input data stream.  If the
/// sequence of bytes `search` is found, then the response will be `response`.
///
/// Can optionally provide an `escape_char`, which will preclude `search` from
/// matching if it is immediately preceded by that character.
///
/// Can optionally provide an `quote_char` to indicate that `search` should be
/// precluded if wrapped in those characters.
#[derive(Clone)]
pub struct Token<T: Clone> {
    search: Vec<u8>,
    response: T,
    escape_char: Option<u8>,
    quote_char: Option<QuotedChars>,
}

impl<T: Clone> Token<T> {
    pub fn new<S: AsRef<[u8]>>(search: S, response: T) -> Self {
        Token {
            search: search.as_ref().to_owned(),
            response,
            escape_char: None,
            quote_char: None,
        }
    }
    #[must_use]
    pub fn with_escape_char(self, escape: u8) -> Self {
        Token {
            search: self.search,
            response: self.response,
            quote_char: self.quote_char,
            escape_char: Some(escape),
        }
    }
    #[must_use]
    pub fn with_quote_char(self, quote_char: QuotedChars) -> Self {
        Token {
            search: self.search,
            response: self.response,
            quote_char: Some(quote_char),
            escape_char: self.escape_char,
        }
    }

    #[must_use]
    pub fn get_search(&self) -> &[u8] {
        self.search.as_ref()
    }

    #[must_use]
    pub fn get_response(&self) -> &T {
        &self.response
    }
}

///
/// Used as a return type to provide:
/// `Found` if a token was found, which token, and where
/// `EndOfData` the scanner hit the end of the buffer/EOF before finding the token
/// `NotFound` if there is no more data in the buffer
pub enum FoundToken<'a, T: Clone> {
    Found { offset: usize, token: &'a Token<T> },
    EndOfData { remaining_length: usize },
    NotFound,
}

///
/// Used as a return type to provide:
/// `Found` if the token was found, which token, and the data preceding it
/// `EndOfData` if the scanner hit EOF and found no token
/// `NotFound` if there is no more data in the boffer
pub enum ReadToken<'a, T: Clone> {
    Found { data: Vec<u8>, token: &'a Token<T> },
    EndOfData { data: Vec<u8> },
    NotFound,
}

struct TokenWorkingMem<'a, T: Clone> {
    token: &'a Token<T>,
    ringbuf: VecDeque<u8>,
    found_escape: bool,
    last_found_quote_char: Option<u8>,
    offset: usize,
}
impl<'a, T: Clone> TokenWorkingMem<'a, T> {
    pub fn new(token: &'a Token<T>) -> Self {
        TokenWorkingMem {
            token,
            ringbuf: VecDeque::with_capacity(token.search.len()),
            found_escape: false,
            last_found_quote_char: None,
            offset: 0,
        }
    }

    /// Is there any unfilled capacity?
    pub fn is_full(&self) -> bool {
        self.ringbuf.capacity() - self.ringbuf.len() == 0
    }

    /// Process the new element
    pub fn push_back(&mut self, elem: u8) {
        if !self.is_full() {
            // it's not full yet, just push the new element in and skip out.
            self.ringbuf.push_back(elem);
            return;
        }
        // we are full, grab the front element, check it for escape and quotes
        // and then append the new guy.
        let ret = self.ringbuf.pop_front();

        if let Some(first) = ret {
            self.offset += 1;
            if let Some(esc) = self.token.escape_char {
                self.found_escape = first == esc;
            }

            if let Some(quoted) = self.token.quote_char {
                //
                // the following logic is a little bit complex, because
                // SingleOrDoubleQuotes can match either ' or ", but we don't
                // want to match the opposite character, IE, a ' won't terminate
                // a " run.

                if let Some(last_char) = self.last_found_quote_char {
                    // if we're searching for the next quote character
                    if last_char == first {
                        // and we've found it - clear the flag
                        self.last_found_quote_char = None;
                    }
                } else if match quoted {
                    // if we're not searching, check against the possible quote
                    // chars to see if we've found a start
                    QuotedChars::SingleOrDoubleQuotes => first == b'\'' || first == b'\"',
                    QuotedChars::DoubleQuotes => first == b'\"',
                    QuotedChars::SingleQuotes => first == b'\'',
                    QuotedChars::Other(o) => first == o,
                } {
                    // we've found a start, flag it.
                    self.last_found_quote_char = Some(first);
                }
            }
        }

        self.ringbuf.push_back(elem);
    }

    /// Have we found the token?
    pub fn matches(&self) -> bool {
        if !self.is_full() {
            return false;
        }
        if self.found_escape {
            // escape character found, we'll never match the token.
            return false;
        }
        if self.last_found_quote_char.is_some() {
            // we're in a quoted sequence, we'll never match the token.
            return false;
        }
        self.ringbuf.iter().eq(&self.token.search)
    }
}

///
/// A Scanner is a forward lookahead struct that scans through an stream of
/// data looking for the indicated tokens.
///
/// The amount of possible forward lookahead is specified by the internal buffer
/// size of the [`BufReader`]
pub struct Scanner<T, R>
where
    T: Read + Sized,
    R: Clone,
{
    reader: Buffer<BufReader<T>>,
    tokens: Vec<Token<R>>,
}

impl<T: Read + Sized, R: Clone> Scanner<T, R> {
    ///
    /// Creates a scanner with the default buffer capacity, 8KB
    pub fn new(input: T, delimiters: &[Token<R>]) -> Self {
        Scanner {
            reader: Buffer::new(BufReader::with_capacity(8 * 1024, input)),
            tokens: Vec::from(delimiters),
        }
    }

    ///
    /// Creates a scanner with the specified buffer capacity
    pub fn with_max_lookahead(input: T, max_buffer: usize, delimiters: &[Token<R>]) -> Self {
        Scanner {
            reader: Buffer::new(BufReader::with_capacity(max_buffer, input)),
            tokens: Vec::from(delimiters),
        }
    }

    ///
    /// Scans through the buffer, looking for the specified token.  Returns the
    /// number of bytes in the stream needed to position the cursor to JUST BEFORE
    /// the token.  I.E., after calling `read_exact(scan_until())`, the next
    /// call to `read()` will return the token itself.
    ///
    /// Returns `Ok(N)` if it found the token in the input stream, or hit the end of the buffer without finding the token
    /// Returns `Ok(None)` if there are no additional characters to read in the buffer - we've hit EOF.
    /// Returns `Err(e)` if there's an error reading from the underlying stream
    pub fn scan_until_next(&mut self) -> Result<FoundToken<R>, std::io::Error> {
        let mut workingmem: Vec<TokenWorkingMem<R>> =
            self.tokens.iter().map(TokenWorkingMem::new).collect();
        let mut num_read = 0;
        for char in &mut self.reader {
            for mem in &mut workingmem {
                mem.push_back(char);

                if mem.matches() {
                    return Ok(FoundToken::Found {
                        offset: mem.offset,
                        token: mem.token,
                    });
                }
            }

            num_read += 1;
        }
        Ok(FoundToken::EndOfData {
            remaining_length: num_read,
        })
    }

    pub fn read_next(&mut self) -> Result<ReadToken<R>, std::io::Error> {
        let mut workingmem: Vec<TokenWorkingMem<R>> =
            self.tokens.iter().map(TokenWorkingMem::new).collect();
        for char in &mut self.reader {
            for mem in &mut workingmem {
                mem.push_back(char);

                if mem.matches() {
                    let buf = self.reader.consume_read_buffer();
                    let mut data: Vec<u8> = buf.into();
                    data.truncate(mem.offset);
                    return Ok(ReadToken::Found {
                        data,
                        token: mem.token,
                    });
                }
            }
        }
        let buf = self.reader.consume_read_buffer();
        if !buf.is_empty() {
            let data: Vec<u8> = buf.into();
            return Ok(ReadToken::EndOfData { data });
        }
        Ok(ReadToken::NotFound)
    }

    pub fn consume(&mut self, len: usize) {
        self.reader.drain(..len);
    }

    pub fn take_back(self) -> Buffer<BufReader<T>> {
        self.reader
    }
}
impl<T: Read + Sized> Scanner<T, LineEnding> {
    pub fn new_lf(input: T) -> Self {
        Scanner {
            reader: Buffer::new(BufReader::with_capacity(8 * 1024, input)),
            tokens: vec![Token::new("\n", LineEnding::LineFeed)],
        }
    }
    pub fn new_crlf(input: T) -> Self {
        Scanner {
            reader: Buffer::new(BufReader::with_capacity(8 * 1024, input)),
            tokens: vec![Token::new("\r\n", LineEnding::CarriageReturnLineFeed)],
        }
    }
    pub fn new_cr(input: T) -> Self {
        Scanner {
            reader: Buffer::new(BufReader::with_capacity(8 * 1024, input)),
            tokens: vec![Token::new("\r", LineEnding::CarriageReturn)],
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LineEnding {
    LineFeed,
    CarriageReturnLineFeed,
    CarriageReturn,
}

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

    #[derive(Copy, Clone, Eq, PartialEq, Debug)]
    enum Tokens {
        Space,
        Other,
    }

    #[test]
    pub fn test_scan_until() -> Result<(), std::io::Error> {
        let data = "this is a basic test\nthis is a second line";

        let delims = &[Token::new(b" ", Tokens::Space)];

        let mut scanner = Scanner::new(data.as_bytes(), delims);

        for exp in [4, 2, 1, 5, 9, 2, 1, 6, 4] {
            match scanner.scan_until_next()? {
                FoundToken::Found { token, offset } => {
                    assert_eq!(offset, exp);
                    assert_eq!(token.response, Tokens::Space);
                    assert_ne!(token.response, Tokens::Other);
                }
                FoundToken::EndOfData { remaining_length } => {
                    assert_eq!(remaining_length, exp);
                }
                FoundToken::NotFound => {
                    panic!("None not expected")
                }
            }
            scanner.consume(exp);
        }

        Ok(())
    }

    #[test]
    pub fn test_scan_escaped() -> Result<(), std::io::Error> {
        let data = "this is a basic \"escaped\\ test\nthis\" is a second line";

        let delims = &[Token::new(b" ", Tokens::Space).with_escape_char(b'\\')];
        let mut scanner = Scanner::new(data.as_bytes(), delims);

        for exp in [4, 2, 1, 5, 20, 2, 1, 6, 4] {
            match scanner.scan_until_next()? {
                FoundToken::Found { token, offset } => {
                    assert_eq!(offset, exp);
                    assert_eq!(token.response, Tokens::Space);
                    assert_ne!(token.response, Tokens::Other);
                }
                FoundToken::EndOfData { .. } => {}
                FoundToken::NotFound => {
                    panic!("None not expected")
                }
            }
            scanner.consume(exp);
        }

        Ok(())
    }

    #[test]
    pub fn test_scan_quoted_double() -> Result<(), std::io::Error> {
        let data = "this is a basic \"escaped\\ test\nthis\" is a second line";
        let delims = &[Token::new(b" ", Tokens::Space).with_quote_char(QuotedChars::DoubleQuotes)];
        let mut scanner = Scanner::new(data.as_bytes(), delims);

        for exp in [4, 2, 1, 5, 20, 2, 1, 6, 4] {
            match scanner.scan_until_next()? {
                FoundToken::Found { token, offset } => {
                    assert_eq!(offset, exp);
                    assert_eq!(token.response, Tokens::Space);
                    assert_ne!(token.response, Tokens::Other);
                }
                FoundToken::EndOfData { .. } => {}
                FoundToken::NotFound => {
                    panic!("None not expected")
                }
            }
            scanner.consume(exp);
        }

        Ok(())
    }
    #[test]
    pub fn test_scan_quoted_single() -> Result<(), std::io::Error> {
        let data = "this is a basic \'escaped\\ test\nthis\' is a second line";
        let delims = &[Token::new(b" ", Tokens::Space).with_quote_char(QuotedChars::SingleQuotes)];

        let mut scanner = Scanner::new(data.as_bytes(), delims);

        for exp in [4, 2, 1, 5, 20, 2, 1, 6, 4] {
            match scanner.scan_until_next()? {
                FoundToken::Found { token, offset } => {
                    assert_eq!(offset, exp);
                    assert_eq!(token.response, Tokens::Space);
                    assert_ne!(token.response, Tokens::Other);
                }
                FoundToken::EndOfData { .. } => {}
                FoundToken::NotFound => {
                    panic!("None not expected")
                }
            }
            scanner.consume(exp);
        }

        Ok(())
    }

    #[test]
    pub fn test_scan_quoted_other() -> Result<(), std::io::Error> {
        let data = "this is a basic |escaped\\ test\nthis| is a second line";

        let delims = &[Token::new(b" ", Tokens::Space).with_quote_char(QuotedChars::Other(b'|'))];
        let mut scanner = Scanner::new(data.as_bytes(), delims);

        for exp in [4, 2, 1, 5, 20, 2, 1, 6, 4] {
            match scanner.scan_until_next()? {
                FoundToken::Found { token, offset } => {
                    assert_eq!(offset, exp);
                    assert_eq!(token.response, Tokens::Space);
                    assert_ne!(token.response, Tokens::Other);
                }
                FoundToken::EndOfData { .. } => {}
                FoundToken::NotFound => {
                    panic!("None not expected")
                }
            }
            scanner.consume(exp);
        }

        Ok(())
    }

    #[test]
    pub fn test_scan_quoted_both() -> Result<(), std::io::Error> {
        let data = "this is a \"more\' advanced\" \'escaped\\ \"test\nthis\' is a second line";
        let delims =
            &[Token::new(b" ", Tokens::Space).with_quote_char(QuotedChars::SingleOrDoubleQuotes)];
        let mut scanner = Scanner::new(data.as_bytes(), delims);

        for exp in [4, 2, 1, 16, 21, 2, 1, 6, 4] {
            match scanner.scan_until_next()? {
                FoundToken::Found { token, offset } => {
                    assert_eq!(offset, exp);
                    assert_eq!(token.response, Tokens::Space);
                    assert_ne!(token.response, Tokens::Other);
                }
                FoundToken::EndOfData { .. } => {}
                FoundToken::NotFound => {
                    panic!("None not expected")
                }
            }
            scanner.consume(exp);
        }

        Ok(())
    }

    #[derive(Copy, Clone, Eq, PartialEq, Debug)]
    enum CSVTokens {
        Field,
        Newline,
    }
    #[test]
    pub fn test_scan_csv() -> Result<(), std::io::Error> {
        let data = "name1,name2,name3,name4\r\nescaped\\,value1,\"quoted,value2\",\'quoted,value3\',\"long value\"\n\n";

        let delims = &[
            Token::new(b",", CSVTokens::Field)
                .with_escape_char(b'\\')
                .with_quote_char(QuotedChars::SingleOrDoubleQuotes),
            Token::new(b"\r\n", CSVTokens::Newline),
            Token::new(b"\n", CSVTokens::Newline),
        ];
        let mut scanner = Scanner::new(data.as_bytes(), delims);

        let exp = &[
            (5, CSVTokens::Field),
            (5, CSVTokens::Field),
            (5, CSVTokens::Field),
            (5, CSVTokens::Newline),
            (15, CSVTokens::Field),
            (15, CSVTokens::Field),
            (15, CSVTokens::Field),
            (12, CSVTokens::Newline),
            (0, CSVTokens::Newline),
        ];

        let mut ctr = 0;
        for (exp_off, exp_ret) in exp {
            let to_consume = match scanner.scan_until_next()? {
                FoundToken::Found { token, offset } => {
                    assert_eq!(offset, *exp_off, "{ctr}{:?}", token.response);
                    assert_eq!(token.response, *exp_ret, "{ctr}");
                    token.search.len()
                }
                FoundToken::EndOfData { .. } => {
                    panic!("EOD Not expected {ctr}")
                }
                FoundToken::NotFound => {
                    panic!("None not expected {ctr}")
                }
            };
            let consumed = exp_off + to_consume;
            scanner.consume(consumed);
            ctr += 1;
        }

        Ok(())
    }

    #[test]
    pub fn test_three_delim() -> Result<(), std::io::Error> {
        let data = "this is a test of the testing test";
        let mut scanner = Scanner::new(data.as_bytes(), &[Token::new("test", "test")]);
        for (exp_off, exp) in &[(10, "test"), (8, "test"), (4, "test")] {
            let to_consume = match scanner.scan_until_next()? {
                FoundToken::Found { offset, token } => {
                    assert_eq!(*exp_off, offset);
                    assert_eq!(*exp, token.response);
                    token.search.len()
                }
                FoundToken::EndOfData { remaining_length } => {
                    assert_eq!(remaining_length, 0);
                    remaining_length
                }
                FoundToken::NotFound => {
                    panic!("Not found");
                }
            };
            scanner.consume(exp_off + to_consume);
        }
        Ok(())
    }
}