Skip to content

Commit

Permalink
perf: reduce condition
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 30, 2024
1 parent cedea6e commit 43494d3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/with_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ where
None
};

if start_byte_index.is_some() && end_byte_index.is_some() {
return self.line.clone();
}

let (last_char_index, last_byte_index) =
self.last_char_index_to_byte_index.get();
let mut byte_index = last_byte_index as usize;
Expand All @@ -59,16 +63,17 @@ where
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;
}
if char_index == start_char_index {
start_byte_index = Some(byte_index);
if end_byte_index.is_some() {
break;
}
} else if char_index == end_char_index {
end_byte_index = Some(byte_index);
self
.last_char_index_to_byte_index
.set((char_index as u32, byte_index as u32));
break;
}
byte_index += char.len_utf8();
char_index += 1;
Expand All @@ -82,15 +87,16 @@ where
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;
}
byte_index -= char.len_utf8();
char_index -= 1;
if char_index == end_char_index {
end_byte_index = Some(byte_index);
if start_byte_index.is_some() {
break;
}
} else if char_index == start_char_index {
start_byte_index = Some(byte_index);
break;
}
}
}
Expand Down

0 comments on commit 43494d3

Please sign in to comment.