byte_parser/
parse_iterator.rs1
2use crate::{
3 pit::PointInTime,
4 ignore_byte::IgnoreByte,
5 while_byte_fn::WhileByteFn,
6 split_on_byte::SplitOnByte,
7 recorder::{Recorder, RecordIter},
8 stop::Stop,
9 expect_byte::ExpectByte
10};
11
12pub trait ParseIterator<'s> {type PointInTime: PointInTime;
22
23 fn slice(&self) -> &'s [u8];
25
26 fn pit(&self) -> Self::PointInTime;
29
30 fn restore_pit(&mut self, pit: Self::PointInTime);
35
36 fn advance(&mut self) -> Option<()>;
38
39 fn recorder(&self) -> Option<&Recorder>;
41
42 fn advance_if<F>(&mut self, advance_if: F) -> Option<bool>
45 where F: FnOnce(&u8) -> bool {
46 match advance_if(&self.peek()?) {
47 true => self.advance().map(|_| true),
48 false => Some(false)
49 }
50 }
51
52 #[inline]
54 fn byte(&self) -> Option<u8> {
55 let pos = self.pit().pos().opt()?;
56 self.slice().get(pos).map(|&a| a)
57 }
58
59 #[inline]
61 fn next(&mut self) -> Option<u8> {
62 self.advance()?;
63 debug_assert!(self.byte().is_some());
65 self.byte()
66 }
67
68 #[inline]
71 fn next_if<F>(&mut self, next_if: F) -> Option<u8>
72 where F: FnOnce(&u8) -> bool {
73 match next_if(&self.peek()?) {
74 true => self.next(),
75 false => None
76 }
77 }
78
79 #[inline]
81 fn peek(&mut self) -> Option<u8> {
82 let pit = self.pit();
83 let n = self.next();
84 self.restore_pit(pit);
85 n
86 }
87
88 #[inline]
90 fn peek_len(&mut self, len: usize) -> Option<&'s [u8]>
91 where Self: Sized {
92 let pit = self.pit();
93 let s = self.record()
94 .consume_len(len)
95 .map(|iter| iter.to_slice())
96 .ok();
97 self.restore_pit(pit);
98 s
99 }
100
101 #[inline]
103 fn peek_at(&mut self, pos: usize) -> Option<u8> {
104 assert!(pos > 0, "peek_at pos must be bigger than 0");
105
106 let pit = self.pit();
107 let n = self.consume_len(pos - 1).ok()
108 .map(|p| p.next())
109 .flatten();
110 self.restore_pit(pit);
111 n
112 }
113
114 #[inline]
129 fn ignore_byte(&mut self, byte: u8) -> IgnoreByte<'_, Self>
130 where Self: Sized {
131 IgnoreByte::new(self, byte)
132 }
133
134 #[inline]
136 fn while_byte_fn<F>(&mut self, f: F) -> WhileByteFn<'_, Self, F>
137 where
138 Self: Sized,
139 F: Fn(&u8) -> bool {
140 WhileByteFn::new(self, f)
141 }
142
143 #[inline]
146 fn consume(&mut self) -> &mut Self {
147 while let Some(_) = self.advance() {}
148 self
149 }
150
151 #[inline]
154 fn consume_and_count(&mut self) -> usize {
155 let mut c = 0;
156 while let Some(_) = self.advance() {
157 c += 1
158 }
159 c
160 }
161
162 #[inline]
165 fn consume_len(&mut self, len: usize) -> Result<&mut Self, usize> {
166 for i in 0..len {
169 self.advance().ok_or(i)?;
170 }
171
172 Ok(self)
173 }
174
175 #[inline]
178 fn consume_at_least(&mut self, len: usize) -> Result<&mut Self, usize> {
179 self.consume_len(len)?;
180 Ok(self.consume())
181 }
182
183 #[inline]
186 fn consume_at_least_and_count(&mut self, len: usize) -> Result<usize, usize> {
187 self.consume_len(len)?;
188 Ok(self.consume_and_count() + len)
189 }
190
191 #[inline]
193 fn consume_while_byte_fn<F>(&mut self, f: F) -> &mut Self
194 where
195 Self: Sized,
196 F: Fn(&u8) -> bool {
197 self.while_byte_fn(f).consume();
198 self
199 }
200
201 #[inline]
203 fn consume_while_byte(&mut self, byte: u8) -> &mut Self
204 where Self: Sized {
205 self.consume_while_byte_fn(|&b| b == byte)
206 }
207
208 #[inline]
233 fn split_on_byte(&mut self, byte: u8) -> SplitOnByte<'_, Self>
234 where Self: Sized {
235 SplitOnByte::new(self, byte)
236 }
237
238 #[inline]
239 fn count_byte(&mut self, byte: u8) -> usize
240 where Self: Sized {
241 self.while_byte_fn(|&b| b == byte)
242 .consume_and_count()
243 }
244
245 #[inline]
247 fn record(&mut self) -> RecordIter<'_, Self>
248 where Self: Sized {
249 RecordIter::new(self)
250 }
251
252 #[inline]
258 fn to_slice(&self) -> &'s [u8] {
259 let start = self.recorder().expect("no recorder found").pos() + 1;
260 let end = self.pit().record_pos() + 1;
261
262 &self.slice()[start..end]
263 }
264
265 #[inline]
272 unsafe fn to_str_unchecked(&self) -> &'s str {
273 std::str::from_utf8_unchecked(self.to_slice())
274 }
275
276 unsafe fn is_valid_utf8() -> bool;
280
281 #[inline]
303 fn to_str(&self) -> &'s str {
304 if unsafe { Self::is_valid_utf8() } {
305 unsafe { self.to_str_unchecked() }
307 } else {
308 std::str::from_utf8(self.to_slice()).expect("invalid utf8")
309 }
310 }
311
312 #[inline]
334 fn try_to_str(&self) -> Result<&'s str, std::str::Utf8Error> {
335 if unsafe { Self::is_valid_utf8() } {
336 Ok(unsafe { self.to_str_unchecked() })
338 } else {
339 std::str::from_utf8(self.to_slice())
340 }
341 }
342
343 #[inline]
349 fn consume_to_slice(&mut self) -> &'s [u8] {
350 self.consume().to_slice()
351 }
352
353 #[inline]
360 unsafe fn consume_to_str_unchecked(&mut self) -> &'s str {
361 self.consume().to_str_unchecked()
362 }
363
364 #[inline]
371 fn consume_to_str(&mut self) -> &'s str {
372 self.consume().to_str()
373 }
374
375 #[inline]
381 fn consume_try_to_str(&mut self) -> Result<&'s str, std::str::Utf8Error> {
382 self.consume().try_to_str()
383 }
384
385 #[inline]
388 fn expect_byte_fn<F>(&mut self, f: F) -> Result<&mut Self, Option<u8>>
389 where F: Fn(u8) -> bool {
390 self.next()
391 .expect_byte_fn(f)
392 .map(|_| self)
393 }
394
395 #[inline]
398 fn expect_byte(&mut self, byte: u8) -> Result<&mut Self, Option<u8>> {
399 self.expect_byte_fn(|b| b == byte)
400 }
401
402 #[inline]
404 fn expect_none(&mut self) -> Result<&mut Self, u8> {
405 match self.next() {
406 Some(b) => Err(b),
407 None => Ok(self)
408 }
409 }
410
411 #[inline]
422 fn stop(&mut self) -> Stop<'_, Self>
423 where Self: Sized {
424 Stop::new(self)
425 }
426
427}
428
429#[cfg(test)]
430mod tests {
431
432 use crate::*;
433
434 #[test]
435 fn test_count_byte() {
436
437 let s = b"baaaab";
438
439 let mut parser = Parser::new( s );
440 assert_eq!( 0, parser.count_byte(b'a') );
441 assert_eq!( b'b', parser.next().unwrap() );
442 assert_eq!( 4, parser.count_byte(b'a') );
443 assert_eq!( b'b', parser.next().unwrap() );
444 assert!( parser.next().is_none() );
445 assert_eq!( 0, parser.count_byte(b'a') );
446
447 }
448
449 #[test]
450 fn combining_multiple_iters() {
451
452 let s = b"ab\raaa\r aab\raa";
453
454 Parser::new(s)
455 .ignore_byte(b'\r')
456 .split_on_byte(b' ')
457 .for_each( |parser| {
458
459 let count_a = parser
462 .ignore_byte(b'b')
463 .count_byte(b'a');
464
465 assert_eq!( count_a, 4 );
466
467 } );
468
469 }
470
471 #[test]
472 fn expect_byte() {
473
474 let s = b"abaa";
475
476 assert!( Parser::new(s)
477 .expect_byte(b'a').unwrap()
478 .expect_byte(b'a').is_err() );
479
480 }
481
482 #[test]
483 fn advance_if() {
484
485 let mut parser = Parser::new(b"ab");
486
487 assert!(parser.advance_if(|&b| b == b'a').unwrap());
488 assert!(!parser.advance_if(|&b| b == b'a').unwrap());
489 assert!(parser.advance_if(|&b| b == b'b').unwrap());
490 assert!(parser.advance_if(|&b| b == b'b').is_none());
491
492 }
493
494 #[test]
495 fn next_if() {
496
497 let mut parser = Parser::new(b"ab");
498
499 assert_eq!(parser.next_if(|&b| b == b'a').unwrap(), b'a');
500 assert!(parser.next_if(|&b| b == b'x').is_none());
501 assert_eq!(parser.next_if(|&b| b == b'b').unwrap(), b'b');
502 assert!(parser.next_if(|&b| b == b'x').is_none());
503
504 }
505
506 #[test]
507 fn peek() {
508
509 let s = b"abaa";
510
511 assert_eq!( b'a', Parser::new(s).peek().unwrap() );
512 assert_eq!( b'a', Parser::new(s).peek_at(1).unwrap() );
513 assert_eq!( b'b', Parser::new(s).peek_at(2).unwrap() );
514 assert_eq!( b'a', Parser::new(s).peek_at(3).unwrap() );
515 assert!( Parser::new(s).peek_at(5).is_none() );
516
517 }
518
519 #[test]
520 fn consume() {
521
522 let mut parser = Parser::new( b"aaa" );
524 assert!( parser.consume().next().is_none() );
525
526 let mut parser = Parser::new( b"aaa" );
528 assert!( parser.consume_len( 1 ).unwrap().next().is_some() );
529
530 let mut parser = Parser::new( b"aaa" );
531 parser.consume();
532 assert!(matches!( parser.consume_len(1), Err(0) ));
533
534 let mut parser = Parser::new( b"aaa" );
536 assert!( parser.consume_at_least( 1 ).is_ok() );
537 assert!( parser.next().is_none() );
538
539 }
540
541}