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 4847738 commit 0f0a248
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/with_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ where
{
/// line is a string reference
pub line: S,
last_char_index_to_byte_index: RefCell<(u32, u32)>,
char_index_to_byte_index: RefCell<[(u32, u32); 2]>,
data: PhantomData<&'a S>,
}

Expand All @@ -20,7 +20,7 @@ where
pub fn new(line: S) -> Self {
Self {
line,
last_char_index_to_byte_index: RefCell::new((0, 0)),
char_index_to_byte_index: RefCell::new([(0, 0), (0, 0)]),
data: PhantomData,
}
}
Expand All @@ -39,40 +39,54 @@ where
let mut start_byte_index = None;
let mut end_byte_index = None;

let (last_char_index, last_byte_index) =
*self.last_char_index_to_byte_index.borrow();
let mut last_byte_index = last_byte_index as usize;
let mut char_index = last_char_index as usize;
if start_char_index < last_char_index as usize {
char_index = 0;
last_byte_index = 0;
let mut current_char_index = 0;
let mut current_byte_index = 0;
for (last_char_index, last_byte_index) in
*self.char_index_to_byte_index.borrow()
{
if start_char_index >= last_char_index as usize {
current_char_index = last_char_index as usize;
current_byte_index = last_byte_index as usize;
}
}

for (byte_index, _) in self
.line
.byte_slice(last_byte_index..line_len)
.byte_slice(current_byte_index..line_len)
.char_indices()
{
if char_index == start_char_index {
start_byte_index = Some(byte_index + last_byte_index);
if current_char_index == start_char_index {
start_byte_index = Some(byte_index + current_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() =
(end_char_index as u32, (byte_index + last_byte_index) as u32);
if current_char_index == end_char_index {
end_byte_index = Some(byte_index + current_byte_index);
break;
}
char_index += 1;
current_char_index += 1;
}

let mut char_index_to_byte_index =
*self.char_index_to_byte_index.borrow_mut();
let start_byte_index = if let Some(start_byte_index) = start_byte_index {
char_index_to_byte_index[0] =
(start_char_index as u32, start_byte_index as u32);
start_byte_index
} else {
char_index_to_byte_index[0] = (0, 0);
return S::default();
};
let end_byte_index = end_byte_index.unwrap_or(line_len);

let end_byte_index = if let Some(end_byte_index) = end_byte_index {
char_index_to_byte_index[1] =
(end_char_index as u32, end_byte_index as u32);
end_byte_index
} else {
char_index_to_byte_index[0] = (0, 0);
line_len
};

#[allow(unsafe_code)]
unsafe {
Expand Down

0 comments on commit 0f0a248

Please sign in to comment.