Skip to content

Commit

Permalink
fix: try perf
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 28, 2024
1 parent 11ea50a commit 7a841b6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/with_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ where
}

let line_len = self.line.len();

let mut start_byte_index =
if start_char_index == 0 { Some(0) } else { None };
let mut end_byte_index = if end_char_index == usize::MAX {
Expand All @@ -52,7 +51,14 @@ where
if start_char_index >= last_char_index as usize
|| end_char_index >= last_char_index as usize
{
for char in self.line.byte_slice(byte_index..line_len).chars() {
#[allow(unsafe_code)]
let slice = unsafe {
// SAFETY: Since `indices` iterates over the `CharIndices` of `self`, we can guarantee
// that the indices obtained from it will always be within the bounds of `self` and they
// will always lie on UTF-8 sequence boundaries.
self.line.byte_slice_unchecked(byte_index..line_len)
};
for char in slice.chars() {
if start_byte_index.is_some() && end_byte_index.is_some() {
break;
}
Expand All @@ -70,7 +76,14 @@ where
char_index += 1;
}
} else {
for char in self.line.byte_slice(0..byte_index).chars().rev() {
#[allow(unsafe_code)]
let slice = unsafe {
// SAFETY: Since `indices` iterates over the `CharIndices` of `self`, we can guarantee
// that the indices obtained from it will always be within the bounds of `self` and they
// will always lie on UTF-8 sequence boundaries.
self.line.byte_slice_unchecked(0..byte_index)
};
for char in slice.chars().rev() {
if start_byte_index.is_some() && end_byte_index.is_some() {
break;
}
Expand Down

0 comments on commit 7a841b6

Please sign in to comment.