scanner_rust/scanner.rs
1use std::{
2 char::REPLACEMENT_CHARACTER,
3 cmp::Ordering,
4 fs::File,
5 intrinsics::copy,
6 io::{ErrorKind, Read},
7 path::Path,
8 str::{from_utf8, from_utf8_unchecked, FromStr},
9};
10
11use generic_array::{
12 typenum::{IsGreaterOrEqual, True, U256, U4},
13 ArrayLength, GenericArray,
14};
15use utf8_width::*;
16
17use crate::{whitespaces::*, ScannerError};
18
19/// A simple text scanner which can parse primitive types and strings using UTF-8.
20#[derive(Educe)]
21#[educe(Debug)]
22pub struct Scanner<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True> = U256> {
23 #[educe(Debug(ignore))]
24 reader: R,
25 buf: GenericArray<u8, N>,
26 buf_length: usize,
27 buf_offset: usize,
28 passing_byte: Option<u8>,
29}
30
31impl<R: Read> Scanner<R> {
32 /// Create a scanner from a reader.
33 ///
34 /// ```rust
35 /// use std::io;
36 ///
37 /// use scanner_rust::Scanner;
38 ///
39 /// let mut sc = Scanner::new(io::stdin());
40 /// ```
41 #[inline]
42 pub fn new(reader: R) -> Scanner<R> {
43 Self::new2(reader)
44 }
45}
46
47impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
48 /// Create a scanner from a reader and set the buffer size via generics.
49 ///
50 /// ```rust
51 /// use std::io;
52 ///
53 /// use scanner_rust::{generic_array::typenum::U1024, Scanner};
54 ///
55 /// let mut sc: Scanner<_, U1024> = Scanner::new2(io::stdin());
56 /// ```
57 #[inline]
58 pub fn new2(reader: R) -> Scanner<R, N> {
59 Scanner {
60 reader,
61 buf: GenericArray::default(),
62 buf_length: 0,
63 buf_offset: 0,
64 passing_byte: None,
65 }
66 }
67}
68
69impl Scanner<File> {
70 /// Create a scanner to read data from a file by its path.
71 ///
72 /// ```rust
73 /// use scanner_rust::Scanner;
74 ///
75 /// let mut sc = Scanner::scan_path("Cargo.toml").unwrap();
76 /// ```
77 #[inline]
78 pub fn scan_path<P: AsRef<Path>>(path: P) -> Result<Scanner<File>, ScannerError> {
79 Self::scan_path2(path)
80 }
81}
82
83impl<N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<File, N> {
84 /// Create a scanner to read data from a file by its path and set the buffer size via generics.
85 ///
86 /// ```rust
87 /// use scanner_rust::{generic_array::typenum::U1024, Scanner};
88 ///
89 /// let mut sc: Scanner<_, U1024> = Scanner::scan_path2("Cargo.toml").unwrap();
90 /// ```
91 #[inline]
92 pub fn scan_path2<P: AsRef<Path>>(path: P) -> Result<Scanner<File, N>, ScannerError> {
93 let reader = File::open(path)?;
94
95 Ok(Scanner::new2(reader))
96 }
97}
98
99impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
100 #[inline]
101 fn buf_align_to_frond_end(&mut self) {
102 unsafe {
103 copy(self.buf.as_ptr().add(self.buf_offset), self.buf.as_mut_ptr(), self.buf_length);
104 }
105
106 self.buf_offset = 0;
107 }
108
109 #[inline]
110 fn buf_left_shift(&mut self, distance: usize) {
111 debug_assert!(self.buf_length >= distance);
112
113 self.buf_offset += distance;
114
115 if self.buf_offset >= N::USIZE - 4 {
116 self.buf_align_to_frond_end();
117 }
118
119 self.buf_length -= distance;
120 }
121
122 /// 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.
123 #[inline]
124 #[allow(clippy::missing_safety_doc)]
125 pub unsafe fn remove_heading_bytes_from_buffer(&mut self, number_of_bytes: usize) {
126 self.buf_left_shift(number_of_bytes);
127 }
128
129 fn passing_read(&mut self) -> Result<bool, ScannerError> {
130 if self.buf_length == 0 {
131 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
132
133 if size == 0 {
134 return Ok(false);
135 }
136
137 self.buf_length += size;
138
139 if let Some(passing_byte) = self.passing_byte.take() {
140 if self.buf[self.buf_offset] == passing_byte {
141 self.buf_left_shift(1);
142
143 return if size == 1 {
144 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
145
146 if size == 0 {
147 Ok(false)
148 } else {
149 self.buf_length += size;
150
151 Ok(true)
152 }
153 } else {
154 Ok(true)
155 };
156 }
157 }
158
159 Ok(true)
160 } else {
161 Ok(true)
162 }
163 }
164}
165
166impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
167 /// 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)`.
168 ///
169 /// ```rust
170 /// use scanner_rust::Scanner;
171 ///
172 /// let mut sc = Scanner::new("5 c 中文".as_bytes());
173 ///
174 /// assert_eq!(Some('5'), sc.next_char().unwrap());
175 /// assert_eq!(Some(' '), sc.next_char().unwrap());
176 /// assert_eq!(Some('c'), sc.next_char().unwrap());
177 /// assert_eq!(Some(' '), sc.next_char().unwrap());
178 /// assert_eq!(Some('中'), sc.next_char().unwrap());
179 /// assert_eq!(Some('文'), sc.next_char().unwrap());
180 /// assert_eq!(None, sc.next_char().unwrap());
181 /// ```
182 pub fn next_char(&mut self) -> Result<Option<char>, ScannerError> {
183 if !self.passing_read()? {
184 return Ok(None);
185 }
186
187 let e = self.buf[self.buf_offset];
188
189 let width = get_width(e);
190
191 match width {
192 0 => {
193 self.buf_left_shift(1);
194
195 Ok(Some(REPLACEMENT_CHARACTER))
196 },
197 1 => {
198 self.buf_left_shift(1);
199
200 Ok(Some(e as char))
201 },
202 _ => {
203 while self.buf_length < width {
204 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..]) {
205 Ok(0) => {
206 self.buf_left_shift(1);
207
208 return Ok(Some(REPLACEMENT_CHARACTER));
209 },
210 Ok(c) => self.buf_length += c,
211 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
212 Err(err) => return Err(err.into()),
213 }
214 }
215
216 let char_str_bytes = &self.buf[self.buf_offset..(self.buf_offset + width)];
217
218 match from_utf8(char_str_bytes) {
219 Ok(char_str) => {
220 let c = char_str.chars().next();
221
222 self.buf_left_shift(width);
223
224 Ok(c)
225 },
226 Err(_) => {
227 self.buf_left_shift(1);
228
229 Ok(Some(REPLACEMENT_CHARACTER))
230 },
231 }
232 },
233 }
234 }
235
236 /// 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)`.
237 ///
238 /// ```rust
239 /// use scanner_rust::Scanner;
240 ///
241 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
242 ///
243 /// assert_eq!(Some("123 456".into()), sc.next_line().unwrap());
244 /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
245 /// assert_eq!(Some("".into()), sc.next_line().unwrap());
246 /// assert_eq!(Some(" 中文 ".into()), sc.next_line().unwrap());
247 /// ```
248 pub fn next_line(&mut self) -> Result<Option<String>, ScannerError> {
249 if !self.passing_read()? {
250 return Ok(None);
251 }
252
253 let mut temp = String::new();
254
255 loop {
256 let e = self.buf[self.buf_offset];
257
258 let width = get_width(e);
259
260 match width {
261 0 => {
262 self.buf_left_shift(1);
263
264 temp.push(REPLACEMENT_CHARACTER);
265 },
266 1 => {
267 match e {
268 b'\n' => {
269 if self.buf_length == 1 {
270 self.passing_byte = Some(b'\r');
271 self.buf_left_shift(1);
272 } else if self.buf[self.buf_offset + 1] == b'\r' {
273 self.buf_left_shift(2);
274 } else {
275 self.buf_left_shift(1);
276 }
277
278 return Ok(Some(temp));
279 },
280 b'\r' => {
281 if self.buf_length == 1 {
282 self.passing_byte = Some(b'\n');
283 self.buf_left_shift(1);
284 } else if self.buf[self.buf_offset + 1] == b'\n' {
285 self.buf_left_shift(2);
286 } else {
287 self.buf_left_shift(1);
288 }
289
290 return Ok(Some(temp));
291 },
292 _ => (),
293 }
294
295 self.buf_left_shift(1);
296
297 temp.push(e as char);
298 },
299 _ => {
300 while self.buf_length < width {
301 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
302 {
303 Ok(0) => {
304 temp.push_str(
305 String::from_utf8_lossy(
306 &self.buf
307 [self.buf_offset..(self.buf_offset + self.buf_length)],
308 )
309 .as_ref(),
310 );
311
312 self.buf_left_shift(self.buf_length);
313
314 return Ok(Some(temp));
315 },
316 Ok(c) => self.buf_length += c,
317 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
318 Err(err) => return Err(err.into()),
319 }
320 }
321
322 let char_str_bytes = &self.buf[self.buf_offset..(self.buf_offset + width)];
323
324 match from_utf8(char_str_bytes) {
325 Ok(char_str) => {
326 temp.push_str(char_str);
327
328 self.buf_left_shift(width);
329 },
330 Err(_) => {
331 self.buf_left_shift(1);
332
333 temp.push(REPLACEMENT_CHARACTER);
334 },
335 }
336 },
337 }
338
339 if self.buf_length == 0 {
340 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
341
342 if size == 0 {
343 return Ok(Some(temp));
344 }
345
346 self.buf_length += size;
347 }
348 }
349 }
350
351 /// Read the next line include the tailing line character (or line chracters like `CrLf`(`\r\n`)) without fully validating UTF-8. If there is nothing to read, it will return `Ok(None)`.
352 ///
353 /// ```rust
354 /// use scanner_rust::Scanner;
355 ///
356 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
357 ///
358 /// assert_eq!(Some("123 456".into()), sc.next_line_raw().unwrap());
359 /// assert_eq!(Some("789 ".into()), sc.next_line_raw().unwrap());
360 /// assert_eq!(Some("".into()), sc.next_line_raw().unwrap());
361 /// assert_eq!(Some(" 中文 ".into()), sc.next_line_raw().unwrap());
362 /// ```
363 pub fn next_line_raw(&mut self) -> Result<Option<Vec<u8>>, ScannerError> {
364 if !self.passing_read()? {
365 return Ok(None);
366 }
367
368 let mut temp = Vec::new();
369
370 loop {
371 let e = self.buf[self.buf_offset];
372
373 let width = get_width(e);
374
375 match width {
376 0 => {
377 self.buf_left_shift(1);
378
379 temp.push(e);
380 },
381 1 => {
382 match e {
383 b'\n' => {
384 if self.buf_length == 1 {
385 self.passing_byte = Some(b'\r');
386 self.buf_left_shift(1);
387 } else if self.buf[self.buf_offset + 1] == b'\r' {
388 self.buf_left_shift(2);
389 } else {
390 self.buf_left_shift(1);
391 }
392
393 return Ok(Some(temp));
394 },
395 b'\r' => {
396 if self.buf_length == 1 {
397 self.passing_byte = Some(b'\n');
398 self.buf_left_shift(1);
399 } else if self.buf[self.buf_offset + 1] == b'\n' {
400 self.buf_left_shift(2);
401 } else {
402 self.buf_left_shift(1);
403 }
404
405 return Ok(Some(temp));
406 },
407 _ => (),
408 }
409
410 self.buf_left_shift(1);
411
412 temp.push(e);
413 },
414 _ => {
415 while self.buf_length < width {
416 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
417 {
418 Ok(0) => {
419 temp.extend_from_slice(
420 &self.buf[self.buf_offset..(self.buf_offset + self.buf_length)],
421 );
422
423 self.buf_left_shift(self.buf_length);
424
425 return Ok(Some(temp));
426 },
427 Ok(c) => self.buf_length += c,
428 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
429 Err(err) => return Err(err.into()),
430 }
431 }
432
433 let char_str_bytes = &self.buf[self.buf_offset..(self.buf_offset + width)];
434
435 temp.extend_from_slice(char_str_bytes);
436
437 self.buf_left_shift(width);
438 },
439 }
440
441 if self.buf_length == 0 {
442 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
443
444 if size == 0 {
445 return Ok(Some(temp));
446 }
447
448 self.buf_length += size;
449 }
450 }
451 }
452
453 /// 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.
454 ///
455 /// ```rust
456 /// use scanner_rust::Scanner;
457 ///
458 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
459 ///
460 /// assert_eq!(Some(7), sc.drop_next_line().unwrap());
461 /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
462 /// assert_eq!(Some(0), sc.drop_next_line().unwrap());
463 /// assert_eq!(Some(" 中文 ".into()), sc.next_line().unwrap());
464 /// assert_eq!(None, sc.drop_next_line().unwrap());
465 /// ```
466 pub fn drop_next_line(&mut self) -> Result<Option<usize>, ScannerError> {
467 if !self.passing_read()? {
468 return Ok(None);
469 }
470
471 let mut c = 0;
472
473 loop {
474 let e = self.buf[self.buf_offset];
475
476 let width = get_width(e);
477
478 match width {
479 0 => {
480 self.buf_left_shift(1);
481
482 c += 1;
483 },
484 1 => {
485 match e {
486 b'\n' => {
487 if self.buf_length == 1 {
488 self.passing_byte = Some(b'\r');
489 self.buf_left_shift(1);
490 } else if self.buf[self.buf_offset + 1] == b'\r' {
491 self.buf_left_shift(2);
492 } else {
493 self.buf_left_shift(1);
494 }
495
496 return Ok(Some(c));
497 },
498 b'\r' => {
499 if self.buf_length == 1 {
500 self.passing_byte = Some(b'\n');
501 self.buf_left_shift(1);
502 } else if self.buf[self.buf_offset + 1] == b'\n' {
503 self.buf_left_shift(2);
504 } else {
505 self.buf_left_shift(1);
506 }
507
508 return Ok(Some(c));
509 },
510 _ => (),
511 }
512
513 self.buf_left_shift(1);
514
515 c += 1;
516 },
517 _ => {
518 while self.buf_length < width {
519 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
520 {
521 Ok(0) => {
522 self.buf_left_shift(self.buf_length);
523 c += self.buf_length;
524
525 return Ok(Some(c));
526 },
527 Ok(c) => self.buf_length += c,
528 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
529 Err(err) => return Err(err.into()),
530 }
531 }
532
533 self.buf_left_shift(width);
534 c += width;
535 },
536 }
537
538 if self.buf_length == 0 {
539 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
540
541 if size == 0 {
542 return Ok(Some(c));
543 }
544
545 self.buf_length += size;
546 }
547 }
548 }
549}
550
551impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
552 /// Skip the next whitespaces (`javaWhitespace`). If there is nothing to read, it will return `Ok(false)`.
553 ///
554 /// ```rust
555 /// use scanner_rust::Scanner;
556 ///
557 /// let mut sc = Scanner::new("1 2 c".as_bytes());
558 ///
559 /// assert_eq!(Some('1'), sc.next_char().unwrap());
560 /// assert_eq!(Some(' '), sc.next_char().unwrap());
561 /// assert_eq!(Some('2'), sc.next_char().unwrap());
562 /// assert_eq!(true, sc.skip_whitespaces().unwrap());
563 /// assert_eq!(Some('c'), sc.next_char().unwrap());
564 /// assert_eq!(false, sc.skip_whitespaces().unwrap());
565 /// ```
566 pub fn skip_whitespaces(&mut self) -> Result<bool, ScannerError> {
567 if !self.passing_read()? {
568 return Ok(false);
569 }
570
571 loop {
572 let e = self.buf[self.buf_offset];
573
574 let width = get_width(e);
575
576 match width {
577 0 => {
578 break;
579 },
580 1 => {
581 if !is_whitespace_1(e) {
582 break;
583 }
584
585 self.buf_left_shift(1);
586 },
587 3 => {
588 while self.buf_length < width {
589 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
590 {
591 Ok(0) => {
592 return Ok(true);
593 },
594 Ok(c) => self.buf_length += c,
595 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
596 Err(err) => return Err(err.into()),
597 }
598 }
599
600 if is_whitespace_3(
601 self.buf[self.buf_offset],
602 self.buf[self.buf_offset + 1],
603 self.buf[self.buf_offset + 2],
604 ) {
605 self.buf_left_shift(3);
606 } else {
607 break;
608 }
609 },
610 _ => {
611 break;
612 },
613 }
614
615 if self.buf_length == 0 {
616 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
617
618 if size == 0 {
619 return Ok(true);
620 }
621
622 self.buf_length += size;
623 }
624 }
625
626 Ok(true)
627 }
628
629 /// Read the next token separated by whitespaces. If there is nothing to read, it will return `Ok(None)`.
630 ///
631 /// ```rust
632 /// use scanner_rust::Scanner;
633 ///
634 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
635 ///
636 /// assert_eq!(Some("123".into()), sc.next().unwrap());
637 /// assert_eq!(Some("456".into()), sc.next().unwrap());
638 /// assert_eq!(Some("789".into()), sc.next().unwrap());
639 /// assert_eq!(Some("中文".into()), sc.next().unwrap());
640 /// assert_eq!(None, sc.next().unwrap());
641 /// ```
642 #[allow(clippy::should_implement_trait)]
643 pub fn next(&mut self) -> Result<Option<String>, ScannerError> {
644 if !self.skip_whitespaces()? {
645 return Ok(None);
646 }
647
648 if self.buf_length == 0 {
649 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
650
651 if size == 0 {
652 return Ok(None);
653 }
654
655 self.buf_length += size;
656 }
657
658 let mut temp = String::new();
659
660 loop {
661 let e = self.buf[self.buf_offset];
662
663 let width = get_width(e);
664
665 match width {
666 0 => {
667 self.buf_left_shift(1);
668
669 temp.push(REPLACEMENT_CHARACTER);
670 },
671 1 => {
672 if is_whitespace_1(e) {
673 return Ok(Some(temp));
674 }
675
676 self.buf_left_shift(1);
677
678 temp.push(e as char);
679 },
680 3 => {
681 while self.buf_length < width {
682 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
683 {
684 Ok(0) => {
685 temp.push_str(
686 String::from_utf8_lossy(
687 &self.buf
688 [self.buf_offset..(self.buf_offset + self.buf_length)],
689 )
690 .as_ref(),
691 );
692
693 self.buf_left_shift(self.buf_length);
694
695 return Ok(Some(temp));
696 },
697 Ok(c) => self.buf_length += c,
698 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
699 Err(err) => return Err(err.into()),
700 }
701 }
702
703 if is_whitespace_3(
704 self.buf[self.buf_offset],
705 self.buf[self.buf_offset + 1],
706 self.buf[self.buf_offset + 2],
707 ) {
708 return Ok(Some(temp));
709 } else {
710 let char_str_bytes = &self.buf[self.buf_offset..(self.buf_offset + width)];
711
712 match from_utf8(char_str_bytes) {
713 Ok(char_str) => {
714 temp.push_str(char_str);
715
716 self.buf_left_shift(width);
717 },
718 Err(_) => {
719 self.buf_left_shift(1);
720
721 temp.push(REPLACEMENT_CHARACTER);
722 },
723 }
724 }
725 },
726 _ => {
727 while self.buf_length < width {
728 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
729 {
730 Ok(0) => {
731 temp.push_str(
732 String::from_utf8_lossy(
733 &self.buf
734 [self.buf_offset..(self.buf_offset + self.buf_length)],
735 )
736 .as_ref(),
737 );
738
739 self.buf_left_shift(self.buf_length);
740
741 return Ok(Some(temp));
742 },
743 Ok(c) => self.buf_length += c,
744 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
745 Err(err) => return Err(err.into()),
746 }
747 }
748
749 let char_str_bytes = &self.buf[self.buf_offset..(self.buf_offset + width)];
750
751 match from_utf8(char_str_bytes) {
752 Ok(char_str) => {
753 temp.push_str(char_str);
754
755 self.buf_left_shift(width);
756 },
757 Err(_) => {
758 self.buf_left_shift(1);
759
760 temp.push(REPLACEMENT_CHARACTER);
761 },
762 }
763 },
764 }
765
766 if self.buf_length == 0 {
767 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
768
769 if size == 0 {
770 return Ok(Some(temp));
771 }
772
773 self.buf_length += size;
774 }
775 }
776 }
777
778 /// Read the next token separated by whitespaces without fully validating UTF-8. If there is nothing to read, it will return `Ok(None)`.
779 ///
780 /// ```rust
781 /// use scanner_rust::Scanner;
782 ///
783 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
784 ///
785 /// assert_eq!(Some("123".into()), sc.next_raw().unwrap());
786 /// assert_eq!(Some("456".into()), sc.next_raw().unwrap());
787 /// assert_eq!(Some("789".into()), sc.next_raw().unwrap());
788 /// assert_eq!(Some("中文".into()), sc.next_raw().unwrap());
789 /// assert_eq!(None, sc.next_raw().unwrap());
790 /// ```
791 pub fn next_raw(&mut self) -> Result<Option<Vec<u8>>, ScannerError> {
792 if !self.skip_whitespaces()? {
793 return Ok(None);
794 }
795
796 if self.buf_length == 0 {
797 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
798
799 if size == 0 {
800 return Ok(None);
801 }
802
803 self.buf_length += size;
804 }
805
806 let mut temp = Vec::new();
807
808 loop {
809 let e = self.buf[self.buf_offset];
810
811 let width = get_width(e);
812
813 match width {
814 0 => {
815 self.buf_left_shift(1);
816
817 temp.push(e);
818 },
819 1 => {
820 if is_whitespace_1(e) {
821 return Ok(Some(temp));
822 }
823
824 self.buf_left_shift(1);
825
826 temp.push(e);
827 },
828 3 => {
829 while self.buf_length < width {
830 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
831 {
832 Ok(0) => {
833 self.buf_left_shift(self.buf_length);
834
835 return Ok(Some(temp));
836 },
837 Ok(c) => self.buf_length += c,
838 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
839 Err(err) => return Err(err.into()),
840 }
841 }
842
843 if is_whitespace_3(
844 self.buf[self.buf_offset],
845 self.buf[self.buf_offset + 1],
846 self.buf[self.buf_offset + 2],
847 ) {
848 return Ok(Some(temp));
849 } else {
850 let char_str_bytes = &self.buf[self.buf_offset..(self.buf_offset + width)];
851
852 temp.extend_from_slice(char_str_bytes);
853
854 self.buf_left_shift(width);
855 }
856 },
857 _ => {
858 while self.buf_length < width {
859 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
860 {
861 Ok(0) => {
862 temp.extend_from_slice(
863 &self.buf[self.buf_offset..(self.buf_offset + self.buf_length)],
864 );
865
866 self.buf_left_shift(self.buf_length);
867
868 return Ok(Some(temp));
869 },
870 Ok(c) => self.buf_length += c,
871 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
872 Err(err) => return Err(err.into()),
873 }
874 }
875
876 let char_str_bytes = &self.buf[self.buf_offset..(self.buf_offset + width)];
877
878 temp.extend_from_slice(char_str_bytes);
879
880 self.buf_left_shift(width);
881 },
882 }
883
884 if self.buf_length == 0 {
885 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
886
887 if size == 0 {
888 return Ok(Some(temp));
889 }
890
891 self.buf_length += size;
892 }
893 }
894 }
895
896 /// 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.
897 ///
898 /// ```rust
899 /// use scanner_rust::Scanner;
900 ///
901 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
902 ///
903 /// assert_eq!(Some(3), sc.drop_next().unwrap());
904 /// assert_eq!(Some("456".into()), sc.next().unwrap());
905 /// assert_eq!(Some(3), sc.drop_next().unwrap());
906 /// assert_eq!(Some("中文".into()), sc.next().unwrap());
907 /// assert_eq!(None, sc.drop_next().unwrap());
908 /// ```
909 pub fn drop_next(&mut self) -> Result<Option<usize>, ScannerError> {
910 if !self.skip_whitespaces()? {
911 return Ok(None);
912 }
913
914 if self.buf_length == 0 {
915 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
916
917 if size == 0 {
918 return Ok(None);
919 }
920
921 self.buf_length += size;
922 }
923
924 let mut c = 0;
925
926 loop {
927 let e = self.buf[self.buf_offset];
928
929 let width = get_width(e);
930
931 match width {
932 0 => {
933 self.buf_left_shift(1);
934
935 c += 1;
936 },
937 1 => {
938 if is_whitespace_1(e) {
939 return Ok(Some(c));
940 }
941
942 self.buf_left_shift(1);
943
944 c += 1;
945 },
946 3 => {
947 while self.buf_length < width {
948 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
949 {
950 Ok(0) => {
951 self.buf_left_shift(self.buf_length);
952 c += self.buf_length;
953
954 return Ok(Some(c));
955 },
956 Ok(c) => self.buf_length += c,
957 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
958 Err(err) => return Err(err.into()),
959 }
960 }
961
962 if is_whitespace_3(
963 self.buf[self.buf_offset],
964 self.buf[self.buf_offset + 1],
965 self.buf[self.buf_offset + 2],
966 ) {
967 return Ok(Some(c));
968 } else {
969 self.buf_left_shift(width);
970 }
971 },
972 _ => {
973 while self.buf_length < width {
974 match self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])
975 {
976 Ok(0) => {
977 self.buf_left_shift(self.buf_length);
978 c += self.buf_length;
979
980 return Ok(Some(c));
981 },
982 Ok(c) => self.buf_length += c,
983 Err(ref err) if err.kind() == ErrorKind::Interrupted => (),
984 Err(err) => return Err(err.into()),
985 }
986 }
987
988 self.buf_left_shift(width);
989 },
990 }
991
992 if self.buf_length == 0 {
993 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
994
995 if size == 0 {
996 return Ok(Some(c));
997 }
998
999 self.buf_length += size;
1000 }
1001 }
1002 }
1003}
1004
1005impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
1006 /// Read the next bytes. If there is nothing to read, it will return `Ok(None)`.
1007 ///
1008 /// ```rust
1009 /// use scanner_rust::Scanner;
1010 ///
1011 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
1012 ///
1013 /// assert_eq!(Some("123".into()), sc.next_bytes(3).unwrap());
1014 /// assert_eq!(Some(" 456".into()), sc.next_bytes(4).unwrap());
1015 /// assert_eq!(Some("\r\n789 ".into()), sc.next_bytes(6).unwrap());
1016 /// assert_eq!(Some("中文".into()), sc.next_raw().unwrap());
1017 /// assert_eq!(Some(" ".into()), sc.next_bytes(2).unwrap());
1018 /// assert_eq!(None, sc.next_bytes(2).unwrap());
1019 /// ```
1020 pub fn next_bytes(
1021 &mut self,
1022 max_number_of_bytes: usize,
1023 ) -> Result<Option<Vec<u8>>, ScannerError> {
1024 if !self.passing_read()? {
1025 return Ok(None);
1026 }
1027
1028 let mut temp = Vec::new();
1029 let mut c = 0;
1030
1031 while c < max_number_of_bytes {
1032 if self.buf_length == 0 {
1033 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
1034
1035 if size == 0 {
1036 return Ok(Some(temp));
1037 }
1038
1039 self.buf_length += size;
1040 }
1041
1042 let dropping_bytes = self.buf_length.min(max_number_of_bytes - c);
1043
1044 temp.extend_from_slice(&self.buf[self.buf_offset..(self.buf_offset + dropping_bytes)]);
1045
1046 self.buf_left_shift(dropping_bytes);
1047
1048 c += dropping_bytes;
1049 }
1050
1051 Ok(Some(temp))
1052 }
1053
1054 /// 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.
1055 ///
1056 /// ```rust
1057 /// use scanner_rust::Scanner;
1058 ///
1059 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
1060 ///
1061 /// assert_eq!(Some(7), sc.drop_next_bytes(7).unwrap());
1062 /// assert_eq!(Some("".into()), sc.next_line().unwrap());
1063 /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
1064 /// assert_eq!(Some(1), sc.drop_next_bytes(1).unwrap());
1065 /// assert_eq!(Some(" 中文 ".into()), sc.next_line().unwrap());
1066 /// assert_eq!(None, sc.drop_next_bytes(1).unwrap());
1067 /// ```
1068 pub fn drop_next_bytes(
1069 &mut self,
1070 max_number_of_bytes: usize,
1071 ) -> Result<Option<usize>, ScannerError> {
1072 if !self.passing_read()? {
1073 return Ok(None);
1074 }
1075
1076 let mut c = 0;
1077
1078 while c < max_number_of_bytes {
1079 if self.buf_length == 0 {
1080 let size = self.reader.read(&mut self.buf[self.buf_offset..])?;
1081
1082 if size == 0 {
1083 return Ok(Some(c));
1084 }
1085
1086 self.buf_length += size;
1087 }
1088
1089 let dropping_bytes = self.buf_length.min(max_number_of_bytes - c);
1090
1091 self.buf_left_shift(dropping_bytes);
1092
1093 c += dropping_bytes;
1094 }
1095
1096 Ok(Some(c))
1097 }
1098}
1099
1100impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
1101 /// Read the next text until it reaches a specific boundary. 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("123 456\r\n789 \n\n 中文 ".as_bytes());
1107 ///
1108 /// assert_eq!(Some("123".into()), sc.next_until(" ").unwrap());
1109 /// assert_eq!(Some("456\r".into()), sc.next_until("\n").unwrap());
1110 /// assert_eq!(Some("78".into()), sc.next_until("9 ").unwrap());
1111 /// assert_eq!(Some("\n\n 中文 ".into()), sc.next_until("kk").unwrap());
1112 /// assert_eq!(None, sc.next().unwrap());
1113 /// ```
1114 pub fn next_until<S: AsRef<str>>(
1115 &mut self,
1116 boundary: S,
1117 ) -> Result<Option<String>, ScannerError> {
1118 if !self.passing_read()? {
1119 return Ok(None);
1120 }
1121
1122 let boundary = boundary.as_ref().as_bytes();
1123 let boundary_length = boundary.len();
1124 let mut temp = String::new();
1125
1126 let mut b = 0;
1127
1128 loop {
1129 let mut p = 0;
1130
1131 while p < self.buf_length {
1132 if self.buf[self.buf_offset + p] == boundary[b] {
1133 b += 1;
1134 p += 1;
1135
1136 if b == boundary_length {
1137 match p.cmp(&boundary_length) {
1138 Ordering::Equal => (),
1139 Ordering::Greater => {
1140 temp.push_str(
1141 String::from_utf8_lossy(
1142 &self.buf[self.buf_offset
1143 ..(self.buf_offset + p - boundary_length)],
1144 )
1145 .as_ref(),
1146 );
1147 },
1148 Ordering::Less => {
1149 let adjusted_temp_length = temp.len() - (boundary_length - p);
1150
1151 unsafe {
1152 temp.as_mut_vec().set_len(adjusted_temp_length);
1153 }
1154 },
1155 }
1156
1157 self.buf_left_shift(p);
1158
1159 return Ok(Some(temp));
1160 }
1161 } else {
1162 b = 0;
1163 p += 1;
1164 }
1165 }
1166
1167 let mut utf8_length = 0;
1168
1169 loop {
1170 let width = get_width(self.buf[self.buf_offset + utf8_length]).max(1);
1171
1172 utf8_length += width;
1173
1174 match utf8_length.cmp(&self.buf_length) {
1175 Ordering::Equal => break,
1176 Ordering::Greater => {
1177 utf8_length -= width;
1178 break;
1179 },
1180 Ordering::Less => (),
1181 }
1182 }
1183
1184 temp.push_str(
1185 String::from_utf8_lossy(
1186 &self.buf[self.buf_offset..(self.buf_offset + utf8_length)],
1187 )
1188 .as_ref(),
1189 );
1190
1191 self.buf_left_shift(utf8_length);
1192
1193 let size = self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])?;
1194
1195 if size == 0 {
1196 return Ok(Some(temp));
1197 }
1198
1199 self.buf_length += size;
1200 }
1201 }
1202
1203 /// 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)`.
1204 ///
1205 /// ```rust
1206 /// use scanner_rust::Scanner;
1207 ///
1208 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
1209 ///
1210 /// assert_eq!(Some("123".into()), sc.next_until_raw(" ").unwrap());
1211 /// assert_eq!(Some("456\r".into()), sc.next_until_raw("\n").unwrap());
1212 /// assert_eq!(Some("78".into()), sc.next_until_raw("9 ").unwrap());
1213 /// assert_eq!(Some("\n\n 中文 ".into()), sc.next_until_raw("kk").unwrap());
1214 /// assert_eq!(None, sc.next().unwrap());
1215 /// ```
1216 pub fn next_until_raw<D: ?Sized + AsRef<[u8]>>(
1217 &mut self,
1218 boundary: &D,
1219 ) -> Result<Option<Vec<u8>>, ScannerError> {
1220 if !self.passing_read()? {
1221 return Ok(None);
1222 }
1223
1224 let boundary = boundary.as_ref();
1225 let boundary_length = boundary.len();
1226 let mut temp = Vec::new();
1227
1228 let mut b = 0;
1229
1230 loop {
1231 let mut p = 0;
1232
1233 while p < self.buf_length {
1234 if self.buf[self.buf_offset + p] == boundary[b] {
1235 b += 1;
1236 p += 1;
1237
1238 if b == boundary_length {
1239 match p.cmp(&boundary_length) {
1240 Ordering::Equal => (),
1241 Ordering::Greater => {
1242 temp.extend_from_slice(
1243 &self.buf
1244 [self.buf_offset..(self.buf_offset + p - boundary_length)],
1245 );
1246 },
1247 Ordering::Less => {
1248 let adjusted_temp_length = temp.len() - (boundary_length - p);
1249
1250 unsafe {
1251 temp.set_len(adjusted_temp_length);
1252 }
1253 },
1254 }
1255
1256 self.buf_left_shift(p);
1257
1258 return Ok(Some(temp));
1259 }
1260 } else {
1261 b = 0;
1262 p += 1;
1263 }
1264 }
1265
1266 let mut utf8_length = 0;
1267
1268 loop {
1269 let width = get_width(self.buf[self.buf_offset + utf8_length]).max(1);
1270
1271 utf8_length += width;
1272
1273 match utf8_length.cmp(&self.buf_length) {
1274 Ordering::Equal => break,
1275 Ordering::Greater => {
1276 utf8_length -= width;
1277 break;
1278 },
1279 Ordering::Less => (),
1280 }
1281 }
1282
1283 temp.extend_from_slice(&self.buf[self.buf_offset..(self.buf_offset + utf8_length)]);
1284
1285 self.buf_left_shift(utf8_length);
1286
1287 let size = self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])?;
1288
1289 if size == 0 {
1290 return Ok(Some(temp));
1291 }
1292
1293 self.buf_length += size;
1294 }
1295 }
1296
1297 /// Drop the next data until it reaches a specific boundary. If there is nothing to read, it will return `Ok(None)`.
1298 ///
1299 /// ```rust
1300 /// use scanner_rust::Scanner;
1301 ///
1302 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
1303 ///
1304 /// assert_eq!(Some(7), sc.drop_next_until("\r\n").unwrap());
1305 /// assert_eq!(Some("789 ".into()), sc.next_line().unwrap());
1306 /// assert_eq!(Some(0), sc.drop_next_until("\n").unwrap());
1307 /// assert_eq!(Some(" 中文 ".into()), sc.next_line().unwrap());
1308 /// assert_eq!(None, sc.drop_next_until("").unwrap());
1309 /// ```
1310 pub fn drop_next_until<D: ?Sized + AsRef<[u8]>>(
1311 &mut self,
1312 boundary: &D,
1313 ) -> Result<Option<usize>, ScannerError> {
1314 if !self.passing_read()? {
1315 return Ok(None);
1316 }
1317
1318 let boundary = boundary.as_ref();
1319 let boundary_length = boundary.len();
1320 let mut c = 0;
1321
1322 let mut b = 0;
1323
1324 loop {
1325 let mut p = 0;
1326
1327 while p < self.buf_length {
1328 if self.buf[self.buf_offset + p] == boundary[b] {
1329 b += 1;
1330 p += 1;
1331
1332 if b == boundary_length {
1333 match p.cmp(&boundary_length) {
1334 Ordering::Equal => (),
1335 Ordering::Greater => {
1336 c += p - boundary_length;
1337 },
1338 Ordering::Less => {
1339 c -= boundary_length - p;
1340 },
1341 }
1342
1343 self.buf_left_shift(p);
1344
1345 return Ok(Some(c));
1346 }
1347 } else {
1348 b = 0;
1349 p += 1;
1350 }
1351 }
1352
1353 let mut utf8_length = 0;
1354
1355 loop {
1356 let width = get_width(self.buf[self.buf_offset + utf8_length]).max(1);
1357
1358 utf8_length += width;
1359
1360 match utf8_length.cmp(&self.buf_length) {
1361 Ordering::Equal => break,
1362 Ordering::Greater => {
1363 utf8_length -= width;
1364 break;
1365 },
1366 Ordering::Less => (),
1367 }
1368 }
1369
1370 c += utf8_length;
1371
1372 self.buf_left_shift(utf8_length);
1373
1374 let size = self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])?;
1375
1376 if size == 0 {
1377 return Ok(Some(c));
1378 }
1379
1380 self.buf_length += size;
1381 }
1382 }
1383}
1384
1385impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
1386 /// Try to fill up the buffer and return the immutable byte slice of the valid buffered data.
1387 /// 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`.
1388 ///
1389 /// ```rust
1390 /// use scanner_rust::Scanner;
1391 ///
1392 /// let mut sc = Scanner::new("123 456\r\n789 \n\n 中文 ".as_bytes());
1393 ///
1394 /// assert_eq!("123 456\r\n789 \n\n 中文 ".as_bytes(), sc.peek(false).unwrap());
1395 /// ```
1396 #[inline]
1397 pub fn peek(&mut self, shift: bool) -> Result<&[u8], ScannerError> {
1398 if shift {
1399 self.buf_align_to_frond_end();
1400 }
1401
1402 loop {
1403 let size = self.reader.read(&mut self.buf[(self.buf_offset + self.buf_length)..])?;
1404
1405 if size == 0 {
1406 break;
1407 }
1408
1409 self.buf_length += size;
1410 }
1411
1412 Ok(&self.buf[self.buf_offset..(self.buf_offset + self.buf_length)])
1413 }
1414}
1415
1416impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
1417 #[inline]
1418 fn next_raw_parse<T: FromStr>(&mut self) -> Result<Option<T>, ScannerError>
1419 where
1420 ScannerError: From<<T as FromStr>::Err>, {
1421 let result = self.next_raw()?;
1422
1423 match result {
1424 Some(s) => Ok(Some(unsafe { from_utf8_unchecked(&s) }.parse()?)),
1425 None => Ok(None),
1426 }
1427 }
1428
1429 /// 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)`.
1430 ///
1431 /// ```rust
1432 /// use scanner_rust::Scanner;
1433 ///
1434 /// let mut sc = Scanner::new("1 2".as_bytes());
1435 ///
1436 /// assert_eq!(Some(1), sc.next_u8().unwrap());
1437 /// assert_eq!(Some(2), sc.next_u8().unwrap());
1438 /// ```
1439 #[inline]
1440 pub fn next_u8(&mut self) -> Result<Option<u8>, ScannerError> {
1441 self.next_raw_parse()
1442 }
1443
1444 /// 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)`.
1445 ///
1446 /// ```rust
1447 /// use scanner_rust::Scanner;
1448 ///
1449 /// let mut sc = Scanner::new("1 2".as_bytes());
1450 ///
1451 /// assert_eq!(Some(1), sc.next_u16().unwrap());
1452 /// assert_eq!(Some(2), sc.next_u16().unwrap());
1453 /// ```
1454 #[inline]
1455 pub fn next_u16(&mut self) -> Result<Option<u16>, ScannerError> {
1456 self.next_raw_parse()
1457 }
1458
1459 /// 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)`.
1460 ///
1461 /// ```rust
1462 /// use scanner_rust::Scanner;
1463 ///
1464 /// let mut sc = Scanner::new("1 2".as_bytes());
1465 ///
1466 /// assert_eq!(Some(1), sc.next_u32().unwrap());
1467 /// assert_eq!(Some(2), sc.next_u32().unwrap());
1468 /// ```
1469 #[inline]
1470 pub fn next_u32(&mut self) -> Result<Option<u32>, ScannerError> {
1471 self.next_raw_parse()
1472 }
1473
1474 /// 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)`.
1475 ///
1476 /// ```rust
1477 /// use scanner_rust::Scanner;
1478 ///
1479 /// let mut sc = Scanner::new("1 2".as_bytes());
1480 ///
1481 /// assert_eq!(Some(1), sc.next_u64().unwrap());
1482 /// assert_eq!(Some(2), sc.next_u64().unwrap());
1483 /// ```
1484 #[inline]
1485 pub fn next_u64(&mut self) -> Result<Option<u64>, ScannerError> {
1486 self.next_raw_parse()
1487 }
1488
1489 /// 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)`.
1490 ///
1491 /// ```rust
1492 /// use scanner_rust::Scanner;
1493 ///
1494 /// let mut sc = Scanner::new("1 2".as_bytes());
1495 ///
1496 /// assert_eq!(Some(1), sc.next_u128().unwrap());
1497 /// assert_eq!(Some(2), sc.next_u128().unwrap());
1498 /// ```
1499 #[inline]
1500 pub fn next_u128(&mut self) -> Result<Option<u128>, ScannerError> {
1501 self.next_raw_parse()
1502 }
1503
1504 /// 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)`.
1505 ///
1506 /// ```rust
1507 /// use scanner_rust::Scanner;
1508 ///
1509 /// let mut sc = Scanner::new("1 2".as_bytes());
1510 ///
1511 /// assert_eq!(Some(1), sc.next_usize().unwrap());
1512 /// assert_eq!(Some(2), sc.next_usize().unwrap());
1513 /// ```
1514 #[inline]
1515 pub fn next_usize(&mut self) -> Result<Option<usize>, ScannerError> {
1516 self.next_raw_parse()
1517 }
1518
1519 /// 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)`.
1520 ///
1521 /// ```rust
1522 /// use scanner_rust::Scanner;
1523 ///
1524 /// let mut sc = Scanner::new("1 2".as_bytes());
1525 ///
1526 /// assert_eq!(Some(1), sc.next_i8().unwrap());
1527 /// assert_eq!(Some(2), sc.next_i8().unwrap());
1528 /// ```
1529 #[inline]
1530 pub fn next_i8(&mut self) -> Result<Option<i8>, ScannerError> {
1531 self.next_raw_parse()
1532 }
1533
1534 /// 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)`.
1535 ///
1536 /// ```rust
1537 /// use scanner_rust::Scanner;
1538 ///
1539 /// let mut sc = Scanner::new("1 2".as_bytes());
1540 ///
1541 /// assert_eq!(Some(1), sc.next_i16().unwrap());
1542 /// assert_eq!(Some(2), sc.next_i16().unwrap());
1543 /// ```
1544 #[inline]
1545 pub fn next_i16(&mut self) -> Result<Option<i16>, ScannerError> {
1546 self.next_raw_parse()
1547 }
1548
1549 /// 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)`.
1550 ///
1551 /// ```rust
1552 /// use scanner_rust::Scanner;
1553 ///
1554 /// let mut sc = Scanner::new("1 2".as_bytes());
1555 ///
1556 /// assert_eq!(Some(1), sc.next_i32().unwrap());
1557 /// assert_eq!(Some(2), sc.next_i32().unwrap());
1558 /// ```
1559 #[inline]
1560 pub fn next_i32(&mut self) -> Result<Option<i32>, ScannerError> {
1561 self.next_raw_parse()
1562 }
1563
1564 /// 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)`.
1565 ///
1566 /// ```rust
1567 /// use scanner_rust::Scanner;
1568 ///
1569 /// let mut sc = Scanner::new("1 2".as_bytes());
1570 ///
1571 /// assert_eq!(Some(1), sc.next_i64().unwrap());
1572 /// assert_eq!(Some(2), sc.next_i64().unwrap());
1573 /// ```
1574 #[inline]
1575 pub fn next_i64(&mut self) -> Result<Option<i64>, ScannerError> {
1576 self.next_raw_parse()
1577 }
1578
1579 /// 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)`.
1580 ///
1581 /// ```rust
1582 /// use scanner_rust::Scanner;
1583 ///
1584 /// let mut sc = Scanner::new("1 2".as_bytes());
1585 ///
1586 /// assert_eq!(Some(1), sc.next_i128().unwrap());
1587 /// assert_eq!(Some(2), sc.next_i128().unwrap());
1588 /// ```
1589 #[inline]
1590 pub fn next_i128(&mut self) -> Result<Option<i128>, ScannerError> {
1591 self.next_raw_parse()
1592 }
1593
1594 /// 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)`.
1595 ///
1596 /// ```rust
1597 /// use scanner_rust::Scanner;
1598 ///
1599 /// let mut sc = Scanner::new("1 2".as_bytes());
1600 ///
1601 /// assert_eq!(Some(1), sc.next_isize().unwrap());
1602 /// assert_eq!(Some(2), sc.next_isize().unwrap());
1603 /// ```
1604 #[inline]
1605 pub fn next_isize(&mut self) -> Result<Option<isize>, ScannerError> {
1606 self.next_raw_parse()
1607 }
1608
1609 /// 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)`.
1610 ///
1611 /// ```rust
1612 /// use scanner_rust::Scanner;
1613 ///
1614 /// let mut sc = Scanner::new("1 2.5".as_bytes());
1615 ///
1616 /// assert_eq!(Some(1.0), sc.next_f32().unwrap());
1617 /// assert_eq!(Some(2.5), sc.next_f32().unwrap());
1618 /// ```
1619 #[inline]
1620 pub fn next_f32(&mut self) -> Result<Option<f32>, ScannerError> {
1621 self.next_raw_parse()
1622 }
1623
1624 /// 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)`.
1625 ///
1626 /// ```rust
1627 /// use scanner_rust::Scanner;
1628 ///
1629 /// let mut sc = Scanner::new("1 2.5".as_bytes());
1630 ///
1631 /// assert_eq!(Some(1.0), sc.next_f64().unwrap());
1632 /// assert_eq!(Some(2.5), sc.next_f64().unwrap());
1633 /// ```
1634 #[inline]
1635 pub fn next_f64(&mut self) -> Result<Option<f64>, ScannerError> {
1636 self.next_raw_parse()
1637 }
1638}
1639
1640impl<R: Read, N: ArrayLength + IsGreaterOrEqual<U4, Output = True>> Scanner<R, N> {
1641 #[inline]
1642 fn next_until_raw_parse<T: FromStr, D: ?Sized + AsRef<[u8]>>(
1643 &mut self,
1644 boundary: &D,
1645 ) -> Result<Option<T>, ScannerError>
1646 where
1647 ScannerError: From<<T as FromStr>::Err>, {
1648 let result = self.next_until_raw(boundary)?;
1649
1650 match result {
1651 Some(s) => Ok(Some(unsafe { from_utf8_unchecked(&s) }.parse()?)),
1652 None => Ok(None),
1653 }
1654 }
1655
1656 /// 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)`.
1657 ///
1658 /// ```rust
1659 /// use scanner_rust::Scanner;
1660 ///
1661 /// let mut sc = Scanner::new("1 2".as_bytes());
1662 ///
1663 /// assert_eq!(Some(1), sc.next_u8_until(" ").unwrap());
1664 /// assert_eq!(Some(2), sc.next_u8_until(" ").unwrap());
1665 /// ```
1666 #[inline]
1667 pub fn next_u8_until<D: ?Sized + AsRef<[u8]>>(
1668 &mut self,
1669 boundary: &D,
1670 ) -> Result<Option<u8>, ScannerError> {
1671 self.next_until_raw_parse(boundary)
1672 }
1673
1674 /// 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)`.
1675 ///
1676 /// ```rust
1677 /// use scanner_rust::Scanner;
1678 ///
1679 /// let mut sc = Scanner::new("1 2".as_bytes());
1680 ///
1681 /// assert_eq!(Some(1), sc.next_u16_until(" ").unwrap());
1682 /// assert_eq!(Some(2), sc.next_u16_until(" ").unwrap());
1683 /// ```
1684 #[inline]
1685 pub fn next_u16_until<D: ?Sized + AsRef<[u8]>>(
1686 &mut self,
1687 boundary: &D,
1688 ) -> Result<Option<u16>, ScannerError> {
1689 self.next_until_raw_parse(boundary)
1690 }
1691
1692 /// 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)`.
1693 ///
1694 /// ```rust
1695 /// use scanner_rust::Scanner;
1696 ///
1697 /// let mut sc = Scanner::new("1 2".as_bytes());
1698 ///
1699 /// assert_eq!(Some(1), sc.next_u32_until(" ").unwrap());
1700 /// assert_eq!(Some(2), sc.next_u32_until(" ").unwrap());
1701 /// ```
1702 #[inline]
1703 pub fn next_u32_until<D: ?Sized + AsRef<[u8]>>(
1704 &mut self,
1705 boundary: &D,
1706 ) -> Result<Option<u32>, ScannerError> {
1707 self.next_until_raw_parse(boundary)
1708 }
1709
1710 /// 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)`.
1711 ///
1712 /// ```rust
1713 /// use scanner_rust::Scanner;
1714 ///
1715 /// let mut sc = Scanner::new("1 2".as_bytes());
1716 ///
1717 /// assert_eq!(Some(1), sc.next_u64_until(" ").unwrap());
1718 /// assert_eq!(Some(2), sc.next_u64_until(" ").unwrap());
1719 /// ```
1720 #[inline]
1721 pub fn next_u64_until<D: ?Sized + AsRef<[u8]>>(
1722 &mut self,
1723 boundary: &D,
1724 ) -> Result<Option<u64>, ScannerError> {
1725 self.next_until_raw_parse(boundary)
1726 }
1727
1728 /// 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)`.
1729 ///
1730 /// ```rust
1731 /// use scanner_rust::Scanner;
1732 ///
1733 /// let mut sc = Scanner::new("1 2".as_bytes());
1734 ///
1735 /// assert_eq!(Some(1), sc.next_u128_until(" ").unwrap());
1736 /// assert_eq!(Some(2), sc.next_u128_until(" ").unwrap());
1737 /// ```
1738 #[inline]
1739 pub fn next_u128_until<D: ?Sized + AsRef<[u8]>>(
1740 &mut self,
1741 boundary: &D,
1742 ) -> Result<Option<u128>, ScannerError> {
1743 self.next_until_raw_parse(boundary)
1744 }
1745
1746 /// 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)`.
1747 ///
1748 /// ```rust
1749 /// use scanner_rust::Scanner;
1750 ///
1751 /// let mut sc = Scanner::new("1 2".as_bytes());
1752 ///
1753 /// assert_eq!(Some(1), sc.next_usize_until(" ").unwrap());
1754 /// assert_eq!(Some(2), sc.next_usize_until(" ").unwrap());
1755 /// ```
1756 #[inline]
1757 pub fn next_usize_until<D: ?Sized + AsRef<[u8]>>(
1758 &mut self,
1759 boundary: &D,
1760 ) -> Result<Option<usize>, ScannerError> {
1761 self.next_until_raw_parse(boundary)
1762 }
1763
1764 /// 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)`.
1765 ///
1766 /// ```rust
1767 /// use scanner_rust::Scanner;
1768 ///
1769 /// let mut sc = Scanner::new("1 2".as_bytes());
1770 ///
1771 /// assert_eq!(Some(1), sc.next_usize_until(" ").unwrap());
1772 /// assert_eq!(Some(2), sc.next_usize_until(" ").unwrap());
1773 /// ```
1774 #[inline]
1775 pub fn next_i8_until<D: ?Sized + AsRef<[u8]>>(
1776 &mut self,
1777 boundary: &D,
1778 ) -> Result<Option<i8>, ScannerError> {
1779 self.next_until_raw_parse(boundary)
1780 }
1781
1782 /// 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)`.
1783 ///
1784 /// ```rust
1785 /// use scanner_rust::Scanner;
1786 ///
1787 /// let mut sc = Scanner::new("1 2".as_bytes());
1788 ///
1789 /// assert_eq!(Some(1), sc.next_i16_until(" ").unwrap());
1790 /// assert_eq!(Some(2), sc.next_i16_until(" ").unwrap());
1791 /// ```
1792 #[inline]
1793 pub fn next_i16_until<D: ?Sized + AsRef<[u8]>>(
1794 &mut self,
1795 boundary: &D,
1796 ) -> Result<Option<i16>, ScannerError> {
1797 self.next_until_raw_parse(boundary)
1798 }
1799
1800 /// 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)`.
1801 ///
1802 /// ```rust
1803 /// use scanner_rust::Scanner;
1804 ///
1805 /// let mut sc = Scanner::new("1 2".as_bytes());
1806 ///
1807 /// assert_eq!(Some(1), sc.next_i32_until(" ").unwrap());
1808 /// assert_eq!(Some(2), sc.next_i32_until(" ").unwrap());
1809 /// ```
1810 #[inline]
1811 pub fn next_i32_until<D: ?Sized + AsRef<[u8]>>(
1812 &mut self,
1813 boundary: &D,
1814 ) -> Result<Option<i32>, ScannerError> {
1815 self.next_until_raw_parse(boundary)
1816 }
1817
1818 /// 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)`.
1819 ///
1820 /// ```rust
1821 /// use scanner_rust::Scanner;
1822 ///
1823 /// let mut sc = Scanner::new("1 2".as_bytes());
1824 ///
1825 /// assert_eq!(Some(1), sc.next_i64_until(" ").unwrap());
1826 /// assert_eq!(Some(2), sc.next_i64_until(" ").unwrap());
1827 /// ```
1828 #[inline]
1829 pub fn next_i64_until<D: ?Sized + AsRef<[u8]>>(
1830 &mut self,
1831 boundary: &D,
1832 ) -> Result<Option<i64>, ScannerError> {
1833 self.next_until_raw_parse(boundary)
1834 }
1835
1836 /// 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)`.
1837 ///
1838 /// ```rust
1839 /// use scanner_rust::Scanner;
1840 ///
1841 /// let mut sc = Scanner::new("1 2".as_bytes());
1842 ///
1843 /// assert_eq!(Some(1), sc.next_i128_until(" ").unwrap());
1844 /// assert_eq!(Some(2), sc.next_i128_until(" ").unwrap());
1845 /// ```
1846 #[inline]
1847 pub fn next_i128_until<D: ?Sized + AsRef<[u8]>>(
1848 &mut self,
1849 boundary: &D,
1850 ) -> Result<Option<i128>, ScannerError> {
1851 self.next_until_raw_parse(boundary)
1852 }
1853
1854 /// 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)`.
1855 ///
1856 /// ```rust
1857 /// use scanner_rust::Scanner;
1858 ///
1859 /// let mut sc = Scanner::new("1 2".as_bytes());
1860 ///
1861 /// assert_eq!(Some(1), sc.next_isize_until(" ").unwrap());
1862 /// assert_eq!(Some(2), sc.next_isize_until(" ").unwrap());
1863 /// ```
1864 #[inline]
1865 pub fn next_isize_until<D: ?Sized + AsRef<[u8]>>(
1866 &mut self,
1867 boundary: &D,
1868 ) -> Result<Option<isize>, ScannerError> {
1869 self.next_until_raw_parse(boundary)
1870 }
1871
1872 /// 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)`.
1873 ///
1874 /// ```rust
1875 /// use scanner_rust::Scanner;
1876 ///
1877 /// let mut sc = Scanner::new("1 2.5".as_bytes());
1878 ///
1879 /// assert_eq!(Some(1.0), sc.next_f32_until(" ").unwrap());
1880 /// assert_eq!(Some(2.5), sc.next_f32_until(" ").unwrap());
1881 /// ```
1882 #[inline]
1883 pub fn next_f32_until<D: ?Sized + AsRef<[u8]>>(
1884 &mut self,
1885 boundary: &D,
1886 ) -> Result<Option<f32>, ScannerError> {
1887 self.next_until_raw_parse(boundary)
1888 }
1889
1890 /// 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)`.
1891 ///
1892 /// ```rust
1893 /// use scanner_rust::Scanner;
1894 ///
1895 /// let mut sc = Scanner::new("1 2.5".as_bytes());
1896 ///
1897 /// assert_eq!(Some(1.0), sc.next_f64_until(" ").unwrap());
1898 /// assert_eq!(Some(2.5), sc.next_f64_until(" ").unwrap());
1899 /// ```
1900 #[inline]
1901 pub fn next_f64_until<D: ?Sized + AsRef<[u8]>>(
1902 &mut self,
1903 boundary: &D,
1904 ) -> Result<Option<f64>, ScannerError> {
1905 self.next_until_raw_parse(boundary)
1906 }
1907}