Skip to content

Commit 2ab3040

Browse files
committed
refactor find byte method
1 parent 74a086a commit 2ab3040

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/iter_city.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a> Iterator for ToCityTemperatures<'a> {
3232
let chunk = &self.chunk[self.position..];
3333

3434
// Since city names have at least 3 characters, we can skip the first 3 bytes.
35-
let city_len = 3 + chunk[3..].find_byte_index(b';');
35+
let city_len = 3 + chunk[3..].find_byte(b';').unwrap();
3636
let city = &chunk[..city_len];
3737

3838
// We don't need to check for the length of the temperature by finding the next line break.

src/util/find.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use std::simd::{cmp::SimdPartialEq, u8x32};
22

3+
type Result<T> = std::result::Result<T, ()>;
4+
35
pub trait FindByte {
4-
fn find_byte_index(&self, byte: u8) -> usize;
6+
fn find_byte(&self, byte: u8) -> Result<usize>;
57
}
68

79
impl FindByte for [u8] {
810
#[inline(always)]
9-
fn find_byte_index(&self, byte: u8) -> usize {
11+
fn find_byte(&self, byte: u8) -> Result<usize> {
1012
let simd_width = 32; // 256-bit wide SIMD register
1113

1214
for (i, chunk) in self.chunks_exact(simd_width).enumerate() {
@@ -24,7 +26,7 @@ impl FindByte for [u8] {
2426
let mask = masks[j];
2527
if mask != 0 {
2628
// Calculate the position of the match
27-
return i * simd_width + j * 8 + mask.trailing_zeros() as usize;
29+
return Ok(i * simd_width + j * 8 + mask.trailing_zeros() as usize);
2830
}
2931
}
3032
}
@@ -34,6 +36,6 @@ impl FindByte for [u8] {
3436
.remainder()
3537
.iter()
3638
.position(|&c| c == byte)
37-
.unwrap()
39+
.ok_or(())
3840
}
3941
}

0 commit comments

Comments
 (0)