diff options
| author | Botahamec <botahamec@outlook.com> | 2023-07-29 21:42:15 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2023-07-29 21:42:15 -0400 |
| commit | 5edaa60802378609d45b0c4c1d6b35f9f78db290 (patch) | |
| tree | cf2e94f68f655968aec34be5f292164477ac0473 /src/scanner.rs | |
| parent | 496f8b6261ca6815383794f5da5ce805dcda0f84 (diff) | |
Add some string analysis methods
Diffstat (limited to 'src/scanner.rs')
| -rw-r--r-- | src/scanner.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/scanner.rs b/src/scanner.rs index e007f2f..ee7d50b 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -34,4 +34,24 @@ impl Scanner { pub fn advance(&mut self, amount: usize) -> Option<String> { self.goto(self.position + amount) } + + pub fn find_substring(&self, substring: impl AsRef<str>) -> Option<usize> { + self.source + .get(self.position..)? + .iter() + .collect::<String>() + .find(substring.as_ref()) + } + + pub fn starts_with(&self, substring: impl AsRef<str>) -> Option<usize> { + let mut i = self.position; + for substring_char in substring.as_ref().chars() { + if *self.source.get(i)? != substring_char { + return None; + } + i += 1; + } + + Some(i) + } } |
