1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541

use crate::{
	pit::PointInTime,
	ignore_byte::IgnoreByte,
	while_byte_fn::WhileByteFn,
	split_on_byte::SplitOnByte,
	recorder::{Recorder, RecordIter},
	stop::Stop,
	expect_byte::ExpectByte
};

/// The main trait of this crate.
///
/// This trait allows to parse a slice or a str more easely.
///
/// This trait is lazy, if something should happen you need to `consume` it.  
/// For example, `to_str` only works if you call `record` and `consume` first.
pub trait ParseIterator<'s> {// s for slice

	/// The type that is used to store information about the current position.
	type PointInTime: PointInTime;

	/// Returns the full underlying slice.
	fn slice(&self) -> &'s [u8];

	/// Returns the current position. Should be used in combination with
	/// `restore_pit`.
	fn pit(&self) -> Self::PointInTime;

	/// Restore to a given position.
	///
	/// ## Warning
	/// Only call this method with a pit that was received from this instance.
	fn restore_pit(&mut self, pit: Self::PointInTime);

	/// Advances the internal position.
	fn advance(&mut self) -> Option<()>;

	/// Returns a `Recorder` if recording was started.
	fn recorder(&self) -> Option<&Recorder>;

	/// Advances if `advance_if` returns `true`. 
	/// Returns `None` if the iterator is empty.
	fn advance_if<F>(&mut self, advance_if: F) -> Option<bool>
	where F: FnOnce(&u8) -> bool {
		match advance_if(&self.peek()?) {
			true => self.advance().map(|_| true),
			false => Some(false)
		}
	}

	/// Returns the current byte if it exists.
	#[inline]
	fn byte(&self) -> Option<u8> {
		let pos = self.pit().pos().opt()?;
		self.slice().get(pos).map(|&a| a)
	}

	/// Returns the next byte if it exists and advances the internal position.
	#[inline]
	fn next(&mut self) -> Option<u8> {
		self.advance()?;
		// just check that everything works as expected
		debug_assert!(self.byte().is_some());
		self.byte()
	}

	/// Advances one if `next_if` returns `true`. 
	/// Returns `None` if did not advance.
	#[inline]
	fn next_if<F>(&mut self, next_if: F) -> Option<u8>
	where F: FnOnce(&u8) -> bool {
		match next_if(&self.peek()?) {
			true => self.next(),
			false => None
		}
	}

	/// Returns the next byte without advancing the internal position.
	#[inline]
	fn peek(&mut self) -> Option<u8> {
		let pit = self.pit();
		let n = self.next();
		self.restore_pit(pit);
		n
	}

	/// Returns the next x bytes without advancing the internal position.
	#[inline]
	fn peek_len(&mut self, len: usize) -> Option<&'s [u8]>
	where Self: Sized {
		let pit = self.pit();
		let s = self.record()
			.consume_len(len)
			.map(|iter| iter.to_slice())
			.ok();
		self.restore_pit(pit);
		s
	}

	/// Tries to get the byte at the given position, without advancing.
	#[inline]
	fn peek_at(&mut self, pos: usize) -> Option<u8> {
		assert!(pos > 0, "peek_at pos must be bigger than 0");

		let pit = self.pit();
		let n = self.consume_len(pos - 1).ok()
			.map(|p| p.next())
			.flatten();
		self.restore_pit(pit);
		n
	}

	/// Skips a given byte when calling next.
	///
	/// ## Warning
	/// If you later call `to_slice` or a similar methods
	/// the skipped byte will still be returned.
	///
	/// ## Example
	/// ```
	/// # use byte_parser::{Parser, ParseIterator};
	/// let mut parser = Parser::new(b"abc");
	/// let mut parser = parser.ignore_byte(b'b');
	/// assert_eq!(b'a', parser.next().unwrap());
	/// assert_eq!(b'c', parser.next().unwrap());
	/// ```
	#[inline]
	fn ignore_byte(&mut self, byte: u8) -> IgnoreByte<'_, Self>
	where Self: Sized {
		IgnoreByte::new(self, byte)
	}

	/// Advances while the function returns `true`.
	#[inline]
	fn while_byte_fn<F>(&mut self, f: F) -> WhileByteFn<'_, Self, F>
	where 
		Self: Sized,
		F: Fn(&u8) -> bool {
		WhileByteFn::new(self, f)
	}

	/// Consumes until the iterator is empty. 
	/// Meaning that `advance` returns None.
	#[inline]
	fn consume(&mut self) -> &mut Self {
		while let Some(_) = self.advance() {}
		self
	}

	/// Consumes until the iterator is empty,
	/// and returns how many times advance got called.
	#[inline]
	fn consume_and_count(&mut self) -> usize {
		let mut c = 0;
		while let Some(_) = self.advance() {
			c += 1
		}
		c
	}

	/// Consumes a given length. Returns how much was consumed if could
	/// not consume all.
	#[inline]
	fn consume_len(&mut self, len: usize) -> Result<&mut Self, usize> {
		// we can not just increase the internal position
		// because calling advance could increase the position more than once
		for i in 0..len {
			self.advance().ok_or(i)?;
		}

		Ok(self)
	}

	/// Consumes until the iterator is empty. 
	/// Returns `Err(len)` if could not consume `len`.
	#[inline]
	fn consume_at_least(&mut self, len: usize) -> Result<&mut Self, usize> {
		self.consume_len(len)?;
		Ok(self.consume())
	}

	/// Consumes until the iterator is empty, returning how much was consumed. 
	/// Returns `Err(len)` if could not consume `len`.
	#[inline]
	fn consume_at_least_and_count(&mut self, len: usize) -> Result<usize, usize> {
		self.consume_len(len)?;
		Ok(self.consume_and_count() + len)
	}

	/// Consumes while the function returns `true`.
	#[inline]
	fn consume_while_byte_fn<F>(&mut self, f: F) -> &mut Self
	where 
		Self: Sized,
		F: Fn(&u8) -> bool {
		self.while_byte_fn(f).consume();
		self
	}

	/// Consumes while a give `byte` is returned.
	#[inline]
	fn consume_while_byte(&mut self, byte: u8) -> &mut Self
	where Self: Sized {
		self.consume_while_byte_fn(|&b| b == byte)
	}

	// Consumes while an ascii whitespace is returned.
	// #[inline]
	// fn consume_while_ascii_whitespace(&mut self) -> &mut Self
	// where Self: Sized {
	// 	self.consume_while_byte_fn(u8::is_ascii_whitespace)
	// }

	/// Splits the iterator at a given byte.
	///
	/// ## Example
	/// ```
	/// # use byte_parser::{StrParser, ParseIterator};
	/// let mut parser = StrParser::new("Hello World!");
	/// let mut splitter = parser.split_on_byte(b' ');
	///
	/// let hello = splitter.next().unwrap()
	/// 	.record().consume_to_str();
	/// let world = splitter.next().unwrap()
	/// 	.record().consume_to_str();
	///
	/// assert_eq!(hello, "Hello");
	/// assert_eq!(world, "World!");
	/// assert!(splitter.next().is_none());
	/// ```
	#[inline]
	fn split_on_byte(&mut self, byte: u8) -> SplitOnByte<'_, Self>
	where Self: Sized {
		SplitOnByte::new(self, byte)
	}

	#[inline]
	fn count_byte(&mut self, byte: u8) -> usize
	where Self: Sized {
		self.while_byte_fn(|&b| b == byte)
			.consume_and_count()
	}

	/// Starts a new `Recorder` which starts recording at this position.
	#[inline]
	fn record(&mut self) -> RecordIter<'_, Self>
	where Self: Sized {
		RecordIter::new(self)
	}

	/// Returns a slice from the start of recording until now.
	///
	/// ## Panics
	/// If not called in context of a recorder. Meaning before
	/// calling `record`.
	#[inline]
	fn to_slice(&self) -> &'s [u8] {
		let start = self.recorder().expect("no recorder found").pos() + 1;
		let end = self.pit().record_pos() + 1;

		&self.slice()[start..end]
	}

	/// Returns a `str` from the start of recording until the current position
	/// without checking if the data is valid utf8.
	/// ## Panics
	/// Panics if not called after `record` gets called.
	/// ## Safety
	/// This function is safe if `Self::is_valid_utf8` returns `true`.
	#[inline]
	unsafe fn to_str_unchecked(&self) -> &'s str {
		std::str::from_utf8_unchecked(self.to_slice())
	}

	/// ## Safety
	/// Returning `false` is always safe. 
	/// If you return `true` the entire underlying slice must be valid utf8.
	unsafe fn is_valid_utf8() -> bool;

	/// Returns a `str` from the start of recording until the current position.
	///
	/// ## Example
	/// ```
	/// # use byte_parser::{Parser, StrParser, ParseIterator};
	/// let str_from_slice = Parser::new(b"abc")
	///		.record()
	/// 	.consume()
	/// 	.to_str();
	/// assert_eq!(str_from_slice, "abc");
	///
	/// let str_from_str = StrParser::new("abc")
	/// 	.record()
	/// 	.consume()
	/// 	.to_str();
	/// assert_eq!(str_from_str, "abc");
	/// ```
	///
	/// ## Panics
	/// Panics if not called after `record` was called. 
	/// Or if invalid utf8 is present.
	#[inline]
	fn to_str(&self) -> &'s str {
		if unsafe { Self::is_valid_utf8() } {
			// Safe because is_valid_utf8 guaranties everything is valid utf8
			unsafe { self.to_str_unchecked() }
		} else {
			std::str::from_utf8(self.to_slice()).expect("invalid utf8")
		}
	}

	/// Returns a `str` from the start of recording until the current position.
	///
	/// ## Example
	/// ```
	/// # use byte_parser::{Parser, StrParser, ParseIterator};
	/// let str_from_slice = Parser::new(b"abc")
	///		.record()
	/// 	.consume()
	/// 	.try_to_str().expect("slice is not valid utf8");
	/// assert_eq!(str_from_slice, "abc");
	///
	/// let str_from_str = StrParser::new("abc")
	/// 	.record()
	/// 	.consume()
	/// 		// can never return Err
	/// 	.try_to_str().unwrap();
	/// assert_eq!(str_from_str, "abc");
	/// ```
	///
	/// ## Panics
	/// Panics if not called after `record` was called.
	#[inline]
	fn try_to_str(&self) -> Result<&'s str, std::str::Utf8Error> {
		if unsafe { Self::is_valid_utf8() } {
			// Safe because is_valid_utf8 guaranties everything is valid utf8
			Ok(unsafe { self.to_str_unchecked() })
		} else {
			std::str::from_utf8(self.to_slice())
		}
	}

	/// Consumes the iterator and then returns a slice from the start of recording
	/// until the current position.
	///
	/// ## Panics
	/// Panics if not called after `record` was called.
	#[inline]
	fn consume_to_slice(&mut self) -> &'s [u8] {
		self.consume().to_slice()
	}

	/// Consumes the iterator and then returns a str from the start of recording
	/// until the current position. Without checking if the underlying data
	/// is valid utf8.
	///
	/// ## Panics
	/// Panics if not called after `record` was called.
	#[inline]
	unsafe fn consume_to_str_unchecked(&mut self) -> &'s str {
		self.consume().to_str_unchecked()
	}

	/// Consumes the iterator and then returns a str from the start of recording
	/// until the current position.
	///
	/// ## Panics
	/// Panics if not called after `record` was called or if the data contains invalid
	/// utf8.
	#[inline]
	fn consume_to_str(&mut self) -> &'s str {
		self.consume().to_str()
	}

	/// Consumes the iterator and then returns a str from the start of recording
	/// until the current position if the data is valid utf8.
	///
	/// ## Panics
	/// Panics if not called after `record` was called.
	#[inline]
	fn consume_try_to_str(&mut self) -> Result<&'s str, std::str::Utf8Error> {
		self.consume().try_to_str()
	}

	/// Returns ```&mut Self``` if the function returns `true` on the next byte.
	/// Else returns the byte that was received.
	#[inline]
	fn expect_byte_fn<F>(&mut self, f: F) -> Result<&mut Self, Option<u8>>
	where F: Fn(u8) -> bool {
		self.next()
			.expect_byte_fn(f)
			.map(|_| self)
	}

	/// Returns ```&mut Self``` if the function byte is equal to the next byte.
	/// Else returns the actual byte that was received.
	#[inline]
	fn expect_byte(&mut self, byte: u8) -> Result<&mut Self, Option<u8>> {
		self.expect_byte_fn(|b| b == byte)
	}

	/// Returns ```&mut Self``` if the end was reached (next returns None).
	#[inline]
	fn expect_none(&mut self) -> Result<&mut Self, u8> {
		match self.next() {
			Some(b) => Err(b),
			None => Ok(self)
		}
	}

	/// Returns a `ParseIterator` that always returns None.
	///
	/// ## Example
	/// ```
	/// # use byte_parser::{StrParser, ParseIterator};
	/// let mut s = StrParser::new("abc");
	/// assert_eq!(b'a', s.next().unwrap());
	/// let mut s = s.stop();
	/// assert!(s.next().is_none());
	/// ```
	#[inline]
	fn stop(&mut self) -> Stop<'_, Self>
	where Self: Sized {
		Stop::new(self)
	}

}

#[cfg(test)]
mod tests {

	use crate::*;

	#[test]
	fn test_count_byte() {

		let s = b"baaaab";

		let mut parser = Parser::new( s );
		assert_eq!( 0, parser.count_byte(b'a') );
		assert_eq!( b'b', parser.next().unwrap() );
		assert_eq!( 4, parser.count_byte(b'a') );
		assert_eq!( b'b', parser.next().unwrap() );
		assert!( parser.next().is_none() );
		assert_eq!( 0, parser.count_byte(b'a') );

	}

	#[test]
	fn combining_multiple_iters() {

		let s = b"ab\raaa\r aab\raa";

		Parser::new(s)
			.ignore_byte(b'\r')
			.split_on_byte(b' ')
			.for_each( |parser| {

				// lets ignore b
				// and count a
				let count_a = parser
					.ignore_byte(b'b')
					.count_byte(b'a');

				assert_eq!( count_a, 4 );

			} );

	}

	#[test]
	fn expect_byte() {

		let s = b"abaa";

		assert!( Parser::new(s)
			.expect_byte(b'a').unwrap()
			.expect_byte(b'a').is_err() );

	}

	#[test]
	fn advance_if() {

		let mut parser = Parser::new(b"ab");

		assert!(parser.advance_if(|&b| b == b'a').unwrap());
		assert!(!parser.advance_if(|&b| b == b'a').unwrap());
		assert!(parser.advance_if(|&b| b == b'b').unwrap());
		assert!(parser.advance_if(|&b| b == b'b').is_none());

	}

	#[test]
	fn next_if() {

		let mut parser = Parser::new(b"ab");

		assert_eq!(parser.next_if(|&b| b == b'a').unwrap(), b'a');
		assert!(parser.next_if(|&b| b == b'x').is_none());
		assert_eq!(parser.next_if(|&b| b == b'b').unwrap(), b'b');
		assert!(parser.next_if(|&b| b == b'x').is_none());

	}

	#[test]
	fn peek() {

		let s = b"abaa";

		assert_eq!( b'a', Parser::new(s).peek().unwrap() );
		assert_eq!( b'a', Parser::new(s).peek_at(1).unwrap() );
		assert_eq!( b'b', Parser::new(s).peek_at(2).unwrap() );
		assert_eq!( b'a', Parser::new(s).peek_at(3).unwrap() );
		assert!( Parser::new(s).peek_at(5).is_none() );

	}

	#[test]
	fn consume() {

		// normal
		let mut parser = Parser::new( b"aaa" );
		assert!( parser.consume().next().is_none() );

		// len
		let mut parser = Parser::new( b"aaa" );
		assert!( parser.consume_len( 1 ).unwrap().next().is_some() );

		let mut parser = Parser::new( b"aaa" );
		parser.consume();
		assert!(matches!( parser.consume_len(1), Err(0) ));

		// at least
		let mut parser = Parser::new( b"aaa" );
		assert!( parser.consume_at_least( 1 ).is_ok() );
		assert!( parser.next().is_none() );

	}

}