Skip to content

Commit

Permalink
Apply PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
CeleritasCelery committed Oct 31, 2024
1 parent b31888a commit 1dbb919
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/byte_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ impl ByteChunk for usize {

#[inline(always)]
fn shift_across(&self, n: Self) -> Self {
let size = (Self::SIZE - 1) * 8;
let shift_distance = (Self::SIZE - 1) * 8;
if cfg!(target_endian = "little") {
(*self >> size) | (n << 8)
(*self >> shift_distance) | (n << 8)
} else {
(*self << size) | (n >> 8)
(*self << shift_distance) | (n >> 8)
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/lines_crlf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn to_byte_idx_impl<T: ByteChunk>(text: &[u8], line_idx: usize) -> usize {
let mut byte_count = 0;
let mut break_count = 0;

// Take care of any unaligned bytes at the beginning.
let mut last_was_cr = false;
for byte in start.iter().copied() {
let is_lf = byte == LF;
Expand All @@ -86,7 +87,7 @@ fn to_byte_idx_impl<T: ByteChunk>(text: &[u8], line_idx: usize) -> usize {
byte_count += 1;
}

// Process the chunks 2 at a time
// Process the chunks 2 at a time.
let mut chunk_count = 0;
let mut prev = T::splat(last_was_cr as u8);
for chunks in middle.chunks_exact(2) {
Expand Down Expand Up @@ -114,7 +115,7 @@ fn to_byte_idx_impl<T: ByteChunk>(text: &[u8], line_idx: usize) -> usize {
prev = cr_flags1;
}

// Process the rest of the chunks
// Process the rest of the chunks.
for chunk in middle[chunk_count..].iter() {
let lf_flags = chunk.cmp_eq_byte(LF);
let cr_flags = chunk.cmp_eq_byte(CR);
Expand All @@ -128,6 +129,7 @@ fn to_byte_idx_impl<T: ByteChunk>(text: &[u8], line_idx: usize) -> usize {
prev = cr_flags;
}

// Take care of any unaligned bytes at the end.
last_was_cr = text.get(byte_count.saturating_sub(1)) == Some(&CR);
for byte in text[byte_count..].iter().copied() {
let is_lf = byte == LF;
Expand Down Expand Up @@ -166,12 +168,11 @@ fn count_breaks_impl<T: ByteChunk>(text: &[u8]) -> usize {
for byte in start.iter().copied() {
let is_lf = byte == LF;
let is_cr = byte == CR;
if is_cr || (is_lf && !last_was_cr) {
count += 1;
}
count += (is_cr | (is_lf & !last_was_cr)) as usize;
last_was_cr = is_cr;
}

// Take care of the middle bytes in big chunks.
let mut prev = T::splat(last_was_cr as u8);
for chunks in middle.chunks_exact(2) {
let lf_flags0 = chunks[0].cmp_eq_byte(LF);
Expand Down Expand Up @@ -203,9 +204,7 @@ fn count_breaks_impl<T: ByteChunk>(text: &[u8]) -> usize {
for byte in end.iter().copied() {
let is_lf = byte == LF;
let is_cr = byte == CR;
if is_cr || (is_lf && !last_was_cr) {
count += 1;
}
count += (is_cr | (is_lf & !last_was_cr)) as usize;
last_was_cr = is_cr;
}

Expand Down

0 comments on commit 1dbb919

Please sign in to comment.