Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 22, 2024
1 parent d923194 commit f45bedf
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/with_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ where
{
/// line is a string reference
pub line: S,
last_char_index_to_byte_index: RefCell<(usize, usize)>,
last_start_char_index_to_start_byte_index: RefCell<(usize, usize)>,
last_end_char_index_to_end_byte_index: RefCell<(usize, usize)>,
data: PhantomData<&'a S>,
}

Expand All @@ -20,7 +21,8 @@ where
pub fn new(line: S) -> Self {
Self {
line,
last_char_index_to_byte_index: RefCell::new((0, 0)),
last_start_char_index_to_start_byte_index: RefCell::new((0, 0)),
last_end_char_index_to_end_byte_index: RefCell::new((0, 0)),
data: PhantomData,
}
}
Expand All @@ -39,27 +41,36 @@ where
let mut start_byte_index = None;
let mut end_byte_index = None;

let (last_char_index, mut last_byte_index) =
*self.last_char_index_to_byte_index.borrow();
let mut char_index = last_char_index;
if start_char_index < last_char_index {
char_index = 0;
last_byte_index = 0;
}
let (last_start_char_index, last_start_byte_index) =
*self.last_start_char_index_to_start_byte_index.borrow();
let (last_end_char_index, last_end_byte_index) =
*self.last_end_char_index_to_end_byte_index.borrow();

let (mut char_index, last_byte_index) =
if start_char_index >= last_end_char_index {
(last_end_char_index, last_end_byte_index)
} else if start_char_index >= last_start_char_index {
(last_start_char_index, last_start_byte_index)
} else {
(0, 0)
};

for (byte_index, _) in self
.line
.byte_slice(last_byte_index..line_len)
.char_indices()
{
if char_index == start_char_index {
start_byte_index = Some(byte_index + last_byte_index);
*self.last_start_char_index_to_start_byte_index.borrow_mut() =
(start_char_index, byte_index + last_byte_index);
if end_char_index == usize::MAX {
break;
}
}
if char_index == end_char_index {
end_byte_index = Some(byte_index + last_byte_index);
*self.last_char_index_to_byte_index.borrow_mut() =
*self.last_end_char_index_to_end_byte_index.borrow_mut() =
(end_char_index, byte_index + last_byte_index);
break;
}
Expand Down

0 comments on commit f45bedf

Please sign in to comment.