From 1dbb9191d02bd8ee02f4a0778d27773dfd520b0e Mon Sep 17 00:00:00 2001 From: Troy Hinckley Date: Thu, 31 Oct 2024 08:27:35 -0500 Subject: [PATCH] Apply PR feedback --- src/byte_chunk.rs | 6 +++--- src/lines_crlf.rs | 15 +++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/byte_chunk.rs b/src/byte_chunk.rs index 3ff4e96..37d945a 100644 --- a/src/byte_chunk.rs +++ b/src/byte_chunk.rs @@ -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) } } diff --git a/src/lines_crlf.rs b/src/lines_crlf.rs index 158ad39..76755c5 100644 --- a/src/lines_crlf.rs +++ b/src/lines_crlf.rs @@ -69,6 +69,7 @@ fn to_byte_idx_impl(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; @@ -86,7 +87,7 @@ fn to_byte_idx_impl(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) { @@ -114,7 +115,7 @@ fn to_byte_idx_impl(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); @@ -128,6 +129,7 @@ fn to_byte_idx_impl(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; @@ -166,12 +168,11 @@ fn count_breaks_impl(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); @@ -203,9 +204,7 @@ fn count_breaks_impl(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; }