scanner_rust/
scanner_ascii.rs

1use std::{
2    char::REPLACEMENT_CHARACTER,
3    cmp::Ordering,
4    fs::File,
5    intrinsics::copy,
6    io::Read,
7    path::Path,
8    str::{from_utf8_unchecked, FromStr},
9};
10
11use crate::{
12    generic_array::{
13        typenum::{IsGreaterOrEqual, True, U256, U4},
14        ArrayLength, GenericArray,
15    },
16    whitespaces::*,
17    ScannerError,
18};
19
20/// A simple text scanner which can parse primitive types and strings using ASCII.
21#[derive(Educe)]
22#[educe(Debug)]
23pub struct ScannerAscii<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True> = U256> {
24    #[educe(Debug(ignore))]
25    reader:       R,
26    buf:          GenericArray<u8, N>,
27    buf_length:   usize,
28    buf_offset:   usize,
29    passing_byte: Option<u8>,
30}
31
32impl<R: Read> ScannerAscii<R> {
33    /// Create a scanner from a reader.
34    ///
35    /// ```rust
36    /// use std::io;
37    ///
38    /// use scanner_rust::ScannerAscii;
39    ///
40    /// let mut sc = ScannerAscii::new(io::stdin());
41    /// ```
42    #[inline]
43    pub fn new(reader: R) -> ScannerAscii<R> {
44        Self::new2(reader)
45    }
46}
47
48impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
49    /// Create a scanner from a reader and set the buffer size via generics.
50    ///
51    /// ```rust
52    /// use std::io;
53    ///
54    /// use scanner_rust::{generic_array::typenum::U1024, ScannerAscii};
55    ///
56    /// let mut sc: ScannerAscii<_, U1024> = ScannerAscii::new2(io::stdin());
57    /// ```
58    #[inline]
59    pub fn new2(reader: R) -> ScannerAscii<R, N> {
60        ScannerAscii {
61            reader,
62            buf: GenericArray::default(),
63            buf_length: 0,
64            buf_offset: 0,
65            passing_byte: None,
66        }
67    }
68}
69
70impl ScannerAscii<File> {
71    /// Create a scanner to read data from a file by its path.
72    ///
73    /// ```rust
74    /// use scanner_rust::ScannerAscii;
75    ///
76    /// let mut sc = ScannerAscii::scan_path("Cargo.toml").unwrap();
77    /// ```
78    #[inline]
79    pub fn scan_path<P: AsRef<Path>>(path: P) -> Result<ScannerAscii<File>, ScannerError> {
80        Self::scan_path2(path)
81    }
82}
83
84impl<N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<File, N> {
85    /// Create a scanner to read data from a file by its path and set the buffer size via generics.
86    ///
87    /// ```rust
88    /// use scanner_rust::{generic_array::typenum::U1024, ScannerAscii};
89    ///
90    /// let mut sc: ScannerAscii<_, U1024> =
91    ///     ScannerAscii::scan_path2("Cargo.toml").unwrap();
92    /// ```
93    #[inline]
94    pub fn scan_path2<P: AsRef<Path>>(path: P) -> Result<ScannerAscii<File, N>, ScannerError> {
95        let reader = File::open(path)?;
96
97        Ok(ScannerAscii::new2(reader))
98    }
99}
100
101impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
102    #[inline]
103    fn buf_align_to_frond_end(&mut self) {
104        unsafe {
105            copy(self.buf.as_ptr().add(self.buf_offset), self.buf.as_mut_ptr(), self.buf_length);
106        }
107
108        self.buf_offset = 0;
109    }
110
111    #[inline]
112    fn buf_left_shift(&mut self, distance: usize) {
113        debug_assert!(self.buf_length >= distance);
114
115        self.buf_offset += distance;
116
117        if self.buf_offset >= N::USIZE - 4 {
118            self.buf_align_to_frond_end();
119        }
120
121        self.buf_length -= distance;
122    }
123
124    /// Left shift (if necessary) the buffer to remove bytes from the start of the buffer. Typically, you should use this after `peek`ing the buffer.
125    #[inline]
126    #[allow(clippy::missing_safety_doc)]
127    pub unsafe fn remove_heading_bytes_from_buffer(&mut self, number_of_bytes: usize) {
128        self.buf_left_shift(number_of_bytes);
129    }
130
131    fn passing_read(&mut self) -> Result<bool, ScannerError> {
132        if self.buf_length == 0 {
133            let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
134
135            if size == 0 {
136                return Ok(false);
137            }
138
139            self.buf_length += size;
140
141            if let Some(passing_byte) = self.passing_byte.take() {
142                if self.buf[self.buf_offset] == passing_byte {
143                    self.buf_left_shift(1);
144
145                    return if size == 1 {
146                        let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
147
148                        if size == 0 {
149                            Ok(false)
150                        } else {
151                            self.buf_length += size;
152
153                            Ok(true)
154                        }
155                    } else {
156                        Ok(true)
157                    };
158                }
159            }
160
161            Ok(true)
162        } else {
163            Ok(true)
164        }
165    }
166}
167
168impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
169    /// Read the next char. If the data is not a correct char, it will return a `Ok(Some(REPLACEMENT_CHARACTER))` which is �. If there is nothing to read, it will return `Ok(None)`.
170    ///
171    /// ```rust
172    /// use scanner_rust::ScannerAscii;
173    ///
174    /// let mut sc = ScannerAscii::new("5 c ab".as_bytes());
175    ///
176    /// assert_eq!(Some('5'), sc.next_char().unwrap());
177    /// assert_eq!(Some(' '), sc.next_char().unwrap());
178    /// assert_eq!(Some('c'), sc.next_char().unwrap());
179    /// assert_eq!(Some(' '), sc.next_char().unwrap());
180    /// assert_eq!(Some('a'), sc.next_char().unwrap());
181    /// assert_eq!(Some('b'), sc.next_char().unwrap());
182    /// assert_eq!(None, sc.next_char().unwrap());
183    /// ```
184    pub fn next_char(&mut self) -> Result<Option<char>, ScannerError> {
185        if !self.passing_read()? {
186            return Ok(None);
187        }
188
189        let e = self.buf[self.buf_offset];
190
191        self.buf_left_shift(1);
192
193        if e >= 128 {
194            Ok(Some(REPLACEMENT_CHARACTER))
195        } else {
196            Ok(Some(e as char))
197        }
198    }
199
200    /// Read the next line but not include the tailing line character (or line chracters like `CrLf`(`\r\n`)). If there is nothing to read, it will return `Ok(None)`.
201    ///
202    /// ```rust
203    /// use scanner_rust::ScannerAscii;
204    ///
205    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
206    ///
207    /// assert_eq!(Some("123 456".into()), sc.next_line().unwrap());
208    /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
209    /// assert_eq!(Some("".into()), sc.next_line().unwrap());
210    /// assert_eq!(Some(" ab ".into()), sc.next_line().unwrap());
211    /// ```
212    pub fn next_line(&mut self) -> Result<Option<String>, ScannerError> {
213        if !self.passing_read()? {
214            return Ok(None);
215        }
216
217        let mut temp = String::new();
218
219        loop {
220            let e = self.buf[self.buf_offset];
221
222            match e {
223                b'\n' => {
224                    if self.buf_length == 1 {
225                        self.passing_byte = Some(b'\r');
226                        self.buf_left_shift(1);
227                    } else if self.buf[self.buf_offset + 1] == b'\r' {
228                        self.buf_left_shift(2);
229                    } else {
230                        self.buf_left_shift(1);
231                    }
232
233                    return Ok(Some(temp));
234                },
235                b'\r' => {
236                    if self.buf_length == 1 {
237                        self.passing_byte = Some(b'\n');
238                        self.buf_left_shift(1);
239                    } else if self.buf[self.buf_offset + 1] == b'\n' {
240                        self.buf_left_shift(2);
241                    } else {
242                        self.buf_left_shift(1);
243                    }
244
245                    return Ok(Some(temp));
246                },
247                _ => (),
248            }
249
250            self.buf_left_shift(1);
251
252            if e >= 128 {
253                temp.push(REPLACEMENT_CHARACTER);
254            } else {
255                temp.push(e as char);
256            }
257
258            if self.buf_length == 0 {
259                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
260
261                if size == 0 {
262                    return Ok(Some(temp));
263                }
264
265                self.buf_length += size;
266            }
267        }
268    }
269
270    /// Read the next line include the tailing line character (or line chracters like `CrLf`(`\r\n`)) without validating ASCII. If there is nothing to read, it will return `Ok(None)`.
271    ///
272    /// ```rust
273    /// use scanner_rust::ScannerAscii;
274    ///
275    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
276    ///
277    /// assert_eq!(Some("123 456".into()), sc.next_line_raw().unwrap());
278    /// assert_eq!(Some("789 ".into()), sc.next_line_raw().unwrap());
279    /// assert_eq!(Some("".into()), sc.next_line_raw().unwrap());
280    /// assert_eq!(Some(" ab ".into()), sc.next_line_raw().unwrap());
281    /// ```
282    pub fn next_line_raw(&mut self) -> Result<Option<Vec<u8>>, ScannerError> {
283        if !self.passing_read()? {
284            return Ok(None);
285        }
286
287        let mut temp = Vec::new();
288
289        loop {
290            let e = self.buf[self.buf_offset];
291
292            match e {
293                b'\n' => {
294                    if self.buf_length == 1 {
295                        self.passing_byte = Some(b'\r');
296                        self.buf_left_shift(1);
297                    } else if self.buf[self.buf_offset + 1] == b'\r' {
298                        self.buf_left_shift(2);
299                    } else {
300                        self.buf_left_shift(1);
301                    }
302
303                    return Ok(Some(temp));
304                },
305                b'\r' => {
306                    if self.buf_length == 1 {
307                        self.passing_byte = Some(b'\n');
308                        self.buf_left_shift(1);
309                    } else if self.buf[self.buf_offset + 1] == b'\n' {
310                        self.buf_left_shift(2);
311                    } else {
312                        self.buf_left_shift(1);
313                    }
314
315                    return Ok(Some(temp));
316                },
317                _ => (),
318            }
319
320            self.buf_left_shift(1);
321
322            temp.push(e);
323
324            if self.buf_length == 0 {
325                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
326
327                if size == 0 {
328                    return Ok(Some(temp));
329                }
330
331                self.buf_length += size;
332            }
333        }
334    }
335
336    /// Drop the next line but not include the tailing line character (or line chracters like `CrLf`(`\r\n`)). If there is nothing to read, it will return `Ok(None)`. If there are something to read, it will return `Ok(Some(i))`. The `i` is the length of the dropped line.
337    ///
338    /// ```rust
339    /// use scanner_rust::ScannerAscii;
340    ///
341    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
342    ///
343    /// assert_eq!(Some(7), sc.drop_next_line().unwrap());
344    /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
345    /// assert_eq!(Some(0), sc.drop_next_line().unwrap());
346    /// assert_eq!(Some(" ab ".into()), sc.next_line().unwrap());
347    /// assert_eq!(None, sc.drop_next_line().unwrap());
348    /// ```
349    pub fn drop_next_line(&mut self) -> Result<Option<usize>, ScannerError> {
350        if !self.passing_read()? {
351            return Ok(None);
352        }
353
354        let mut c = 0;
355
356        loop {
357            let e = self.buf[self.buf_offset];
358
359            match e {
360                b'\n' => {
361                    if self.buf_length == 1 {
362                        self.passing_byte = Some(b'\r');
363                        self.buf_left_shift(1);
364                    } else if self.buf[self.buf_offset + 1] == b'\r' {
365                        self.buf_left_shift(2);
366                    } else {
367                        self.buf_left_shift(1);
368                    }
369
370                    return Ok(Some(c));
371                },
372                b'\r' => {
373                    if self.buf_length == 1 {
374                        self.passing_byte = Some(b'\n');
375                        self.buf_left_shift(1);
376                    } else if self.buf[self.buf_offset + 1] == b'\n' {
377                        self.buf_left_shift(2);
378                    } else {
379                        self.buf_left_shift(1);
380                    }
381
382                    return Ok(Some(c));
383                },
384                _ => (),
385            }
386
387            self.buf_left_shift(1);
388
389            c += 1;
390
391            if self.buf_length == 0 {
392                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
393
394                if size == 0 {
395                    return Ok(Some(c));
396                }
397
398                self.buf_length += size;
399            }
400        }
401    }
402}
403
404impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
405    /// Skip the next whitespaces (`javaWhitespace`). If there is nothing to read, it will return `Ok(false)`.
406    ///
407    /// ```rust
408    /// use scanner_rust::ScannerAscii;
409    ///
410    /// let mut sc = ScannerAscii::new("1 2   c".as_bytes());
411    ///
412    /// assert_eq!(Some('1'), sc.next_char().unwrap());
413    /// assert_eq!(Some(' '), sc.next_char().unwrap());
414    /// assert_eq!(Some('2'), sc.next_char().unwrap());
415    /// assert_eq!(true, sc.skip_whitespaces().unwrap());
416    /// assert_eq!(Some('c'), sc.next_char().unwrap());
417    /// assert_eq!(false, sc.skip_whitespaces().unwrap());
418    /// ```
419    pub fn skip_whitespaces(&mut self) -> Result<bool, ScannerError> {
420        if !self.passing_read()? {
421            return Ok(false);
422        }
423
424        loop {
425            if !is_whitespace_1(self.buf[self.buf_offset]) {
426                break;
427            }
428
429            self.buf_left_shift(1);
430
431            if self.buf_length == 0 {
432                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
433
434                if size == 0 {
435                    return Ok(true);
436                }
437
438                self.buf_length += size;
439            }
440        }
441
442        Ok(true)
443    }
444
445    /// Read the next token separated by whitespaces. If there is nothing to read, it will return `Ok(None)`.
446    ///
447    /// ```rust
448    /// use scanner_rust::ScannerAscii;
449    ///
450    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
451    ///
452    /// assert_eq!(Some("123".into()), sc.next().unwrap());
453    /// assert_eq!(Some("456".into()), sc.next().unwrap());
454    /// assert_eq!(Some("789".into()), sc.next().unwrap());
455    /// assert_eq!(Some("ab".into()), sc.next().unwrap());
456    /// assert_eq!(None, sc.next().unwrap());
457    /// ```
458    #[allow(clippy::should_implement_trait)]
459    pub fn next(&mut self) -> Result<Option<String>, ScannerError> {
460        if !self.skip_whitespaces()? {
461            return Ok(None);
462        }
463
464        if self.buf_length == 0 {
465            let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
466
467            if size == 0 {
468                return Ok(None);
469            }
470
471            self.buf_length += size;
472        }
473
474        let mut temp = String::new();
475
476        loop {
477            let e = self.buf[self.buf_offset];
478
479            if is_whitespace_1(e) {
480                return Ok(Some(temp));
481            }
482
483            self.buf_left_shift(1);
484
485            if e >= 128 {
486                temp.push(REPLACEMENT_CHARACTER);
487            } else {
488                temp.push(e as char);
489            }
490
491            if self.buf_length == 0 {
492                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
493
494                if size == 0 {
495                    return Ok(Some(temp));
496                }
497
498                self.buf_length += size;
499            }
500        }
501    }
502
503    /// Read the next token separated by whitespaces without validating ASCII. If there is nothing to read, it will return `Ok(None)`.
504    ///
505    /// ```rust
506    /// use scanner_rust::ScannerAscii;
507    ///
508    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
509    ///
510    /// assert_eq!(Some("123".into()), sc.next_raw().unwrap());
511    /// assert_eq!(Some("456".into()), sc.next_raw().unwrap());
512    /// assert_eq!(Some("789".into()), sc.next_raw().unwrap());
513    /// assert_eq!(Some("ab".into()), sc.next_raw().unwrap());
514    /// assert_eq!(None, sc.next_raw().unwrap());
515    /// ```
516    pub fn next_raw(&mut self) -> Result<Option<Vec<u8>>, ScannerError> {
517        if !self.skip_whitespaces()? {
518            return Ok(None);
519        }
520
521        if self.buf_length == 0 {
522            let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
523
524            if size == 0 {
525                return Ok(None);
526            }
527
528            self.buf_length += size;
529        }
530
531        let mut temp = Vec::new();
532
533        loop {
534            let e = self.buf[self.buf_offset];
535
536            if is_whitespace_1(e) {
537                return Ok(Some(temp));
538            }
539
540            self.buf_left_shift(1);
541
542            temp.push(e);
543
544            if self.buf_length == 0 {
545                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
546
547                if size == 0 {
548                    return Ok(Some(temp));
549                }
550
551                self.buf_length += size;
552            }
553        }
554    }
555
556    /// Drop the next token separated by whitespaces. If there is nothing to read, it will return `Ok(None)`. If there are something to read, it will return `Ok(Some(i))`. The `i` is the length of the dropped line.
557    ///
558    /// ```rust
559    /// use scanner_rust::ScannerAscii;
560    ///
561    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
562    ///
563    /// assert_eq!(Some(3), sc.drop_next().unwrap());
564    /// assert_eq!(Some("456".into()), sc.next().unwrap());
565    /// assert_eq!(Some(3), sc.drop_next().unwrap());
566    /// assert_eq!(Some("ab".into()), sc.next().unwrap());
567    /// assert_eq!(None, sc.drop_next().unwrap());
568    /// ```
569    pub fn drop_next(&mut self) -> Result<Option<usize>, ScannerError> {
570        if !self.skip_whitespaces()? {
571            return Ok(None);
572        }
573
574        if self.buf_length == 0 {
575            let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
576
577            if size == 0 {
578                return Ok(None);
579            }
580
581            self.buf_length += size;
582        }
583
584        let mut c = 0;
585
586        loop {
587            if is_whitespace_1(self.buf[self.buf_offset]) {
588                return Ok(Some(c));
589            }
590
591            self.buf_left_shift(1);
592
593            c += 1;
594
595            if self.buf_length == 0 {
596                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
597
598                if size == 0 {
599                    return Ok(Some(c));
600                }
601
602                self.buf_length += size;
603            }
604        }
605    }
606}
607
608impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
609    /// Read the next bytes. If there is nothing to read, it will return `Ok(None)`.
610    ///
611    /// ```rust
612    /// use scanner_rust::ScannerAscii;
613    ///
614    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
615    ///
616    /// assert_eq!(Some("123".into()), sc.next_bytes(3).unwrap());
617    /// assert_eq!(Some(" 456".into()), sc.next_bytes(4).unwrap());
618    /// assert_eq!(Some("\r\n789 ".into()), sc.next_bytes(6).unwrap());
619    /// assert_eq!(Some("ab".into()), sc.next_raw().unwrap());
620    /// assert_eq!(Some(" ".into()), sc.next_bytes(2).unwrap());
621    /// assert_eq!(None, sc.next_bytes(2).unwrap());
622    /// ```
623    pub fn next_bytes(
624        &mut self,
625        max_number_of_bytes: usize,
626    ) -> Result<Option<Vec<u8>>, ScannerError> {
627        if !self.passing_read()? {
628            return Ok(None);
629        }
630
631        let mut temp = Vec::new();
632        let mut c = 0;
633
634        while c < max_number_of_bytes {
635            if self.buf_length == 0 {
636                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
637
638                if size == 0 {
639                    return Ok(Some(temp));
640                }
641
642                self.buf_length += size;
643            }
644
645            let dropping_bytes = self.buf_length.min(max_number_of_bytes - c);
646
647            temp.extend_from_slice(&self.buf[self.buf_offset..(self.buf_offset + dropping_bytes)]);
648
649            self.buf_left_shift(dropping_bytes);
650
651            c += dropping_bytes;
652        }
653
654        Ok(Some(temp))
655    }
656
657    /// Drop the next N bytes. If there is nothing to read, it will return `Ok(None)`. If there are something to read, it will return `Ok(Some(i))`. The `i` is the length of the actually dropped bytes.
658    ///
659    /// ```rust
660    /// use scanner_rust::ScannerAscii;
661    ///
662    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
663    ///
664    /// assert_eq!(Some(7), sc.drop_next_bytes(7).unwrap());
665    /// assert_eq!(Some("".into()), sc.next_line().unwrap());
666    /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
667    /// assert_eq!(Some(1), sc.drop_next_bytes(1).unwrap());
668    /// assert_eq!(Some(" ab ".into()), sc.next_line().unwrap());
669    /// assert_eq!(None, sc.drop_next_bytes(1).unwrap());
670    /// ```
671    pub fn drop_next_bytes(
672        &mut self,
673        max_number_of_bytes: usize,
674    ) -> Result<Option<usize>, ScannerError> {
675        if !self.passing_read()? {
676            return Ok(None);
677        }
678
679        let mut c = 0;
680
681        while c < max_number_of_bytes {
682            if self.buf_length == 0 {
683                let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
684
685                if size == 0 {
686                    return Ok(Some(c));
687                }
688
689                self.buf_length += size;
690            }
691
692            let dropping_bytes = self.buf_length.min(max_number_of_bytes - c);
693
694            self.buf_left_shift(dropping_bytes);
695
696            c += dropping_bytes;
697        }
698
699        Ok(Some(c))
700    }
701}
702
703impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
704    /// Read the next text until it reaches a specific boundary. If there is nothing to read, it will return `Ok(None)`.
705    ///
706    /// ```rust
707    /// use scanner_rust::ScannerAscii;
708    ///
709    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
710    ///
711    /// assert_eq!(Some("123".into()), sc.next_until(" ").unwrap());
712    /// assert_eq!(Some("456\r".into()), sc.next_until("\n").unwrap());
713    /// assert_eq!(Some("78".into()), sc.next_until("9 ").unwrap());
714    /// assert_eq!(Some("\n\n ab ".into()), sc.next_until("kk").unwrap());
715    /// assert_eq!(None, sc.next().unwrap());
716    /// ```
717    pub fn next_until<S: AsRef<str>>(
718        &mut self,
719        boundary: S,
720    ) -> Result<Option<String>, ScannerError> {
721        if !self.passing_read()? {
722            return Ok(None);
723        }
724
725        let boundary = boundary.as_ref().as_bytes();
726        let boundary_length = boundary.len();
727        let mut temp = String::new();
728
729        let mut b = 0;
730
731        loop {
732            let mut p = 0;
733
734            while p < self.buf_length {
735                if self.buf[self.buf_offset + p] == boundary[b] {
736                    b += 1;
737                    p += 1;
738
739                    if b == boundary_length {
740                        match p.cmp(&boundary_length) {
741                            Ordering::Equal => (),
742                            Ordering::Greater => {
743                                temp.push_str(
744                                    String::from_utf8_lossy(
745                                        &self.buf[self.buf_offset
746                                            ..(self.buf_offset + p - boundary_length)],
747                                    )
748                                    .as_ref(),
749                                );
750                            },
751                            Ordering::Less => {
752                                let adjusted_temp_length = temp.len() - (boundary_length - p);
753
754                                unsafe {
755                                    temp.as_mut_vec().set_len(adjusted_temp_length);
756                                }
757                            },
758                        }
759
760                        self.buf_left_shift(p);
761
762                        return Ok(Some(temp));
763                    }
764                } else {
765                    b = 0;
766                    p += 1;
767                }
768            }
769
770            temp.push_str(
771                String::from_utf8_lossy(
772                    &self.buf[self.buf_offset..(self.buf_offset + self.buf_length)],
773                )
774                .as_ref(),
775            );
776
777            self.buf_left_shift(self.buf_length);
778
779            let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
780
781            if size == 0 {
782                return Ok(Some(temp));
783            }
784
785            self.buf_length += size;
786        }
787    }
788
789    /// Read the next data until it reaches a specific boundary without fully validating UTF-8. If there is nothing to read, it will return `Ok(None)`.
790    ///
791    /// ```rust
792    /// use scanner_rust::ScannerAscii;
793    ///
794    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
795    ///
796    /// assert_eq!(Some("123".into()), sc.next_until_raw(" ").unwrap());
797    /// assert_eq!(Some("456\r".into()), sc.next_until_raw("\n").unwrap());
798    /// assert_eq!(Some("78".into()), sc.next_until_raw("9 ").unwrap());
799    /// assert_eq!(Some("\n\n ab ".into()), sc.next_until_raw("kk").unwrap());
800    /// assert_eq!(None, sc.next().unwrap());
801    /// ```
802    pub fn next_until_raw<D: ?Sized + AsRef<[u8]>>(
803        &mut self,
804        boundary: &D,
805    ) -> Result<Option<Vec<u8>>, ScannerError> {
806        if !self.passing_read()? {
807            return Ok(None);
808        }
809
810        let boundary = boundary.as_ref();
811        let boundary_length = boundary.len();
812        let mut temp = Vec::new();
813
814        let mut b = 0;
815
816        loop {
817            let mut p = 0;
818
819            while p < self.buf_length {
820                if self.buf[self.buf_offset + p] == boundary[b] {
821                    b += 1;
822                    p += 1;
823
824                    if b == boundary_length {
825                        match p.cmp(&boundary_length) {
826                            Ordering::Equal => (),
827                            Ordering::Greater => {
828                                temp.extend_from_slice(
829                                    &self.buf
830                                        [self.buf_offset..(self.buf_offset + p - boundary_length)],
831                                );
832                            },
833                            Ordering::Less => {
834                                let adjusted_temp_length = temp.len() - (boundary_length - p);
835
836                                unsafe {
837                                    temp.set_len(adjusted_temp_length);
838                                }
839                            },
840                        }
841
842                        self.buf_left_shift(p);
843
844                        return Ok(Some(temp));
845                    }
846                } else {
847                    b = 0;
848                    p += 1;
849                }
850            }
851
852            temp.extend_from_slice(&self.buf[self.buf_offset..(self.buf_offset + self.buf_length)]);
853
854            self.buf_left_shift(self.buf_length);
855
856            let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
857
858            if size == 0 {
859                return Ok(Some(temp));
860            }
861
862            self.buf_length += size;
863        }
864    }
865
866    /// Drop the next data until it reaches a specific boundary. If there is nothing to read, it will return `Ok(None)`.
867    ///
868    /// ```rust
869    /// use scanner_rust::ScannerAscii;
870    ///
871    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
872    ///
873    /// assert_eq!(Some(7), sc.drop_next_until("\r\n").unwrap());
874    /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
875    /// assert_eq!(Some(0), sc.drop_next_until("\n").unwrap());
876    /// assert_eq!(Some(" ab ".into()), sc.next_line().unwrap());
877    /// assert_eq!(None, sc.drop_next_until("").unwrap());
878    /// ```
879    pub fn drop_next_until<D: ?Sized + AsRef<[u8]>>(
880        &mut self,
881        boundary: &D,
882    ) -> Result<Option<usize>, ScannerError> {
883        if !self.passing_read()? {
884            return Ok(None);
885        }
886
887        let boundary = boundary.as_ref();
888        let boundary_length = boundary.len();
889        let mut c = 0;
890
891        let mut b = 0;
892
893        loop {
894            let mut p = 0;
895
896            while p < self.buf_length {
897                if self.buf[self.buf_offset + p] == boundary[b] {
898                    b += 1;
899                    p += 1;
900
901                    if b == boundary_length {
902                        match p.cmp(&boundary_length) {
903                            Ordering::Equal => (),
904                            Ordering::Greater => {
905                                c += p - boundary_length;
906                            },
907                            Ordering::Less => {
908                                c -= boundary_length - p;
909                            },
910                        }
911
912                        self.buf_left_shift(p);
913
914                        return Ok(Some(c));
915                    }
916                } else {
917                    b = 0;
918                    p += 1;
919                }
920            }
921
922            c += self.buf_length;
923
924            self.buf_left_shift(self.buf_length);
925
926            let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
927
928            if size == 0 {
929                return Ok(Some(c));
930            }
931
932            self.buf_length += size;
933        }
934    }
935}
936
937impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
938    /// Try to fill up the buffer and return the immutable byte slice of the valid buffered data.
939    /// If the `shift` parameter is set to `false`, the guaranteed minimum data length of the result is **32** (if the unread data is long enough), otherwise it is `BUFFER_SIZE`.
940    ///
941    /// ```rust
942    /// use scanner_rust::ScannerAscii;
943    ///
944    /// let mut sc = ScannerAscii::new("123 456\r\n789 \n\n ab ".as_bytes());
945    ///
946    /// assert_eq!("123 456\r\n789 \n\n ab ".as_bytes(), sc.peek(false).unwrap());
947    /// ```
948    #[inline]
949    pub fn peek(&mut self, shift: bool) -> Result<&[u8], ScannerError> {
950        if shift {
951            self.buf_align_to_frond_end();
952        }
953
954        loop {
955            let size = self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])?;
956
957            if size == 0 {
958                break;
959            }
960
961            self.buf_length += size;
962        }
963
964        Ok(&self.buf[self.buf_offset..(self.buf_offset + self.buf_length)])
965    }
966}
967
968impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
969    #[inline]
970    fn next_raw_parse<T: FromStr>(&mut self) -> Result<Option<T>, ScannerError>
971    where
972        ScannerError: From<<T as FromStr>::Err>, {
973        let result = self.next_raw()?;
974
975        match result {
976            Some(s) => Ok(Some(unsafe { from_utf8_unchecked(&s) }.parse()?)),
977            None => Ok(None),
978        }
979    }
980
981    /// Read the next token separated by whitespaces and parse it to a `u8` value. If there is nothing to read, it will return `Ok(None)`.
982    ///
983    /// ```rust
984    /// use scanner_rust::Scanner;
985    ///
986    /// let mut sc = Scanner::new("1 2".as_bytes());
987    ///
988    /// assert_eq!(Some(1), sc.next_u8().unwrap());
989    /// assert_eq!(Some(2), sc.next_u8().unwrap());
990    /// ```
991    #[inline]
992    pub fn next_u8(&mut self) -> Result<Option<u8>, ScannerError> {
993        self.next_raw_parse()
994    }
995
996    /// Read the next token separated by whitespaces and parse it to a `u16` value. If there is nothing to read, it will return `Ok(None)`.
997    ///
998    /// ```rust
999    /// use scanner_rust::Scanner;
1000    ///
1001    /// let mut sc = Scanner::new("1 2".as_bytes());
1002    ///
1003    /// assert_eq!(Some(1), sc.next_u16().unwrap());
1004    /// assert_eq!(Some(2), sc.next_u16().unwrap());
1005    /// ```
1006    #[inline]
1007    pub fn next_u16(&mut self) -> Result<Option<u16>, ScannerError> {
1008        self.next_raw_parse()
1009    }
1010
1011    /// Read the next token separated by whitespaces and parse it to a `u32` value. If there is nothing to read, it will return `Ok(None)`.
1012    ///
1013    /// ```rust
1014    /// use scanner_rust::Scanner;
1015    ///
1016    /// let mut sc = Scanner::new("1 2".as_bytes());
1017    ///
1018    /// assert_eq!(Some(1), sc.next_u32().unwrap());
1019    /// assert_eq!(Some(2), sc.next_u32().unwrap());
1020    /// ```
1021    #[inline]
1022    pub fn next_u32(&mut self) -> Result<Option<u32>, ScannerError> {
1023        self.next_raw_parse()
1024    }
1025
1026    /// Read the next token separated by whitespaces and parse it to a `u64` value. If there is nothing to read, it will return `Ok(None)`.
1027    ///
1028    /// ```rust
1029    /// use scanner_rust::Scanner;
1030    ///
1031    /// let mut sc = Scanner::new("1 2".as_bytes());
1032    ///
1033    /// assert_eq!(Some(1), sc.next_u64().unwrap());
1034    /// assert_eq!(Some(2), sc.next_u64().unwrap());
1035    /// ```
1036    #[inline]
1037    pub fn next_u64(&mut self) -> Result<Option<u64>, ScannerError> {
1038        self.next_raw_parse()
1039    }
1040
1041    /// Read the next token separated by whitespaces and parse it to a `u128` value. If there is nothing to read, it will return `Ok(None)`.
1042    ///
1043    /// ```rust
1044    /// use scanner_rust::Scanner;
1045    ///
1046    /// let mut sc = Scanner::new("1 2".as_bytes());
1047    ///
1048    /// assert_eq!(Some(1), sc.next_u128().unwrap());
1049    /// assert_eq!(Some(2), sc.next_u128().unwrap());
1050    /// ```
1051    #[inline]
1052    pub fn next_u128(&mut self) -> Result<Option<u128>, ScannerError> {
1053        self.next_raw_parse()
1054    }
1055
1056    /// Read the next token separated by whitespaces and parse it to a `usize` value. If there is nothing to read, it will return `Ok(None)`.
1057    ///
1058    /// ```rust
1059    /// use scanner_rust::Scanner;
1060    ///
1061    /// let mut sc = Scanner::new("1 2".as_bytes());
1062    ///
1063    /// assert_eq!(Some(1), sc.next_usize().unwrap());
1064    /// assert_eq!(Some(2), sc.next_usize().unwrap());
1065    /// ```
1066    #[inline]
1067    pub fn next_usize(&mut self) -> Result<Option<usize>, ScannerError> {
1068        self.next_raw_parse()
1069    }
1070
1071    /// Read the next token separated by whitespaces and parse it to a `i8` value. If there is nothing to read, it will return `Ok(None)`.
1072    ///
1073    /// ```rust
1074    /// use scanner_rust::Scanner;
1075    ///
1076    /// let mut sc = Scanner::new("1 2".as_bytes());
1077    ///
1078    /// assert_eq!(Some(1), sc.next_i8().unwrap());
1079    /// assert_eq!(Some(2), sc.next_i8().unwrap());
1080    /// ```
1081    #[inline]
1082    pub fn next_i8(&mut self) -> Result<Option<i8>, ScannerError> {
1083        self.next_raw_parse()
1084    }
1085
1086    /// Read the next token separated by whitespaces and parse it to a `i16` value. If there is nothing to read, it will return `Ok(None)`.
1087    ///
1088    /// ```rust
1089    /// use scanner_rust::Scanner;
1090    ///
1091    /// let mut sc = Scanner::new("1 2".as_bytes());
1092    ///
1093    /// assert_eq!(Some(1), sc.next_i16().unwrap());
1094    /// assert_eq!(Some(2), sc.next_i16().unwrap());
1095    /// ```
1096    #[inline]
1097    pub fn next_i16(&mut self) -> Result<Option<i16>, ScannerError> {
1098        self.next_raw_parse()
1099    }
1100
1101    /// Read the next token separated by whitespaces and parse it to a `i32` value. If there is nothing to read, it will return `Ok(None)`.
1102    ///
1103    /// ```rust
1104    /// use scanner_rust::Scanner;
1105    ///
1106    /// let mut sc = Scanner::new("1 2".as_bytes());
1107    ///
1108    /// assert_eq!(Some(1), sc.next_i32().unwrap());
1109    /// assert_eq!(Some(2), sc.next_i32().unwrap());
1110    /// ```
1111    #[inline]
1112    pub fn next_i32(&mut self) -> Result<Option<i32>, ScannerError> {
1113        self.next_raw_parse()
1114    }
1115
1116    /// Read the next token separated by whitespaces and parse it to a `i64` value. If there is nothing to read, it will return `Ok(None)`.
1117    ///
1118    /// ```rust
1119    /// use scanner_rust::Scanner;
1120    ///
1121    /// let mut sc = Scanner::new("1 2".as_bytes());
1122    ///
1123    /// assert_eq!(Some(1), sc.next_i64().unwrap());
1124    /// assert_eq!(Some(2), sc.next_i64().unwrap());
1125    /// ```
1126    #[inline]
1127    pub fn next_i64(&mut self) -> Result<Option<i64>, ScannerError> {
1128        self.next_raw_parse()
1129    }
1130
1131    /// Read the next token separated by whitespaces and parse it to a `i128` value. If there is nothing to read, it will return `Ok(None)`.
1132    ///
1133    /// ```rust
1134    /// use scanner_rust::Scanner;
1135    ///
1136    /// let mut sc = Scanner::new("1 2".as_bytes());
1137    ///
1138    /// assert_eq!(Some(1), sc.next_i128().unwrap());
1139    /// assert_eq!(Some(2), sc.next_i128().unwrap());
1140    /// ```
1141    #[inline]
1142    pub fn next_i128(&mut self) -> Result<Option<i128>, ScannerError> {
1143        self.next_raw_parse()
1144    }
1145
1146    /// Read the next token separated by whitespaces and parse it to a `isize` value. If there is nothing to read, it will return `Ok(None)`.
1147    ///
1148    /// ```rust
1149    /// use scanner_rust::Scanner;
1150    ///
1151    /// let mut sc = Scanner::new("1 2".as_bytes());
1152    ///
1153    /// assert_eq!(Some(1), sc.next_isize().unwrap());
1154    /// assert_eq!(Some(2), sc.next_isize().unwrap());
1155    /// ```
1156    #[inline]
1157    pub fn next_isize(&mut self) -> Result<Option<isize>, ScannerError> {
1158        self.next_raw_parse()
1159    }
1160
1161    /// Read the next token separated by whitespaces and parse it to a `f32` value. If there is nothing to read, it will return `Ok(None)`.
1162    ///
1163    /// ```rust
1164    /// use scanner_rust::Scanner;
1165    ///
1166    /// let mut sc = Scanner::new("1 2.5".as_bytes());
1167    ///
1168    /// assert_eq!(Some(1.0), sc.next_f32().unwrap());
1169    /// assert_eq!(Some(2.5), sc.next_f32().unwrap());
1170    /// ```
1171    #[inline]
1172    pub fn next_f32(&mut self) -> Result<Option<f32>, ScannerError> {
1173        self.next_raw_parse()
1174    }
1175
1176    /// Read the next token separated by whitespaces and parse it to a `f64` value. If there is nothing to read, it will return `Ok(None)`.
1177    ///
1178    /// ```rust
1179    /// use scanner_rust::Scanner;
1180    ///
1181    /// let mut sc = Scanner::new("1 2.5".as_bytes());
1182    ///
1183    /// assert_eq!(Some(1.0), sc.next_f64().unwrap());
1184    /// assert_eq!(Some(2.5), sc.next_f64().unwrap());
1185    /// ```
1186    #[inline]
1187    pub fn next_f64(&mut self) -> Result<Option<f64>, ScannerError> {
1188        self.next_raw_parse()
1189    }
1190}
1191
1192impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> ScannerAscii<R, N> {
1193    #[inline]
1194    fn next_until_raw_parse<T: FromStr, D: ?Sized + AsRef<[u8]>>(
1195        &mut self,
1196        boundary: &D,
1197    ) -> Result<Option<T>, ScannerError>
1198    where
1199        ScannerError: From<<T as FromStr>::Err>, {
1200        let result = self.next_until_raw(boundary)?;
1201
1202        match result {
1203            Some(s) => Ok(Some(unsafe { from_utf8_unchecked(&s) }.parse()?)),
1204            None => Ok(None),
1205        }
1206    }
1207
1208    /// Read the next text until it reaches a specific boundary and parse it to a `u8` value. If there is nothing to read, it will return `Ok(None)`.
1209    ///
1210    /// ```rust
1211    /// use scanner_rust::ScannerAscii;
1212    ///
1213    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1214    ///
1215    /// assert_eq!(Some(1), sc.next_u8_until(" ").unwrap());
1216    /// assert_eq!(Some(2), sc.next_u8_until(" ").unwrap());
1217    /// ```
1218    #[inline]
1219    pub fn next_u8_until<D: ?Sized + AsRef<[u8]>>(
1220        &mut self,
1221        boundary: &D,
1222    ) -> Result<Option<u8>, ScannerError> {
1223        self.next_until_raw_parse(boundary)
1224    }
1225
1226    /// Read the next text until it reaches a specific boundary and parse it to a `u16` value. If there is nothing to read, it will return `Ok(None)`.
1227    ///
1228    /// ```rust
1229    /// use scanner_rust::ScannerAscii;
1230    ///
1231    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1232    ///
1233    /// assert_eq!(Some(1), sc.next_u16_until(" ").unwrap());
1234    /// assert_eq!(Some(2), sc.next_u16_until(" ").unwrap());
1235    /// ```
1236    #[inline]
1237    pub fn next_u16_until<D: ?Sized + AsRef<[u8]>>(
1238        &mut self,
1239        boundary: &D,
1240    ) -> Result<Option<u16>, ScannerError> {
1241        self.next_until_raw_parse(boundary)
1242    }
1243
1244    /// Read the next text until it reaches a specific boundary and parse it to a `u32` value. If there is nothing to read, it will return `Ok(None)`.
1245    ///
1246    /// ```rust
1247    /// use scanner_rust::ScannerAscii;
1248    ///
1249    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1250    ///
1251    /// assert_eq!(Some(1), sc.next_u32_until(" ").unwrap());
1252    /// assert_eq!(Some(2), sc.next_u32_until(" ").unwrap());
1253    /// ```
1254    #[inline]
1255    pub fn next_u32_until<D: ?Sized + AsRef<[u8]>>(
1256        &mut self,
1257        boundary: &D,
1258    ) -> Result<Option<u32>, ScannerError> {
1259        self.next_until_raw_parse(boundary)
1260    }
1261
1262    /// Read the next text until it reaches a specific boundary and parse it to a `u64` value. If there is nothing to read, it will return `Ok(None)`.
1263    ///
1264    /// ```rust
1265    /// use scanner_rust::ScannerAscii;
1266    ///
1267    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1268    ///
1269    /// assert_eq!(Some(1), sc.next_u64_until(" ").unwrap());
1270    /// assert_eq!(Some(2), sc.next_u64_until(" ").unwrap());
1271    /// ```
1272    #[inline]
1273    pub fn next_u64_until<D: ?Sized + AsRef<[u8]>>(
1274        &mut self,
1275        boundary: &D,
1276    ) -> Result<Option<u64>, ScannerError> {
1277        self.next_until_raw_parse(boundary)
1278    }
1279
1280    /// Read the next text until it reaches a specific boundary and parse it to a `u128` value. If there is nothing to read, it will return `Ok(None)`.
1281    ///
1282    /// ```rust
1283    /// use scanner_rust::ScannerAscii;
1284    ///
1285    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1286    ///
1287    /// assert_eq!(Some(1), sc.next_u128_until(" ").unwrap());
1288    /// assert_eq!(Some(2), sc.next_u128_until(" ").unwrap());
1289    /// ```
1290    #[inline]
1291    pub fn next_u128_until<D: ?Sized + AsRef<[u8]>>(
1292        &mut self,
1293        boundary: &D,
1294    ) -> Result<Option<u128>, ScannerError> {
1295        self.next_until_raw_parse(boundary)
1296    }
1297
1298    /// Read the next text until it reaches a specific boundary and parse it to a `usize` value. If there is nothing to read, it will return `Ok(None)`.
1299    ///
1300    /// ```rust
1301    /// use scanner_rust::ScannerAscii;
1302    ///
1303    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1304    ///
1305    /// assert_eq!(Some(1), sc.next_usize_until(" ").unwrap());
1306    /// assert_eq!(Some(2), sc.next_usize_until(" ").unwrap());
1307    /// ```
1308    #[inline]
1309    pub fn next_usize_until<D: ?Sized + AsRef<[u8]>>(
1310        &mut self,
1311        boundary: &D,
1312    ) -> Result<Option<usize>, ScannerError> {
1313        self.next_until_raw_parse(boundary)
1314    }
1315
1316    /// Read the next text until it reaches a specific boundary and parse it to a `i8` value. If there is nothing to read, it will return `Ok(None)`.
1317    ///
1318    /// ```rust
1319    /// use scanner_rust::ScannerAscii;
1320    ///
1321    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1322    ///
1323    /// assert_eq!(Some(1), sc.next_usize_until(" ").unwrap());
1324    /// assert_eq!(Some(2), sc.next_usize_until(" ").unwrap());
1325    /// ```
1326    #[inline]
1327    pub fn next_i8_until<D: ?Sized + AsRef<[u8]>>(
1328        &mut self,
1329        boundary: &D,
1330    ) -> Result<Option<i8>, ScannerError> {
1331        self.next_until_raw_parse(boundary)
1332    }
1333
1334    /// Read the next text until it reaches a specific boundary and parse it to a `i16` value. If there is nothing to read, it will return `Ok(None)`.
1335    ///
1336    /// ```rust
1337    /// use scanner_rust::ScannerAscii;
1338    ///
1339    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1340    ///
1341    /// assert_eq!(Some(1), sc.next_i16_until(" ").unwrap());
1342    /// assert_eq!(Some(2), sc.next_i16_until(" ").unwrap());
1343    /// ```
1344    #[inline]
1345    pub fn next_i16_until<D: ?Sized + AsRef<[u8]>>(
1346        &mut self,
1347        boundary: &D,
1348    ) -> Result<Option<i16>, ScannerError> {
1349        self.next_until_raw_parse(boundary)
1350    }
1351
1352    /// Read the next text until it reaches a specific boundary and parse it to a `i32` value. If there is nothing to read, it will return `Ok(None)`.
1353    ///
1354    /// ```rust
1355    /// use scanner_rust::ScannerAscii;
1356    ///
1357    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1358    ///
1359    /// assert_eq!(Some(1), sc.next_i32_until(" ").unwrap());
1360    /// assert_eq!(Some(2), sc.next_i32_until(" ").unwrap());
1361    /// ```
1362    #[inline]
1363    pub fn next_i32_until<D: ?Sized + AsRef<[u8]>>(
1364        &mut self,
1365        boundary: &D,
1366    ) -> Result<Option<i32>, ScannerError> {
1367        self.next_until_raw_parse(boundary)
1368    }
1369
1370    /// Read the next text until it reaches a specific boundary and parse it to a `i64` value. If there is nothing to read, it will return `Ok(None)`.
1371    ///
1372    /// ```rust
1373    /// use scanner_rust::ScannerAscii;
1374    ///
1375    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1376    ///
1377    /// assert_eq!(Some(1), sc.next_i64_until(" ").unwrap());
1378    /// assert_eq!(Some(2), sc.next_i64_until(" ").unwrap());
1379    /// ```
1380    #[inline]
1381    pub fn next_i64_until<D: ?Sized + AsRef<[u8]>>(
1382        &mut self,
1383        boundary: &D,
1384    ) -> Result<Option<i64>, ScannerError> {
1385        self.next_until_raw_parse(boundary)
1386    }
1387
1388    /// Read the next text until it reaches a specific boundary and parse it to a `i128` value. If there is nothing to read, it will return `Ok(None)`.
1389    ///
1390    /// ```rust
1391    /// use scanner_rust::ScannerAscii;
1392    ///
1393    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1394    ///
1395    /// assert_eq!(Some(1), sc.next_i128_until(" ").unwrap());
1396    /// assert_eq!(Some(2), sc.next_i128_until(" ").unwrap());
1397    /// ```
1398    #[inline]
1399    pub fn next_i128_until<D: ?Sized + AsRef<[u8]>>(
1400        &mut self,
1401        boundary: &D,
1402    ) -> Result<Option<i128>, ScannerError> {
1403        self.next_until_raw_parse(boundary)
1404    }
1405
1406    /// Read the next text until it reaches a specific boundary and parse it to a `isize` value. If there is nothing to read, it will return `Ok(None)`.
1407    ///
1408    /// ```rust
1409    /// use scanner_rust::ScannerAscii;
1410    ///
1411    /// let mut sc = ScannerAscii::new("1 2".as_bytes());
1412    ///
1413    /// assert_eq!(Some(1), sc.next_isize_until(" ").unwrap());
1414    /// assert_eq!(Some(2), sc.next_isize_until(" ").unwrap());
1415    /// ```
1416    #[inline]
1417    pub fn next_isize_until<D: ?Sized + AsRef<[u8]>>(
1418        &mut self,
1419        boundary: &D,
1420    ) -> Result<Option<isize>, ScannerError> {
1421        self.next_until_raw_parse(boundary)
1422    }
1423
1424    /// Read the next text until it reaches a specific boundary and parse it to a `f32` value. If there is nothing to read, it will return `Ok(None)`.
1425    ///
1426    /// ```rust
1427    /// use scanner_rust::ScannerAscii;
1428    ///
1429    /// let mut sc = ScannerAscii::new("1 2.5".as_bytes());
1430    ///
1431    /// assert_eq!(Some(1.0), sc.next_f32_until(" ").unwrap());
1432    /// assert_eq!(Some(2.5), sc.next_f32_until(" ").unwrap());
1433    /// ```
1434    #[inline]
1435    pub fn next_f32_until<D: ?Sized + AsRef<[u8]>>(
1436        &mut self,
1437        boundary: &D,
1438    ) -> Result<Option<f32>, ScannerError> {
1439        self.next_until_raw_parse(boundary)
1440    }
1441
1442    /// Read the next text until it reaches a specific boundary and parse it to a `f64` value. If there is nothing to read, it will return `Ok(None)`.
1443    ///
1444    /// ```rust
1445    /// use scanner_rust::ScannerAscii;
1446    ///
1447    /// let mut sc = ScannerAscii::new("1 2.5".as_bytes());
1448    ///
1449    /// assert_eq!(Some(1.0), sc.next_f64_until(" ").unwrap());
1450    /// assert_eq!(Some(2.5), sc.next_f64_until(" ").unwrap());
1451    /// ```
1452    #[inline]
1453    pub fn next_f64_until<D: ?Sized + AsRef<[u8]>>(
1454        &mut self,
1455        boundary: &D,
1456    ) -> Result<Option<f64>, ScannerError> {
1457        self.next_until_raw_parse(boundary)
1458    }
1459}