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