| 1 | use super::{Parse, ParseElem, ParseLiteral, ParseSlice, RuleResult}; |
| 2 | |
| 3 | impl<T> Parse for [T] { |
| 4 | type PositionRepr = usize; |
| 5 | #[inline] |
| 6 | fn start(&self) -> usize { |
| 7 | 0 |
| 8 | } |
| 9 | |
| 10 | #[inline] |
| 11 | fn is_eof(&self, pos: usize) -> bool { |
| 12 | pos >= self.len() |
| 13 | } |
| 14 | |
| 15 | #[inline] |
| 16 | fn position_repr(&self, pos: usize) -> usize { |
| 17 | pos |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl<'input, T: 'input + Copy> ParseElem<'input> for [T] { |
| 22 | type Element = T; |
| 23 | |
| 24 | #[inline] |
| 25 | fn parse_elem(&'input self, pos: usize) -> RuleResult<T> { |
| 26 | match self[pos..].first() { |
| 27 | Some(c) => RuleResult::Matched(pos + 1, *c), |
| 28 | None => RuleResult::Failed, |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl ParseLiteral for [u8] { |
| 34 | #[inline] |
| 35 | fn parse_string_literal(&self, pos: usize, literal: &str) -> RuleResult<()> { |
| 36 | let l = literal.len(); |
| 37 | if self.len() >= pos + l && &self[pos..pos + l] == literal.as_bytes() { |
| 38 | RuleResult::Matched(pos + l, ()) |
| 39 | } else { |
| 40 | RuleResult::Failed |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl<'input, T: 'input> ParseSlice<'input> for [T] { |
| 46 | type Slice = &'input [T]; |
| 47 | #[inline] |
| 48 | fn parse_slice(&'input self, p1: usize, p2: usize) -> &'input [T] { |
| 49 | &self[p1..p2] |
| 50 | } |
| 51 | } |