| 1 |
use std::ops; |
| 2 |
|
| 3 |
use regex_cursor::{Cursor, RopeyCursor}; |
| 4 |
use ropey::RopeSlice; |
| 5 |
|
| 6 |
use crate::{Input, IntoInput}; |
| 7 |
|
| 8 |
pub struct RopeInput<'a> { |
| 9 |
src: RopeSlice<'a>, |
| 10 |
cursor: regex_cursor::RopeyCursor<'a>, |
| 11 |
} |
| 12 |
|
| 13 |
impl<'a> RopeInput<'a> { |
| 14 |
pub fn new(src: RopeSlice<'a>) -> Self { |
| 15 |
RopeInput { |
| 16 |
src, |
| 17 |
cursor: regex_cursor::RopeyCursor::new(src), |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
impl<'a> IntoInput for RopeSlice<'a> { |
| 23 |
type Input = RopeInput<'a>; |
| 24 |
|
| 25 |
fn into_input(self) -> Self::Input { |
| 26 |
RopeInput { |
| 27 |
src: self, |
| 28 |
cursor: RopeyCursor::new(self), |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl<'a> Input for RopeInput<'a> { |
| 34 |
type Cursor = RopeyCursor<'a>; |
| 35 |
fn cursor_at(&mut self, offset: u32) -> &mut RopeyCursor<'a> { |
| 36 |
let offset = offset as usize; |
| 37 |
debug_assert!( |
| 38 |
offset <= self.src.len_bytes(), |
| 39 |
"parser offset out of bounds: {offset} > {}", |
| 40 |
self.src.len_bytes() |
| 41 |
); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
if offset < self.cursor.offset() || offset - self.cursor.offset() > 4096 { |
| 46 |
self.cursor = regex_cursor::RopeyCursor::at(self.src, offset); |
| 47 |
} else { |
| 48 |
while self.cursor.offset() + self.cursor.chunk().len() <= offset { |
| 49 |
if !self.cursor.advance() { |
| 50 |
break; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
&mut self.cursor |
| 55 |
} |
| 56 |
|
| 57 |
fn eq(&mut self, range1: ops::Range<u32>, range2: ops::Range<u32>) -> bool { |
| 58 |
let range1 = self |
| 59 |
.src |
| 60 |
.byte_slice(range1.start as usize..range1.end as usize); |
| 61 |
let range2 = self |
| 62 |
.src |
| 63 |
.byte_slice(range2.start as usize..range2.end as usize); |
| 64 |
range1 == range2 |
| 65 |
} |
| 66 |
} |