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
use { crate::*, std::{ str::CharIndices, }, }; pub struct Ranger<'s> { s: &'s str, char_indices: CharIndices<'s>, pos: usize, last: Option<char>, } impl<'s> Ranger<'s> { pub fn new(s: &'s str) -> Self { Self { s, char_indices: s.char_indices(), pos: 0, last: None, } } pub fn until(&mut self, end: char) -> Result<&'s str, LogParseError> { for (idx, c) in &mut self.char_indices { if c == end { let start = self.pos; self.pos = idx; self.last = Some(c); return Ok(&self.s[start..self.pos]); } } Err(LogParseError::CharNotFound(end)) } pub fn between(&mut self, start: char, end: char) -> Result<&'s str, LogParseError> { if Some(start) == self.last { self.pos += start.len_utf8(); return self.until(end); } for (idx, c) in &mut self.char_indices { if c == start { let pos = idx + start.len_utf8(); for (idx, c) in &mut self.char_indices { if c == end { self.pos = idx; self.last = Some(c); return Ok(&self.s[pos..self.pos]); } } return Err(LogParseError::CharNotFound(end)); } } Err(LogParseError::CharNotFound(start)) } }