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