From 9d2b52fe99fe2ecad339580134637d22c94030a0 Mon Sep 17 00:00:00 2001 From: Alisa Sireneva Date: Mon, 29 Jul 2024 21:20:27 +0300 Subject: [PATCH] Fix clippy warnings, add clippy to CI --- .github/workflows/ci.yml | 32 +++++++---- src/arch/aarch64/neon/memchr.rs | 92 +++++++++++++++--------------- src/arch/all/memchr.rs | 94 +++++++++++++++---------------- src/arch/all/mod.rs | 18 +++--- src/arch/all/packedpair/mod.rs | 2 +- src/arch/all/rabinkarp.rs | 18 +++--- src/arch/all/twoway.rs | 8 +-- src/arch/generic/memchr.rs | 94 +++++++++++++++---------------- src/arch/wasm32/simd128/memchr.rs | 92 +++++++++++++++--------------- src/arch/x86_64/avx2/memchr.rs | 92 +++++++++++++++--------------- src/arch/x86_64/sse2/memchr.rs | 92 +++++++++++++++--------------- src/ext.rs | 2 +- src/lib.rs | 54 +++++++++--------- src/memchr.rs | 78 +++++++++++-------------- src/memmem/searcher.rs | 8 +-- src/tests/memchr/mod.rs | 8 +-- src/tests/memchr/prop.rs | 41 ++++++-------- src/tests/packedpair.rs | 1 + src/tests/substring/mod.rs | 3 +- src/tests/substring/naive.rs | 14 +---- src/tests/substring/prop.rs | 24 +++----- src/vector.rs | 12 ++++ 22 files changed, 432 insertions(+), 447 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c1565b..6c33a32 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -94,6 +94,7 @@ jobs: uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} + components: clippy - name: Use Cross if: matrix.os == 'ubuntu-latest' && matrix.target != '' run: | @@ -117,6 +118,8 @@ jobs: run: lscpu - name: Basic build run: ${{ env.CARGO }} build --verbose $TARGET + - name: Run clippy + run: ${{ env.CARGO }} clippy --verbose $TARGET - name: Build docs run: ${{ env.CARGO }} doc --verbose $TARGET - name: Show byte order for debugging @@ -130,7 +133,7 @@ jobs: - name: Run tests with miscellaneous features run: ${{ env.CARGO }} test --verbose --features logging - # Setup and run tests on the wasm32-wasi target via wasmtime. + # Setup and run tests on the wasm32-wasip1 target via wasmtime. wasm: runs-on: ubuntu-latest env: @@ -143,11 +146,12 @@ jobs: uses: dtolnay/rust-toolchain@master with: toolchain: stable - - name: Add wasm32-wasi target - run: rustup target add wasm32-wasi + components: clippy + - name: Add wasm32-wasip1 target + run: rustup target add wasm32-wasip1 - name: Download and install Wasmtime run: | - echo "CARGO_BUILD_TARGET=wasm32-wasi" >> $GITHUB_ENV + echo "CARGO_BUILD_TARGET=wasm32-wasip1" >> $GITHUB_ENV echo "RUSTFLAGS=-Ctarget-feature=+simd128" >> $GITHUB_ENV curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v$WASMTIME_VERSION/wasmtime-v$WASMTIME_VERSION-x86_64-linux.tar.xz tar xvf wasmtime-v$WASMTIME_VERSION-x86_64-linux.tar.xz @@ -155,6 +159,8 @@ jobs: echo "CARGO_TARGET_WASM32_WASI_RUNNER=wasmtime run --wasm simd --" >> $GITHUB_ENV - name: Basic build run: cargo build --verbose + - name: Run clippy + run: cargo clippy --verbose --all-targets -- -Dwarnings - name: Run tests run: cargo test --verbose - name: Run with only 'alloc' enabled @@ -162,7 +168,7 @@ jobs: - name: Run tests without any features enabled (core-only) run: cargo test --verbose --no-default-features - # Setup and run tests on the wasm32-wasi target via wasmtime, but without + # Setup and run tests on the wasm32-wasip1 target via wasmtime, but without # simd128 enabled. wasm-no-simd128: runs-on: ubuntu-latest @@ -176,11 +182,12 @@ jobs: uses: dtolnay/rust-toolchain@master with: toolchain: stable - - name: Add wasm32-wasi target - run: rustup target add wasm32-wasi + components: clippy + - name: Add wasm32-wasip1 target + run: rustup target add wasm32-wasip1 - name: Download and install Wasmtime run: | - echo "CARGO_BUILD_TARGET=wasm32-wasi" >> $GITHUB_ENV + echo "CARGO_BUILD_TARGET=wasm32-wasip1" >> $GITHUB_ENV echo "RUSTFLAGS=-Ctarget-feature=-simd128" >> $GITHUB_ENV curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v$WASMTIME_VERSION/wasmtime-v$WASMTIME_VERSION-x86_64-linux.tar.xz tar xvf wasmtime-v$WASMTIME_VERSION-x86_64-linux.tar.xz @@ -188,6 +195,8 @@ jobs: echo "CARGO_TARGET_WASM32_WASI_RUNNER=wasmtime run --" >> $GITHUB_ENV - name: Basic build run: cargo build --verbose + - name: Run clippy + run: cargo clippy --verbose --all-targets -- -Dwarnings - name: Run tests run: cargo test --verbose - name: Run with only 'alloc' enabled @@ -207,8 +216,11 @@ jobs: with: toolchain: stable targets: x86_64-unknown-none + components: clippy - name: Build for x86_64-unknown-none with only 'alloc' enabled run: cargo build --verbose --no-default-features --features alloc --target x86_64-unknown-none + - name: Run clippy for x86_64-unknown-linux-gnu without SSE + run: cargo clippy --verbose --all-targets -- -Dwarnings - name: Run tests for x86_64-unknown-linux-gnu without SSE run: cargo test --verbose env: @@ -270,8 +282,8 @@ jobs: uses: dtolnay/rust-toolchain@master with: toolchain: stable - - name: Add wasm32-wasi target - run: rustup target add wasm32-wasi + - name: Add wasm32-wasip1 target + run: rustup target add wasm32-wasip1 - name: Download and install Wasmtime run: | # Note that we don't have to set CARGO_BUILD_TARGET and other diff --git a/src/arch/aarch64/neon/memchr.rs b/src/arch/aarch64/neon/memchr.rs index 5fcc762..854e0c9 100644 --- a/src/arch/aarch64/neon/memchr.rs +++ b/src/arch/aarch64/neon/memchr.rs @@ -142,13 +142,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -186,13 +186,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -227,13 +227,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -490,13 +490,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -534,13 +534,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -776,13 +776,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -822,13 +822,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -986,10 +986,9 @@ mod tests { #[test] fn forward_two() { crate::tests::memchr::Runner::new(2).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2] => Some(Two::new(n1, n2)?.iter(haystack).collect()), + _ => None, }, ) } @@ -997,10 +996,11 @@ mod tests { #[test] fn reverse_two() { crate::tests::memchr::Runner::new(2).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2] => { + Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + } + _ => None, }, ) } @@ -1008,11 +1008,11 @@ mod tests { #[test] fn forward_three() { crate::tests::memchr::Runner::new(3).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + } + _ => None, }, ) } @@ -1020,11 +1020,11 @@ mod tests { #[test] fn reverse_three() { crate::tests::memchr::Runner::new(3).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => Some( + Three::new(n1, n2, n3)?.iter(haystack).rev().collect(), + ), + _ => None, }, ) } diff --git a/src/arch/all/memchr.rs b/src/arch/all/memchr.rs index 62fe2a3..b2c2fb3 100644 --- a/src/arch/all/memchr.rs +++ b/src/arch/all/memchr.rs @@ -113,13 +113,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -183,13 +183,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -222,7 +222,7 @@ impl One { debug_assert_eq!(0, cur.as_usize() % USIZE_BYTES); let a = cur.sub(2 * USIZE_BYTES).cast::().read(); - let b = cur.sub(1 * USIZE_BYTES).cast::().read(); + let b = cur.sub(USIZE_BYTES).cast::().read(); if self.has_needle(a) || self.has_needle(b) { break; } @@ -242,13 +242,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `0` will always be returned. @@ -423,13 +423,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -489,13 +489,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -687,13 +687,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -753,13 +753,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -928,10 +928,9 @@ mod tests { #[test] fn forward_two() { crate::tests::memchr::Runner::new(2).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2).iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2] => Some(Two::new(n1, n2).iter(haystack).collect()), + _ => None, }, ) } @@ -939,10 +938,11 @@ mod tests { #[test] fn reverse_two() { crate::tests::memchr::Runner::new(2).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2).iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2] => { + Some(Two::new(n1, n2).iter(haystack).rev().collect()) + } + _ => None, }, ) } @@ -950,11 +950,11 @@ mod tests { #[test] fn forward_three() { crate::tests::memchr::Runner::new(3).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3).iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(Three::new(n1, n2, n3).iter(haystack).collect()) + } + _ => None, }, ) } @@ -962,11 +962,11 @@ mod tests { #[test] fn reverse_three() { crate::tests::memchr::Runner::new(3).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3).iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(Three::new(n1, n2, n3).iter(haystack).rev().collect()) + } + _ => None, }, ) } diff --git a/src/arch/all/mod.rs b/src/arch/all/mod.rs index 559cb75..a1fd0f3 100644 --- a/src/arch/all/mod.rs +++ b/src/arch/all/mod.rs @@ -95,15 +95,15 @@ pub fn is_equal(x: &[u8], y: &[u8]) -> bool { /// * Both `x` and `y` must be valid for reads of up to `n` bytes. /// * Both `x` and `y` must point to an initialized value. /// * Both `x` and `y` must each point to an allocated object and -/// must either be in bounds or at most one byte past the end of the -/// allocated object. `x` and `y` do not need to point to the same allocated -/// object, but they may. +/// must either be in bounds or at most one byte past the end of the +/// allocated object. `x` and `y` do not need to point to the same allocated +/// object, but they may. /// * Both `x` and `y` must be _derived from_ a pointer to their respective -/// allocated objects. +/// allocated objects. /// * The distance between `x` and `x+n` must not overflow `isize`. Similarly -/// for `y` and `y+n`. +/// for `y` and `y+n`. /// * The distance being in bounds must not rely on "wrapping around" the -/// address space. +/// address space. #[inline(always)] pub unsafe fn is_equal_raw( mut x: *const u8, @@ -149,10 +149,8 @@ pub unsafe fn is_equal_raw( y = y.add(2); n -= 2; } - if n > 0 { - if x.read() != y.read() { - return false; - } + if n > 0 && x.read() != y.read() { + return false; } true } diff --git a/src/arch/all/packedpair/mod.rs b/src/arch/all/packedpair/mod.rs index 148a985..3b40d1e 100644 --- a/src/arch/all/packedpair/mod.rs +++ b/src/arch/all/packedpair/mod.rs @@ -176,7 +176,7 @@ impl Pair { core::mem::swap(&mut rare1, &mut rare2); core::mem::swap(&mut index1, &mut index2); } - let max = usize::from(core::u8::MAX); + let max = usize::from(u8::MAX); for (i, &b) in needle.iter().enumerate().take(max).skip(2) { if ranker.rank(b) < ranker.rank(rare1) { rare2 = rare1; diff --git a/src/arch/all/rabinkarp.rs b/src/arch/all/rabinkarp.rs index e0bafba..881fa21 100644 --- a/src/arch/all/rabinkarp.rs +++ b/src/arch/all/rabinkarp.rs @@ -86,7 +86,7 @@ impl Finder { #[inline] pub fn new(needle: &[u8]) -> Finder { let mut s = Finder { hash: Hash::new(), hash_2pow: 1 }; - let first_byte = match needle.get(0) { + let first_byte = match needle.first() { None => return s, Some(&first_byte) => first_byte, }; @@ -138,13 +138,13 @@ impl Finder { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// * It must be the case that `start <= end`. #[inline] pub unsafe fn find_raw( @@ -236,13 +236,13 @@ impl FinderRev { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// * It must be the case that `start <= end`. #[inline] pub unsafe fn rfind_raw( diff --git a/src/arch/all/twoway.rs b/src/arch/all/twoway.rs index 0df3b4a..ae77837 100644 --- a/src/arch/all/twoway.rs +++ b/src/arch/all/twoway.rs @@ -69,10 +69,10 @@ pub struct FinderRev(TwoWay); /// /// 1. Do something to detect a "critical" position in the needle. /// 2. For the current position in the haystack, look if `needle[critical..]` -/// matches at that position. +/// matches at that position. /// 3. If so, look if `needle[..critical]` matches. /// 4. If a mismatch occurs, shift the search by some amount based on the -/// critical position and a pre-computed shift. +/// critical position and a pre-computed shift. /// /// This type is wrapped in the forward and reverse finders that expose /// consistent forward or reverse APIs. @@ -323,7 +323,7 @@ impl FinderRev { let nlen = needle.len(); let mut pos = haystack.len(); let mut shift = nlen; - let first_byte = match needle.get(0) { + let first_byte = match needle.first() { None => return Some(pos), Some(&first_byte) => first_byte, }; @@ -364,7 +364,7 @@ impl FinderRev { ) -> Option { let nlen = needle.len(); let mut pos = haystack.len(); - let first_byte = match needle.get(0) { + let first_byte = match needle.first() { None => return Some(pos), Some(&first_byte) => first_byte, }; diff --git a/src/arch/generic/memchr.rs b/src/arch/generic/memchr.rs index 580b3cc..f4484ae 100644 --- a/src/arch/generic/memchr.rs +++ b/src/arch/generic/memchr.rs @@ -127,18 +127,18 @@ impl One { /// # Safety /// /// * It must be the case that `start < end` and that the distance between - /// them is at least equal to `V::BYTES`. That is, it must always be valid - /// to do at least an unaligned load of `V` at `start`. + /// them is at least equal to `V::BYTES`. That is, it must always be valid + /// to do at least an unaligned load of `V` at `start`. /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. #[inline(always)] pub(crate) unsafe fn find_raw( &self, @@ -173,7 +173,7 @@ impl One { debug_assert_eq!(0, cur.as_usize() % V::BYTES); let a = V::load_aligned(cur); - let b = V::load_aligned(cur.add(1 * V::BYTES)); + let b = V::load_aligned(cur.add(V::BYTES)); let c = V::load_aligned(cur.add(2 * V::BYTES)); let d = V::load_aligned(cur.add(3 * V::BYTES)); let eqa = self.v1.cmpeq(a); @@ -191,7 +191,7 @@ impl One { let mask = eqb.movemask(); if mask.has_non_zero() { - return Some(cur.add(1 * V::BYTES).add(topos(mask))); + return Some(cur.add(V::BYTES).add(topos(mask))); } let mask = eqc.movemask(); @@ -238,18 +238,18 @@ impl One { /// # Safety /// /// * It must be the case that `start < end` and that the distance between - /// them is at least equal to `V::BYTES`. That is, it must always be valid - /// to do at least an unaligned load of `V` at `start`. + /// them is at least equal to `V::BYTES`. That is, it must always be valid + /// to do at least an unaligned load of `V` at `start`. /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. #[inline(always)] pub(crate) unsafe fn rfind_raw( &self, @@ -282,7 +282,7 @@ impl One { cur = cur.sub(Self::LOOP_SIZE); let a = V::load_aligned(cur); - let b = V::load_aligned(cur.add(1 * V::BYTES)); + let b = V::load_aligned(cur.add(V::BYTES)); let c = V::load_aligned(cur.add(2 * V::BYTES)); let d = V::load_aligned(cur.add(3 * V::BYTES)); let eqa = self.v1.cmpeq(a); @@ -305,7 +305,7 @@ impl One { let mask = eqb.movemask(); if mask.has_non_zero() { - return Some(cur.add(1 * V::BYTES).add(topos(mask))); + return Some(cur.add(V::BYTES).add(topos(mask))); } let mask = eqa.movemask(); @@ -333,18 +333,18 @@ impl One { /// # Safety /// /// * It must be the case that `start < end` and that the distance between - /// them is at least equal to `V::BYTES`. That is, it must always be valid - /// to do at least an unaligned load of `V` at `start`. + /// them is at least equal to `V::BYTES`. That is, it must always be valid + /// to do at least an unaligned load of `V` at `start`. /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. #[inline(always)] pub(crate) unsafe fn count_raw( &self, @@ -372,7 +372,7 @@ impl One { debug_assert_eq!(0, cur.as_usize() % V::BYTES); let a = V::load_aligned(cur); - let b = V::load_aligned(cur.add(1 * V::BYTES)); + let b = V::load_aligned(cur.add(V::BYTES)); let c = V::load_aligned(cur.add(2 * V::BYTES)); let d = V::load_aligned(cur.add(3 * V::BYTES)); let eqa = self.v1.cmpeq(a); @@ -477,18 +477,18 @@ impl Two { /// # Safety /// /// * It must be the case that `start < end` and that the distance between - /// them is at least equal to `V::BYTES`. That is, it must always be valid - /// to do at least an unaligned load of `V` at `start`. + /// them is at least equal to `V::BYTES`. That is, it must always be valid + /// to do at least an unaligned load of `V` at `start`. /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. #[inline(always)] pub(crate) unsafe fn find_raw( &self, @@ -576,18 +576,18 @@ impl Two { /// # Safety /// /// * It must be the case that `start < end` and that the distance between - /// them is at least equal to `V::BYTES`. That is, it must always be valid - /// to do at least an unaligned load of `V` at `start`. + /// them is at least equal to `V::BYTES`. That is, it must always be valid + /// to do at least an unaligned load of `V` at `start`. /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. #[inline(always)] pub(crate) unsafe fn rfind_raw( &self, @@ -749,18 +749,18 @@ impl Three { /// # Safety /// /// * It must be the case that `start < end` and that the distance between - /// them is at least equal to `V::BYTES`. That is, it must always be valid - /// to do at least an unaligned load of `V` at `start`. + /// them is at least equal to `V::BYTES`. That is, it must always be valid + /// to do at least an unaligned load of `V` at `start`. /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. #[inline(always)] pub(crate) unsafe fn find_raw( &self, @@ -858,18 +858,18 @@ impl Three { /// # Safety /// /// * It must be the case that `start < end` and that the distance between - /// them is at least equal to `V::BYTES`. That is, it must always be valid - /// to do at least an unaligned load of `V` at `start`. + /// them is at least equal to `V::BYTES`. That is, it must always be valid + /// to do at least an unaligned load of `V` at `start`. /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. #[inline(always)] pub(crate) unsafe fn rfind_raw( &self, diff --git a/src/arch/wasm32/simd128/memchr.rs b/src/arch/wasm32/simd128/memchr.rs index fa314c9..1048fc8 100644 --- a/src/arch/wasm32/simd128/memchr.rs +++ b/src/arch/wasm32/simd128/memchr.rs @@ -137,13 +137,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -181,13 +181,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -226,13 +226,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -484,13 +484,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -528,13 +528,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -765,13 +765,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -811,13 +811,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -975,10 +975,9 @@ mod tests { #[test] fn forward_two() { crate::tests::memchr::Runner::new(2).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2] => Some(Two::new(n1, n2)?.iter(haystack).collect()), + _ => None, }, ) } @@ -986,10 +985,11 @@ mod tests { #[test] fn reverse_two() { crate::tests::memchr::Runner::new(2).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2] => { + Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + } + _ => None, }, ) } @@ -997,11 +997,11 @@ mod tests { #[test] fn forward_three() { crate::tests::memchr::Runner::new(3).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + } + _ => None, }, ) } @@ -1009,11 +1009,11 @@ mod tests { #[test] fn reverse_three() { crate::tests::memchr::Runner::new(3).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => Some( + Three::new(n1, n2, n3)?.iter(haystack).rev().collect(), + ), + _ => None, }, ) } diff --git a/src/arch/x86_64/avx2/memchr.rs b/src/arch/x86_64/avx2/memchr.rs index 59f8c7f..c88d2c4 100644 --- a/src/arch/x86_64/avx2/memchr.rs +++ b/src/arch/x86_64/avx2/memchr.rs @@ -165,13 +165,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -231,13 +231,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -285,13 +285,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `0` will always be returned. @@ -640,13 +640,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -706,13 +706,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -1023,13 +1023,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -1091,13 +1091,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -1307,10 +1307,9 @@ mod tests { #[test] fn forward_two() { crate::tests::memchr::Runner::new(2).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2] => Some(Two::new(n1, n2)?.iter(haystack).collect()), + _ => None, }, ) } @@ -1318,10 +1317,11 @@ mod tests { #[test] fn reverse_two() { crate::tests::memchr::Runner::new(2).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2] => { + Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + } + _ => None, }, ) } @@ -1329,11 +1329,11 @@ mod tests { #[test] fn forward_three() { crate::tests::memchr::Runner::new(3).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + } + _ => None, }, ) } @@ -1341,11 +1341,11 @@ mod tests { #[test] fn reverse_three() { crate::tests::memchr::Runner::new(3).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => Some( + Three::new(n1, n2, n3)?.iter(haystack).rev().collect(), + ), + _ => None, }, ) } diff --git a/src/arch/x86_64/sse2/memchr.rs b/src/arch/x86_64/sse2/memchr.rs index c6f75df..4c1d052 100644 --- a/src/arch/x86_64/sse2/memchr.rs +++ b/src/arch/x86_64/sse2/memchr.rs @@ -142,13 +142,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -198,13 +198,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -243,13 +243,13 @@ impl One { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `0` will always be returned. @@ -506,13 +506,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -562,13 +562,13 @@ impl Two { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -807,13 +807,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -865,13 +865,13 @@ impl Three { /// * Both `start` and `end` must be valid for reads. /// * Both `start` and `end` must point to an initialized value. /// * Both `start` and `end` must point to the same allocated object and - /// must either be in bounds or at most one byte past the end of the - /// allocated object. + /// must either be in bounds or at most one byte past the end of the + /// allocated object. /// * Both `start` and `end` must be _derived from_ a pointer to the same - /// object. + /// object. /// * The distance between `start` and `end` must not overflow `isize`. /// * The distance being in bounds must not rely on "wrapping around" the - /// address space. + /// address space. /// /// Note that callers may pass a pair of pointers such that `start >= end`. /// In that case, `None` will always be returned. @@ -1032,10 +1032,9 @@ mod tests { #[test] fn forward_two() { crate::tests::memchr::Runner::new(2).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2] => Some(Two::new(n1, n2)?.iter(haystack).collect()), + _ => None, }, ) } @@ -1043,10 +1042,11 @@ mod tests { #[test] fn reverse_two() { crate::tests::memchr::Runner::new(2).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2] => { + Some(Two::new(n1, n2)?.iter(haystack).rev().collect()) + } + _ => None, }, ) } @@ -1054,11 +1054,11 @@ mod tests { #[test] fn forward_three() { crate::tests::memchr::Runner::new(3).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(Three::new(n1, n2, n3)?.iter(haystack).collect()) + } + _ => None, }, ) } @@ -1066,11 +1066,11 @@ mod tests { #[test] fn reverse_three() { crate::tests::memchr::Runner::new(3).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(Three::new(n1, n2, n3)?.iter(haystack).rev().collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => Some( + Three::new(n1, n2, n3)?.iter(haystack).rev().collect(), + ), + _ => None, }, ) } diff --git a/src/ext.rs b/src/ext.rs index 802697a..5404e63 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,5 +1,5 @@ /// A trait for adding some helper routines to pointers. -pub(crate) trait Pointer { +pub(crate) trait Pointer: Copy { /// Returns the distance, in units of `T`, between `self` and `origin`. /// /// # Safety diff --git a/src/lib.rs b/src/lib.rs index b310516..e054fa1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,36 +137,36 @@ accelerated implementations of `memchr` (and friends) and `memmem`. # Crate features * **std** - When enabled (the default), this will permit features specific to -the standard library. Currently, the only thing used from the standard library -is runtime SIMD CPU feature detection. This means that this feature must be -enabled to get AVX2 accelerated routines on `x86_64` targets without enabling -the `avx2` feature at compile time, for example. When `std` is not enabled, -this crate will still attempt to use SSE2 accelerated routines on `x86_64`. It -will also use AVX2 accelerated routines when the `avx2` feature is enabled at -compile time. In general, enable this feature if you can. + the standard library. Currently, the only thing used from the standard library + is runtime SIMD CPU feature detection. This means that this feature must be + enabled to get AVX2 accelerated routines on `x86_64` targets without enabling + the `avx2` feature at compile time, for example. When `std` is not enabled, + this crate will still attempt to use SSE2 accelerated routines on `x86_64`. It + will also use AVX2 accelerated routines when the `avx2` feature is enabled at + compile time. In general, enable this feature if you can. * **alloc** - When enabled (the default), APIs in this crate requiring some -kind of allocation will become available. For example, the -[`memmem::Finder::into_owned`](crate::memmem::Finder::into_owned) API and the -[`arch::all::shiftor`](crate::arch::all::shiftor) substring search -implementation. Otherwise, this crate is designed from the ground up to be -usable in core-only contexts, so the `alloc` feature doesn't add much -currently. Notably, disabling `std` but enabling `alloc` will **not** result -in the use of AVX2 on `x86_64` targets unless the `avx2` feature is enabled -at compile time. (With `std` enabled, AVX2 can be used even without the `avx2` -feature enabled at compile time by way of runtime CPU feature detection.) + kind of allocation will become available. For example, the + [`memmem::Finder::into_owned`](crate::memmem::Finder::into_owned) API and the + [`arch::all::shiftor`] substring search implementation. Otherwise, this crate + is designed from the ground up to be usable in core-only contexts, so the + `alloc` feature doesn't add much currently. Notably, disabling `std` but + enabling `alloc` will **not** result in the use of AVX2 on `x86_64` targets + unless the `avx2` feature is enabled at compile time. (With `std` enabled, + AVX2 can be used even without the `avx2` feature enabled at compile time by + way of runtime CPU feature detection.) * **logging** - When enabled (disabled by default), the `log` crate is used -to emit log messages about what kinds of `memchr` and `memmem` algorithms -are used. Namely, both `memchr` and `memmem` have a number of different -implementation choices depending on the target and CPU, and the log messages -can help show what specific implementations are being used. Generally, this is -useful for debugging performance issues. + to emit log messages about what kinds of `memchr` and `memmem` algorithms + are used. Namely, both `memchr` and `memmem` have a number of different + implementation choices depending on the target and CPU, and the log messages + can help show what specific implementations are being used. Generally, this is + useful for debugging performance issues. * **libc** - **DEPRECATED**. Previously, this enabled the use of the target's -`memchr` function from whatever `libc` was linked into the program. This -feature is now a no-op because this crate's implementation of `memchr` should -now be sufficiently fast on a number of platforms that `libc` should no longer -be needed. (This feature is somewhat of a holdover from this crate's origins. -Originally, this crate was literally just a safe wrapper function around the -`memchr` function from `libc`.) + `memchr` function from whatever `libc` was linked into the program. This + feature is now a no-op because this crate's implementation of `memchr` should + now be sufficiently fast on a number of platforms that `libc` should no longer + be needed. (This feature is somewhat of a holdover from this crate's origins. + Originally, this crate was literally just a safe wrapper function around the + `memchr` function from `libc`.) */ #![deny(missing_docs)] diff --git a/src/memchr.rs b/src/memchr.rs index 92a18bd..ca37013 100644 --- a/src/memchr.rs +++ b/src/memchr.rs @@ -213,7 +213,7 @@ pub fn memrchr3( /// The iterator returned implements `DoubleEndedIterator`. This means it /// can also be used to find occurrences in reverse order. #[inline] -pub fn memchr_iter<'h>(needle: u8, haystack: &'h [u8]) -> Memchr<'h> { +pub fn memchr_iter(needle: u8, haystack: &[u8]) -> Memchr<'_> { Memchr::new(needle, haystack) } @@ -229,11 +229,7 @@ pub fn memrchr_iter(needle: u8, haystack: &[u8]) -> Rev> { /// The iterator returned implements `DoubleEndedIterator`. This means it /// can also be used to find occurrences in reverse order. #[inline] -pub fn memchr2_iter<'h>( - needle1: u8, - needle2: u8, - haystack: &'h [u8], -) -> Memchr2<'h> { +pub fn memchr2_iter(needle1: u8, needle2: u8, haystack: &[u8]) -> Memchr2<'_> { Memchr2::new(needle1, needle2, haystack) } @@ -253,12 +249,12 @@ pub fn memrchr2_iter( /// The iterator returned implements `DoubleEndedIterator`. This means it /// can also be used to find occurrences in reverse order. #[inline] -pub fn memchr3_iter<'h>( +pub fn memchr3_iter( needle1: u8, needle2: u8, needle3: u8, - haystack: &'h [u8], -) -> Memchr3<'h> { + haystack: &[u8], +) -> Memchr3<'_> { Memchr3::new(needle1, needle2, needle3, haystack) } @@ -796,10 +792,9 @@ mod tests { #[test] fn forward2_iter() { crate::tests::memchr::Runner::new(2).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(memchr2_iter(n1, n2, haystack).collect()) + |haystack, needles| match needles { + &[n1, n2] => Some(memchr2_iter(n1, n2, haystack).collect()), + _ => None, }, ) } @@ -807,10 +802,9 @@ mod tests { #[test] fn forward2_oneshot() { crate::tests::memchr::Runner::new(2).forward_oneshot( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(memchr2(n1, n2, haystack)) + |haystack, needles| match needles { + &[n1, n2] => Some(memchr2(n1, n2, haystack)), + _ => None, }, ) } @@ -818,10 +812,9 @@ mod tests { #[test] fn reverse2_iter() { crate::tests::memchr::Runner::new(2).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(memrchr2_iter(n1, n2, haystack).collect()) + |haystack, needles| match needles { + &[n1, n2] => Some(memrchr2_iter(n1, n2, haystack).collect()), + _ => None, }, ) } @@ -829,10 +822,9 @@ mod tests { #[test] fn reverse2_oneshot() { crate::tests::memchr::Runner::new(2).reverse_oneshot( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - Some(memrchr2(n1, n2, haystack)) + |haystack, needles| match needles { + &[n1, n2] => Some(memrchr2(n1, n2, haystack)), + _ => None, }, ) } @@ -840,11 +832,11 @@ mod tests { #[test] fn forward3_iter() { crate::tests::memchr::Runner::new(3).forward_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(memchr3_iter(n1, n2, n3, haystack).collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(memchr3_iter(n1, n2, n3, haystack).collect()) + } + _ => None, }, ) } @@ -852,11 +844,9 @@ mod tests { #[test] fn forward3_oneshot() { crate::tests::memchr::Runner::new(3).forward_oneshot( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(memchr3(n1, n2, n3, haystack)) + |haystack, needles| match needles { + &[n1, n2, n3] => Some(memchr3(n1, n2, n3, haystack)), + _ => None, }, ) } @@ -864,11 +854,11 @@ mod tests { #[test] fn reverse3_iter() { crate::tests::memchr::Runner::new(3).reverse_iter( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(memrchr3_iter(n1, n2, n3, haystack).collect()) + |haystack, needles| match needles { + &[n1, n2, n3] => { + Some(memrchr3_iter(n1, n2, n3, haystack).collect()) + } + _ => None, }, ) } @@ -876,11 +866,9 @@ mod tests { #[test] fn reverse3_oneshot() { crate::tests::memchr::Runner::new(3).reverse_oneshot( - |haystack, needles| { - let n1 = needles.get(0).copied()?; - let n2 = needles.get(1).copied()?; - let n3 = needles.get(2).copied()?; - Some(memrchr3(n1, n2, n3, haystack)) + |haystack, needles| match needles { + &[n1, n2, n3] => Some(memrchr3(n1, n2, n3, haystack)), + _ => None, }, ) } diff --git a/src/memmem/searcher.rs b/src/memmem/searcher.rs index 2a533e0..81bedc6 100644 --- a/src/memmem/searcher.rs +++ b/src/memmem/searcher.rs @@ -630,10 +630,8 @@ impl Prefilter { if rarest_rank > MAX_FALLBACK_RANK { None } else { - let finder = crate::arch::all::packedpair::Finder::with_pair( - needle, - pair.clone(), - )?; + let finder = + crate::arch::all::packedpair::Finder::with_pair(needle, pair)?; let call = prefilter_kind_fallback; let kind = PrefilterKind { fallback: finder }; Some(Prefilter { call, kind, rarest_byte, rarest_offset }) @@ -915,7 +913,7 @@ impl PrefilterState { // `skipped` to overflow a `u32`. (And we use a `u32` to reduce the // size of a prefilter state.) self.skipped = match u32::try_from(skipped) { - Err(_) => core::u32::MAX, + Err(_) => u32::MAX, Ok(skipped) => self.skipped.saturating_add(skipped), }; } diff --git a/src/tests/memchr/mod.rs b/src/tests/memchr/mod.rs index 0564ad4..c343929 100644 --- a/src/tests/memchr/mod.rs +++ b/src/tests/memchr/mod.rs @@ -10,7 +10,7 @@ pub(crate) mod naive; #[macro_use] pub(crate) mod prop; -const SEEDS: &'static [Seed] = &[ +const SEEDS: &[Seed] = &[ Seed { haystack: "a", needles: &[b'a'], positions: &[0] }, Seed { haystack: "aa", needles: &[b'a'], positions: &[0, 1] }, Seed { haystack: "aaa", needles: &[b'a'], positions: &[0, 1, 2] }, @@ -288,16 +288,14 @@ impl Seed { // Add bytes to the start of the corpus. for i in 0..Seed::EXPAND_LEN { let mut t = Test::new(self); - let mut new: String = core::iter::repeat('%').take(i).collect(); - new.push_str(&t.haystack); - t.haystack = new; + t.haystack = "%".repeat(i) + &t.haystack; t.expected = t.expected.into_iter().map(|p| p + i).collect(); more.push(t); } // Add bytes to the end of the corpus. for i in 1..Seed::EXPAND_LEN { let mut t = Test::new(self); - let padding: String = core::iter::repeat('%').take(i).collect(); + let padding: String = "%".repeat(i); t.haystack.push_str(&padding); more.push(t); } diff --git a/src/tests/memchr/prop.rs b/src/tests/memchr/prop.rs index b988260..83409f6 100644 --- a/src/tests/memchr/prop.rs +++ b/src/tests/memchr/prop.rs @@ -15,7 +15,7 @@ macro_rules! define_memchr_quickcheck { use quickcheck::TestResult; - use crate::tests::memchr::{ + use $crate::tests::memchr::{ naive, prop::{double_ended_take, naive1_iter, naive2_iter, naive3_iter}, }; @@ -263,21 +263,16 @@ where let mut found_front = alloc::vec![]; let mut found_back = alloc::vec![]; - for take_front in take_side { - if take_front { - if let Some(pos) = iter.next() { - found_front.push(pos); + (|| { + for take_front in take_side { + if take_front { + found_front.push(iter.next()?); } else { - break; + found_back.push(iter.next_back()?); } - } else { - if let Some(pos) = iter.next_back() { - found_back.push(pos); - } else { - break; - } - }; - } + } + None::<()> + })(); let mut all_found = found_front; all_found.extend(found_back.into_iter().rev()); @@ -286,19 +281,19 @@ where // return an iterator of the 0-based indices of haystack that match the needle #[cfg(not(miri))] -pub(crate) fn naive1_iter<'a>( +pub(crate) fn naive1_iter( n1: u8, - haystack: &'a [u8], -) -> impl DoubleEndedIterator + 'a { + haystack: &[u8], +) -> impl DoubleEndedIterator + '_ { haystack.iter().enumerate().filter(move |&(_, &b)| b == n1).map(|t| t.0) } #[cfg(not(miri))] -pub(crate) fn naive2_iter<'a>( +pub(crate) fn naive2_iter( n1: u8, n2: u8, - haystack: &'a [u8], -) -> impl DoubleEndedIterator + 'a { + haystack: &[u8], +) -> impl DoubleEndedIterator + '_ { haystack .iter() .enumerate() @@ -307,12 +302,12 @@ pub(crate) fn naive2_iter<'a>( } #[cfg(not(miri))] -pub(crate) fn naive3_iter<'a>( +pub(crate) fn naive3_iter( n1: u8, n2: u8, n3: u8, - haystack: &'a [u8], -) -> impl DoubleEndedIterator + 'a { + haystack: &[u8], +) -> impl DoubleEndedIterator + '_ { haystack .iter() .enumerate() diff --git a/src/tests/packedpair.rs b/src/tests/packedpair.rs index 204635b..2ee0f7c 100644 --- a/src/tests/packedpair.rs +++ b/src/tests/packedpair.rs @@ -24,6 +24,7 @@ const SEEDS: &[Seed] = &[ /// /// These tests specifically look for the occurrence of a possible substring /// match based on a pair of bytes matching at the right offsets. +#[allow(clippy::type_complexity)] pub(crate) struct Runner { fwd: Option< Box< diff --git a/src/tests/substring/mod.rs b/src/tests/substring/mod.rs index dd10cbd..919ec4c 100644 --- a/src/tests/substring/mod.rs +++ b/src/tests/substring/mod.rs @@ -12,7 +12,7 @@ pub(crate) mod naive; #[macro_use] pub(crate) mod prop; -const SEEDS: &'static [Seed] = &[ +const SEEDS: &[Seed] = &[ Seed::new("", "", Some(0), Some(0)), Seed::new("", "a", Some(0), Some(1)), Seed::new("", "ab", Some(0), Some(2)), @@ -63,6 +63,7 @@ const SEEDS: &'static [Seed] = &[ /// for a subset of needles/haystacks. For example, the "packed pair" substring /// search implementation only works for haystacks of some minimum length based /// of the pair of bytes selected and the size of the vector used. +#[allow(clippy::type_complexity)] pub(crate) struct Runner { fwd: Option< Box Option> + 'static>, diff --git a/src/tests/substring/naive.rs b/src/tests/substring/naive.rs index 1bc6009..209cd1e 100644 --- a/src/tests/substring/naive.rs +++ b/src/tests/substring/naive.rs @@ -8,23 +8,13 @@ The idea is that they are so simple that they are unlikely to be incorrect. /// Naively search forwards for the given needle in the given haystack. pub(crate) fn find(haystack: &[u8], needle: &[u8]) -> Option { let end = haystack.len().checked_sub(needle.len()).map_or(0, |i| i + 1); - for i in 0..end { - if needle == &haystack[i..i + needle.len()] { - return Some(i); - } - } - None + (0..end).find(|&i| needle == &haystack[i..i + needle.len()]) } /// Naively search in reverse for the given needle in the given haystack. pub(crate) fn rfind(haystack: &[u8], needle: &[u8]) -> Option { let end = haystack.len().checked_sub(needle.len()).map_or(0, |i| i + 1); - for i in (0..end).rev() { - if needle == &haystack[i..i + needle.len()] { - return Some(i); - } - } - None + (0..end).rev().find(|&i| needle == &haystack[i..i + needle.len()]) } #[cfg(test)] diff --git a/src/tests/substring/prop.rs b/src/tests/substring/prop.rs index a8352ec..13399d3 100644 --- a/src/tests/substring/prop.rs +++ b/src/tests/substring/prop.rs @@ -17,18 +17,18 @@ macro_rules! define_substring_forward_quickcheck { #[cfg(not(miri))] quickcheck::quickcheck! { fn qc_fwd_prefix_is_substring(bs: alloc::vec::Vec) -> bool { - crate::tests::substring::prop::prefix_is_substring(&bs, $fwd) + $crate::tests::substring::prop::prefix_is_substring(&bs, $fwd) } fn qc_fwd_suffix_is_substring(bs: alloc::vec::Vec) -> bool { - crate::tests::substring::prop::suffix_is_substring(&bs, $fwd) + $crate::tests::substring::prop::suffix_is_substring(&bs, $fwd) } fn qc_fwd_matches_naive( haystack: alloc::vec::Vec, needle: alloc::vec::Vec ) -> bool { - crate::tests::substring::prop::same_as_naive( + $crate::tests::substring::prop::same_as_naive( false, &haystack, &needle, @@ -48,18 +48,18 @@ macro_rules! define_substring_reverse_quickcheck { #[cfg(not(miri))] quickcheck::quickcheck! { fn qc_rev_prefix_is_substring(bs: alloc::vec::Vec) -> bool { - crate::tests::substring::prop::prefix_is_substring(&bs, $rev) + $crate::tests::substring::prop::prefix_is_substring(&bs, $rev) } fn qc_rev_suffix_is_substring(bs: alloc::vec::Vec) -> bool { - crate::tests::substring::prop::suffix_is_substring(&bs, $rev) + $crate::tests::substring::prop::suffix_is_substring(&bs, $rev) } fn qc_rev_matches_naive( haystack: alloc::vec::Vec, needle: alloc::vec::Vec ) -> bool { - crate::tests::substring::prop::same_as_naive( + $crate::tests::substring::prop::same_as_naive( true, &haystack, &needle, @@ -77,11 +77,7 @@ pub(crate) fn prefix_is_substring( ) -> bool { for i in 0..bs.len().saturating_sub(1) { let prefix = &bs[..i]; - let result = match search(bs, prefix) { - None => continue, - Some(result) => result, - }; - if !result.is_some() { + if search(bs, prefix) == Some(None) { return false; } } @@ -95,11 +91,7 @@ pub(crate) fn suffix_is_substring( ) -> bool { for i in 0..bs.len().saturating_sub(1) { let suffix = &bs[i..]; - let result = match search(bs, suffix) { - None => continue, - Some(result) => result, - }; - if !result.is_some() { + if search(bs, suffix) == Some(None) { return false; } } diff --git a/src/vector.rs b/src/vector.rs index d86fbca..6bf6b32 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -114,9 +114,17 @@ pub(crate) trait MoveMask: Copy + core::fmt::Debug { /// /// We call this "sensible" because this is what we get using native sse/avx /// movemask instructions. But neon has no such native equivalent. +#[cfg(any( + target_arch = "x86_64", + all(target_arch = "wasm32", target_feature = "simd128") +))] #[derive(Clone, Copy, Debug)] pub(crate) struct SensibleMoveMask(u32); +#[cfg(any( + target_arch = "x86_64", + all(target_arch = "wasm32", target_feature = "simd128") +))] impl SensibleMoveMask { /// Get the mask in a form suitable for computing offsets. /// @@ -135,6 +143,10 @@ impl SensibleMoveMask { } } +#[cfg(any( + target_arch = "x86_64", + all(target_arch = "wasm32", target_feature = "simd128") +))] impl MoveMask for SensibleMoveMask { #[inline(always)] fn all_zeros_except_least_significant(n: usize) -> SensibleMoveMask {