scanner_rust/
scanner_str.rs

1use std::str::FromStr;
2
3use utf8_width::*;
4
5use crate::{whitespaces::*, ScannerError};
6
7/// A simple text scanner which can in-memory-ly parse primitive types and strings using UTF-8 from a string slice.
8#[derive(Debug)]
9pub struct ScannerStr<'a> {
10    text:        &'a str,
11    text_length: usize,
12    position:    usize,
13}
14
15impl<'a> ScannerStr<'a> {
16    /// Create a scanner from a string.
17    ///
18    /// ```rust
19    /// use std::io;
20    ///
21    /// use scanner_rust::ScannerStr;
22    ///
23    /// let mut sc = ScannerStr::new("123 456");
24    /// ```
25    #[inline]
26    pub fn new<S: ?Sized + AsRef<str>>(text: &S) -> ScannerStr {
27        let text = text.as_ref();
28
29        ScannerStr {
30            text,
31            text_length: text.len(),
32            position: 0,
33        }
34    }
35}
36
37impl<'a> ScannerStr<'a> {
38    /// 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)`.
39    ///
40    /// ```rust
41    /// use scanner_rust::ScannerStr;
42    ///
43    /// let mut sc = ScannerStr::new("5 c 中文");
44    ///
45    /// assert_eq!(Some('5'), sc.next_char().unwrap());
46    /// assert_eq!(Some(' '), sc.next_char().unwrap());
47    /// assert_eq!(Some('c'), sc.next_char().unwrap());
48    /// assert_eq!(Some(' '), sc.next_char().unwrap());
49    /// assert_eq!(Some('中'), sc.next_char().unwrap());
50    /// assert_eq!(Some('文'), sc.next_char().unwrap());
51    /// assert_eq!(None, sc.next_char().unwrap());
52    /// ```
53    pub fn next_char(&mut self) -> Result<Option<char>, ScannerError> {
54        if self.position == self.text_length {
55            return Ok(None);
56        }
57
58        let data = self.text.as_bytes();
59
60        let e = data[self.position];
61
62        let width = unsafe { get_width_assume_valid(e) };
63
64        match width {
65            1 => {
66                self.position += 1;
67
68                Ok(Some(e as char))
69            },
70            _ => {
71                let char_str = &self.text[self.position..(self.position + width)];
72
73                self.position += width;
74
75                Ok(char_str.chars().next())
76            },
77        }
78    }
79
80    /// 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)`.
81    ///
82    /// ```rust
83    /// use scanner_rust::ScannerStr;
84    ///
85    /// let mut sc = ScannerStr::new("123 456\r\n789 \n\n 中文 ");
86    ///
87    /// assert_eq!(Some("123 456"), sc.next_line().unwrap());
88    /// assert_eq!(Some("789 "), sc.next_line().unwrap());
89    /// assert_eq!(Some(""), sc.next_line().unwrap());
90    /// assert_eq!(Some(" 中文 "), sc.next_line().unwrap());
91    /// ```
92    pub fn next_line(&mut self) -> Result<Option<&'a str>, ScannerError> {
93        if self.position == self.text_length {
94            return Ok(None);
95        }
96
97        let data = self.text.as_bytes();
98
99        let mut p = self.position;
100
101        loop {
102            let e = data[p];
103
104            let width = unsafe { get_width_assume_valid(e) };
105
106            match width {
107                1 => {
108                    match e {
109                        b'\n' => {
110                            let text = &self.text[self.position..p];
111
112                            if p + 1 < self.text_length && data[p + 1] == b'\r' {
113                                self.position = p + 2;
114                            } else {
115                                self.position = p + 1;
116                            }
117
118                            return Ok(Some(text));
119                        },
120                        b'\r' => {
121                            let text = &self.text[self.position..p];
122
123                            if p + 1 < self.text_length && data[p + 1] == b'\n' {
124                                self.position = p + 2;
125                            } else {
126                                self.position = p + 1;
127                            }
128
129                            return Ok(Some(text));
130                        },
131                        _ => (),
132                    }
133
134                    p += 1;
135                },
136                _ => {
137                    p += width;
138                },
139            }
140
141            if p == self.text_length {
142                break;
143            }
144        }
145
146        let text = &self.text[self.position..p];
147
148        self.position = p;
149
150        Ok(Some(text))
151    }
152}
153
154impl<'a> ScannerStr<'a> {
155    /// Skip the next whitespaces (`javaWhitespace`). If there is nothing to read, it will return `Ok(false)`.
156    ///
157    /// ```rust
158    /// use scanner_rust::ScannerStr;
159    ///
160    /// let mut sc = ScannerStr::new("1 2   c");
161    ///
162    /// assert_eq!(Some('1'), sc.next_char().unwrap());
163    /// assert_eq!(Some(' '), sc.next_char().unwrap());
164    /// assert_eq!(Some('2'), sc.next_char().unwrap());
165    /// assert_eq!(true, sc.skip_whitespaces().unwrap());
166    /// assert_eq!(Some('c'), sc.next_char().unwrap());
167    /// assert_eq!(false, sc.skip_whitespaces().unwrap());
168    /// ```
169    pub fn skip_whitespaces(&mut self) -> Result<bool, ScannerError> {
170        if self.position == self.text_length {
171            return Ok(false);
172        }
173
174        let data = self.text.as_bytes();
175
176        loop {
177            let e = data[self.position];
178
179            let width = unsafe { get_width_assume_valid(e) };
180
181            match width {
182                1 => {
183                    if !is_whitespace_1(e) {
184                        break;
185                    }
186
187                    self.position += 1;
188                },
189                3 => {
190                    if !is_whitespace_3(
191                        data[self.position],
192                        data[self.position + 1],
193                        data[self.position + 2],
194                    ) {
195                        break;
196                    }
197
198                    self.position += 3;
199                },
200                _ => {
201                    break;
202                },
203            }
204
205            if self.position == self.text_length {
206                break;
207            }
208        }
209
210        Ok(true)
211    }
212
213    /// Read the next token separated by whitespaces. If there is nothing to read, it will return `Ok(None)`.
214    ///
215    /// ```rust
216    /// use scanner_rust::ScannerStr;
217    ///
218    /// let mut sc = ScannerStr::new("123 456\r\n789 \n\n 中文 ");
219    ///
220    /// assert_eq!(Some("123"), sc.next().unwrap());
221    /// assert_eq!(Some("456"), sc.next().unwrap());
222    /// assert_eq!(Some("789"), sc.next().unwrap());
223    /// assert_eq!(Some("中文"), sc.next().unwrap());
224    /// assert_eq!(None, sc.next().unwrap());
225    /// ```
226    #[allow(clippy::should_implement_trait)]
227    pub fn next(&mut self) -> Result<Option<&'a str>, ScannerError> {
228        if !self.skip_whitespaces()? {
229            return Ok(None);
230        }
231
232        if self.position == self.text_length {
233            return Ok(None);
234        }
235
236        let data = self.text.as_bytes();
237
238        let mut p = self.position;
239
240        loop {
241            let e = data[p];
242
243            let width = unsafe { get_width_assume_valid(e) };
244
245            match width {
246                1 => {
247                    if is_whitespace_1(e) {
248                        let text = &self.text[self.position..p];
249
250                        self.position = p;
251
252                        return Ok(Some(text));
253                    }
254
255                    p += 1;
256                },
257                3 => {
258                    if is_whitespace_3(
259                        data[self.position],
260                        data[self.position + 1],
261                        data[self.position + 2],
262                    ) {
263                        let text = &self.text[self.position..p];
264
265                        self.position = p;
266
267                        return Ok(Some(text));
268                    } else {
269                        p += 3;
270                    }
271                },
272                _ => {
273                    p += width;
274                },
275            }
276
277            if p == self.text_length {
278                break;
279            }
280        }
281
282        let text = &self.text[self.position..p];
283
284        self.position = p;
285
286        Ok(Some(text))
287    }
288}
289
290impl<'a> ScannerStr<'a> {
291    /// Read the next text (as a string slice) with a specific max number of characters. If there is nothing to read, it will return `Ok(None)`.
292    ///
293    /// ```rust
294    /// use scanner_rust::ScannerStr;
295    ///
296    /// let mut sc = ScannerStr::new("123 456\r\n789 \n\n 中文 ");
297    ///
298    /// assert_eq!(Some("123"), sc.next_str(3).unwrap());
299    /// assert_eq!(Some(" 456"), sc.next_str(4).unwrap());
300    /// assert_eq!(Some("\r\n789 "), sc.next_str(6).unwrap());
301    /// assert_eq!(Some("\n\n 中"), sc.next_str(4).unwrap());
302    /// assert_eq!(Some("文"), sc.next().unwrap());
303    /// assert_eq!(Some(" "), sc.next_str(2).unwrap());
304    /// assert_eq!(None, sc.next_str(2).unwrap());
305    /// ```
306    pub fn next_str(
307        &mut self,
308        max_number_of_characters: usize,
309    ) -> Result<Option<&str>, ScannerError> {
310        if self.position == self.text_length {
311            return Ok(None);
312        }
313
314        let data = self.text.as_bytes();
315
316        let mut p = self.position;
317        let mut c = 0;
318
319        while c < max_number_of_characters {
320            let width = unsafe { get_width_assume_valid(data[p]) };
321
322            p += width;
323
324            c += 1;
325
326            if p == self.text_length {
327                break;
328            }
329        }
330
331        let text = &self.text[self.position..p];
332
333        self.position = p;
334
335        Ok(Some(text))
336    }
337}
338
339impl<'a> ScannerStr<'a> {
340    /// Read the next text until it reaches a specific boundary. If there is nothing to read, it will return `Ok(None)`.
341    ///
342    /// ```rust
343    /// use scanner_rust::ScannerStr;
344    ///
345    /// let mut sc = ScannerStr::new("123 456\r\n789 \n\n 中文 ");
346    ///
347    /// assert_eq!(Some("123"), sc.next_until(" ").unwrap());
348    /// assert_eq!(Some("456\r"), sc.next_until("\n").unwrap());
349    /// assert_eq!(Some("78"), sc.next_until("9 ").unwrap());
350    /// assert_eq!(Some("\n\n 中文 "), sc.next_until("kk").unwrap());
351    /// assert_eq!(None, sc.next().unwrap());
352    /// ```
353    pub fn next_until<S: AsRef<str>>(
354        &mut self,
355        boundary: S,
356    ) -> Result<Option<&'a str>, ScannerError> {
357        if self.position == self.text_length {
358            return Ok(None);
359        }
360
361        let boundary = boundary.as_ref().as_bytes();
362        let boundary_length = boundary.len();
363
364        if boundary_length == 0 || boundary_length >= self.text_length - self.position {
365            let text = &self.text[self.position..];
366
367            self.position = self.text_length;
368
369            return Ok(Some(text));
370        }
371
372        let data = self.text.as_bytes();
373
374        for i in self.position..(self.text_length - boundary_length) {
375            let e = i + boundary_length;
376
377            if &data[i..e] == boundary {
378                let text = &self.text[self.position..i];
379
380                self.position = e;
381
382                return Ok(Some(text));
383            }
384        }
385
386        let text = &self.text[self.position..];
387
388        self.position = self.text_length;
389
390        Ok(Some(text))
391    }
392}
393
394impl<'a> ScannerStr<'a> {
395    #[inline]
396    fn next_parse<T: FromStr>(&mut self) -> Result<Option<T>, ScannerError>
397    where
398        ScannerError: From<<T as FromStr>::Err>, {
399        let result = self.next()?;
400
401        match result {
402            Some(s) => Ok(Some(s.parse()?)),
403            None => Ok(None),
404        }
405    }
406
407    /// 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)`.
408    ///
409    /// ```rust
410    /// use scanner_rust::ScannerStr;
411    ///
412    /// let mut sc = ScannerStr::new("1 2");
413    ///
414    /// assert_eq!(Some(1), sc.next_u8().unwrap());
415    /// assert_eq!(Some(2), sc.next_u8().unwrap());
416    /// ```
417    #[inline]
418    pub fn next_u8(&mut self) -> Result<Option<u8>, ScannerError> {
419        self.next_parse()
420    }
421
422    /// 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)`.
423    ///
424    /// ```rust
425    /// use scanner_rust::ScannerStr;
426    ///
427    /// let mut sc = ScannerStr::new("1 2");
428    ///
429    /// assert_eq!(Some(1), sc.next_u16().unwrap());
430    /// assert_eq!(Some(2), sc.next_u16().unwrap());
431    /// ```
432    #[inline]
433    pub fn next_u16(&mut self) -> Result<Option<u16>, ScannerError> {
434        self.next_parse()
435    }
436
437    /// 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)`.
438    ///
439    /// ```rust
440    /// use scanner_rust::ScannerStr;
441    ///
442    /// let mut sc = ScannerStr::new("1 2");
443    ///
444    /// assert_eq!(Some(1), sc.next_u32().unwrap());
445    /// assert_eq!(Some(2), sc.next_u32().unwrap());
446    /// ```
447    #[inline]
448    pub fn next_u32(&mut self) -> Result<Option<u32>, ScannerError> {
449        self.next_parse()
450    }
451
452    /// 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)`.
453    ///
454    /// ```rust
455    /// use scanner_rust::ScannerStr;
456    ///
457    /// let mut sc = ScannerStr::new("1 2");
458    ///
459    /// assert_eq!(Some(1), sc.next_u64().unwrap());
460    /// assert_eq!(Some(2), sc.next_u64().unwrap());
461    /// ```
462    #[inline]
463    pub fn next_u64(&mut self) -> Result<Option<u64>, ScannerError> {
464        self.next_parse()
465    }
466
467    /// 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)`.
468    ///
469    /// ```rust
470    /// use scanner_rust::ScannerStr;
471    ///
472    /// let mut sc = ScannerStr::new("1 2");
473    ///
474    /// assert_eq!(Some(1), sc.next_u128().unwrap());
475    /// assert_eq!(Some(2), sc.next_u128().unwrap());
476    /// ```
477    #[inline]
478    pub fn next_u128(&mut self) -> Result<Option<u128>, ScannerError> {
479        self.next_parse()
480    }
481
482    /// 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)`.
483    ///
484    /// ```rust
485    /// use scanner_rust::ScannerStr;
486    ///
487    /// let mut sc = ScannerStr::new("1 2");
488    ///
489    /// assert_eq!(Some(1), sc.next_usize().unwrap());
490    /// assert_eq!(Some(2), sc.next_usize().unwrap());
491    /// ```
492    #[inline]
493    pub fn next_usize(&mut self) -> Result<Option<usize>, ScannerError> {
494        self.next_parse()
495    }
496
497    /// 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)`.
498    ///
499    /// ```rust
500    /// use scanner_rust::ScannerStr;
501    ///
502    /// let mut sc = ScannerStr::new("1 2");
503    ///
504    /// assert_eq!(Some(1), sc.next_i8().unwrap());
505    /// assert_eq!(Some(2), sc.next_i8().unwrap());
506    /// ```
507    #[inline]
508    pub fn next_i8(&mut self) -> Result<Option<i8>, ScannerError> {
509        self.next_parse()
510    }
511
512    /// 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)`.
513    ///
514    /// ```rust
515    /// use scanner_rust::ScannerStr;
516    ///
517    /// let mut sc = ScannerStr::new("1 2");
518    ///
519    /// assert_eq!(Some(1), sc.next_i16().unwrap());
520    /// assert_eq!(Some(2), sc.next_i16().unwrap());
521    /// ```
522    #[inline]
523    pub fn next_i16(&mut self) -> Result<Option<i16>, ScannerError> {
524        self.next_parse()
525    }
526
527    /// 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)`.
528    ///
529    /// ```rust
530    /// use scanner_rust::ScannerStr;
531    ///
532    /// let mut sc = ScannerStr::new("1 2");
533    ///
534    /// assert_eq!(Some(1), sc.next_i32().unwrap());
535    /// assert_eq!(Some(2), sc.next_i32().unwrap());
536    /// ```
537    #[inline]
538    pub fn next_i32(&mut self) -> Result<Option<i32>, ScannerError> {
539        self.next_parse()
540    }
541
542    /// 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)`.
543    ///
544    /// ```rust
545    /// use scanner_rust::ScannerStr;
546    ///
547    /// let mut sc = ScannerStr::new("1 2");
548    ///
549    /// assert_eq!(Some(1), sc.next_i64().unwrap());
550    /// assert_eq!(Some(2), sc.next_i64().unwrap());
551    /// ```
552    #[inline]
553    pub fn next_i64(&mut self) -> Result<Option<i64>, ScannerError> {
554        self.next_parse()
555    }
556
557    /// 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)`.
558    ///
559    /// ```rust
560    /// use scanner_rust::ScannerStr;
561    ///
562    /// let mut sc = ScannerStr::new("1 2");
563    ///
564    /// assert_eq!(Some(1), sc.next_i128().unwrap());
565    /// assert_eq!(Some(2), sc.next_i128().unwrap());
566    /// ```
567    #[inline]
568    pub fn next_i128(&mut self) -> Result<Option<i128>, ScannerError> {
569        self.next_parse()
570    }
571
572    /// 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)`.
573    ///
574    /// ```rust
575    /// use scanner_rust::ScannerStr;
576    ///
577    /// let mut sc = ScannerStr::new("1 2");
578    ///
579    /// assert_eq!(Some(1), sc.next_isize().unwrap());
580    /// assert_eq!(Some(2), sc.next_isize().unwrap());
581    /// ```
582    #[inline]
583    pub fn next_isize(&mut self) -> Result<Option<isize>, ScannerError> {
584        self.next_parse()
585    }
586
587    /// 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)`.
588    ///
589    /// ```rust
590    /// use scanner_rust::ScannerStr;
591    ///
592    /// let mut sc = ScannerStr::new("1 2.5");
593    ///
594    /// assert_eq!(Some(1.0), sc.next_f32().unwrap());
595    /// assert_eq!(Some(2.5), sc.next_f32().unwrap());
596    /// ```
597    #[inline]
598    pub fn next_f32(&mut self) -> Result<Option<f32>, ScannerError> {
599        self.next_parse()
600    }
601
602    /// 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)`.
603    ///
604    /// ```rust
605    /// use scanner_rust::ScannerStr;
606    ///
607    /// let mut sc = ScannerStr::new("1 2.5");
608    ///
609    /// assert_eq!(Some(1.0), sc.next_f64().unwrap());
610    /// assert_eq!(Some(2.5), sc.next_f64().unwrap());
611    /// ```
612    #[inline]
613    pub fn next_f64(&mut self) -> Result<Option<f64>, ScannerError> {
614        self.next_parse()
615    }
616}
617
618impl<'a> ScannerStr<'a> {
619    #[inline]
620    fn next_raw_parse<T: FromStr, S: AsRef<str>>(
621        &mut self,
622        boundary: S,
623    ) -> Result<Option<T>, ScannerError>
624    where
625        ScannerError: From<<T as FromStr>::Err>, {
626        let result = self.next_until(boundary)?;
627
628        match result {
629            Some(s) => Ok(Some(s.parse()?)),
630            None => Ok(None),
631        }
632    }
633
634    /// 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)`.
635    ///
636    /// ```rust
637    /// use scanner_rust::ScannerStr;
638    ///
639    /// let mut sc = ScannerStr::new("1 2");
640    ///
641    /// assert_eq!(Some(1), sc.next_u8_until(" ").unwrap());
642    /// assert_eq!(Some(2), sc.next_u8_until(" ").unwrap());
643    /// ```
644    #[inline]
645    pub fn next_u8_until<S: AsRef<str>>(
646        &mut self,
647        boundary: S,
648    ) -> Result<Option<u8>, ScannerError> {
649        self.next_raw_parse(boundary)
650    }
651
652    /// 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)`.
653    ///
654    /// ```rust
655    /// use scanner_rust::ScannerStr;
656    ///
657    /// let mut sc = ScannerStr::new("1 2");
658    ///
659    /// assert_eq!(Some(1), sc.next_u16_until(" ").unwrap());
660    /// assert_eq!(Some(2), sc.next_u16_until(" ").unwrap());
661    /// ```
662    #[inline]
663    pub fn next_u16_until<S: AsRef<str>>(
664        &mut self,
665        boundary: S,
666    ) -> Result<Option<u16>, ScannerError> {
667        self.next_raw_parse(boundary)
668    }
669
670    /// 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)`.
671    ///
672    /// ```rust
673    /// use scanner_rust::ScannerStr;
674    ///
675    /// let mut sc = ScannerStr::new("1 2");
676    ///
677    /// assert_eq!(Some(1), sc.next_u32_until(" ").unwrap());
678    /// assert_eq!(Some(2), sc.next_u32_until(" ").unwrap());
679    /// ```
680    #[inline]
681    pub fn next_u32_until<S: AsRef<str>>(
682        &mut self,
683        boundary: S,
684    ) -> Result<Option<u32>, ScannerError> {
685        self.next_raw_parse(boundary)
686    }
687
688    /// 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)`.
689    ///
690    /// ```rust
691    /// use scanner_rust::ScannerStr;
692    ///
693    /// let mut sc = ScannerStr::new("1 2");
694    ///
695    /// assert_eq!(Some(1), sc.next_u64_until(" ").unwrap());
696    /// assert_eq!(Some(2), sc.next_u64_until(" ").unwrap());
697    /// ```
698    #[inline]
699    pub fn next_u64_until<S: AsRef<str>>(
700        &mut self,
701        boundary: S,
702    ) -> Result<Option<u64>, ScannerError> {
703        self.next_raw_parse(boundary)
704    }
705
706    /// 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)`.
707    ///
708    /// ```rust
709    /// use scanner_rust::ScannerStr;
710    ///
711    /// let mut sc = ScannerStr::new("1 2");
712    ///
713    /// assert_eq!(Some(1), sc.next_u128_until(" ").unwrap());
714    /// assert_eq!(Some(2), sc.next_u128_until(" ").unwrap());
715    /// ```
716    #[inline]
717    pub fn next_u128_until<S: AsRef<str>>(
718        &mut self,
719        boundary: S,
720    ) -> Result<Option<u128>, ScannerError> {
721        self.next_raw_parse(boundary)
722    }
723
724    /// 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)`.
725    ///
726    /// ```rust
727    /// use scanner_rust::ScannerStr;
728    ///
729    /// let mut sc = ScannerStr::new("1 2");
730    ///
731    /// assert_eq!(Some(1), sc.next_usize_until(" ").unwrap());
732    /// assert_eq!(Some(2), sc.next_usize_until(" ").unwrap());
733    /// ```
734    #[inline]
735    pub fn next_usize_until<S: AsRef<str>>(
736        &mut self,
737        boundary: S,
738    ) -> Result<Option<usize>, ScannerError> {
739        self.next_raw_parse(boundary)
740    }
741
742    /// 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)`.
743    ///
744    /// ```rust
745    /// use scanner_rust::ScannerStr;
746    ///
747    /// let mut sc = ScannerStr::new("1 2");
748    ///
749    /// assert_eq!(Some(1), sc.next_i8_until(" ").unwrap());
750    /// assert_eq!(Some(2), sc.next_i8_until(" ").unwrap());
751    /// ```
752    #[inline]
753    pub fn next_i8_until<S: AsRef<str>>(
754        &mut self,
755        boundary: S,
756    ) -> Result<Option<i8>, ScannerError> {
757        self.next_raw_parse(boundary)
758    }
759
760    /// 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)`.
761    ///
762    /// ```rust
763    /// use scanner_rust::ScannerStr;
764    ///
765    /// let mut sc = ScannerStr::new("1 2");
766    ///
767    /// assert_eq!(Some(1), sc.next_i16_until(" ").unwrap());
768    /// assert_eq!(Some(2), sc.next_i16_until(" ").unwrap());
769    /// ```
770    #[inline]
771    pub fn next_i16_until<S: AsRef<str>>(
772        &mut self,
773        boundary: S,
774    ) -> Result<Option<i16>, ScannerError> {
775        self.next_raw_parse(boundary)
776    }
777
778    /// 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)`.
779    ///
780    /// ```rust
781    /// use scanner_rust::ScannerStr;
782    ///
783    /// let mut sc = ScannerStr::new("1 2");
784    ///
785    /// assert_eq!(Some(1), sc.next_i32_until(" ").unwrap());
786    /// assert_eq!(Some(2), sc.next_i32_until(" ").unwrap());
787    /// ```
788    #[inline]
789    pub fn next_i32_until<S: AsRef<str>>(
790        &mut self,
791        boundary: S,
792    ) -> Result<Option<i32>, ScannerError> {
793        self.next_raw_parse(boundary)
794    }
795
796    /// 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)`.
797    ///
798    /// ```rust
799    /// use scanner_rust::ScannerStr;
800    ///
801    /// let mut sc = ScannerStr::new("1 2");
802    ///
803    /// assert_eq!(Some(1), sc.next_i64_until(" ").unwrap());
804    /// assert_eq!(Some(2), sc.next_i64_until(" ").unwrap());
805    /// ```
806    #[inline]
807    pub fn next_i64_until<S: AsRef<str>>(
808        &mut self,
809        boundary: S,
810    ) -> Result<Option<i64>, ScannerError> {
811        self.next_raw_parse(boundary)
812    }
813
814    /// 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)`.
815    ///
816    /// ```rust
817    /// use scanner_rust::ScannerStr;
818    ///
819    /// let mut sc = ScannerStr::new("1 2");
820    ///
821    /// assert_eq!(Some(1), sc.next_i128_until(" ").unwrap());
822    /// assert_eq!(Some(2), sc.next_i128_until(" ").unwrap());
823    /// ```
824    #[inline]
825    pub fn next_i128_until<S: AsRef<str>>(
826        &mut self,
827        boundary: S,
828    ) -> Result<Option<i128>, ScannerError> {
829        self.next_raw_parse(boundary)
830    }
831
832    /// 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)`.
833    ///
834    /// ```rust
835    /// use scanner_rust::ScannerStr;
836    ///
837    /// let mut sc = ScannerStr::new("1 2");
838    ///
839    /// assert_eq!(Some(1), sc.next_isize_until(" ").unwrap());
840    /// assert_eq!(Some(2), sc.next_isize_until(" ").unwrap());
841    /// ```
842    #[inline]
843    pub fn next_isize_until<S: AsRef<str>>(
844        &mut self,
845        boundary: S,
846    ) -> Result<Option<isize>, ScannerError> {
847        self.next_raw_parse(boundary)
848    }
849
850    /// 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)`.
851    ///
852    /// ```rust
853    /// use scanner_rust::ScannerStr;
854    ///
855    /// let mut sc = ScannerStr::new("1 2.5");
856    ///
857    /// assert_eq!(Some(1.0), sc.next_f32_until(" ").unwrap());
858    /// assert_eq!(Some(2.5), sc.next_f32_until(" ").unwrap());
859    /// ```
860    #[inline]
861    pub fn next_f32_until<S: AsRef<str>>(
862        &mut self,
863        boundary: S,
864    ) -> Result<Option<f32>, ScannerError> {
865        self.next_raw_parse(boundary)
866    }
867
868    /// 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)`.
869    ///
870    /// ```rust
871    /// use scanner_rust::ScannerStr;
872    ///
873    /// let mut sc = ScannerStr::new("1 2.5");
874    ///
875    /// assert_eq!(Some(1.0), sc.next_f64_until(" ").unwrap());
876    /// assert_eq!(Some(2.5), sc.next_f64_until(" ").unwrap());
877    /// ```
878    #[inline]
879    pub fn next_f64_until<S: AsRef<str>>(
880        &mut self,
881        boundary: S,
882    ) -> Result<Option<f64>, ScannerError> {
883        self.next_raw_parse(boundary)
884    }
885}
886
887impl<'a> Iterator for ScannerStr<'a> {
888    type Item = &'a str;
889
890    #[inline]
891    fn next(&mut self) -> Option<Self::Item> {
892        self.next().unwrap_or(None)
893    }
894}