diff options
| author | Botahamec <botahamec@outlook.com> | 2023-07-29 21:52:59 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2023-07-29 21:52:59 -0400 |
| commit | c0ad3ae92d6d26410e160113fe0bbf3ab87f7c1f (patch) | |
| tree | 3e461fcff5b71e0513958532c50e38be435d9939 /src/scanner.rs | |
| parent | c6c033c1eff1cbcb97a6d94b9002436d97effdd6 (diff) | |
Add cset scanning functions
Diffstat (limited to 'src/scanner.rs')
| -rw-r--r-- | src/scanner.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/scanner.rs b/src/scanner.rs index 6b31717..4e5ca21 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -1,3 +1,5 @@ +use crate::csets::CharacterSet; + pub struct Scanner { source: Box<[char]>, position: usize, @@ -59,4 +61,31 @@ impl Scanner { let position = self.starts_with(substring)?; self.goto(position) } + + pub fn any(&self, cset: impl CharacterSet) -> Option<usize> { + cset.contains(*self.source.get(self.position)?) + .then_some(self.position + 1) + } + + pub fn many(&self, cset: impl CharacterSet) -> Option<usize> { + if !cset.contains(*self.source.get(self.position)?) { + return None; + } + + let mut i = self.position; + while i < self.source.len() && cset.contains(self.source[i]) { + i += 1; + } + + Some(i) + } + + pub fn upto(&self, cset: impl CharacterSet) -> Option<usize> { + let mut i = self.position; + while !cset.contains(*self.source.get(i)?) { + i += 1; + } + + Some(i) + } } |
