Skip to main content

oni_comb_parser/
str_input.rs

1use crate::input::Input;
2
3pub struct StrInput<'a> {
4  src: &'a str,
5  offset: usize,
6}
7
8impl<'a> StrInput<'a> {
9  pub fn new(src: &'a str) -> Self {
10    Self { src, offset: 0 }
11  }
12
13  pub(crate) fn advance(&mut self, n: usize) {
14    self.offset += n;
15  }
16
17  pub(crate) fn as_str(&self) -> &'a str {
18    &self.src[self.offset..]
19  }
20
21  /// 次のバイトを消費せずに覗く。EOF なら `None`。
22  #[inline]
23  pub fn peek_byte(&self) -> Option<u8> {
24    self.src.as_bytes().get(self.offset).copied()
25  }
26}
27
28impl<'a> Input for StrInput<'a> {
29  type Checkpoint = usize;
30  type Slice<'s>
31    = &'s str
32  where
33    Self: 's;
34
35  fn checkpoint(&self) -> Self::Checkpoint {
36    self.offset
37  }
38
39  fn reset(&mut self, checkpoint: Self::Checkpoint) {
40    self.offset = checkpoint;
41  }
42
43  fn offset(&self) -> usize {
44    self.offset
45  }
46
47  fn remaining(&self) -> &str {
48    &self.src[self.offset..]
49  }
50
51  fn is_eof(&self) -> bool {
52    self.offset >= self.src.len()
53  }
54}