tl/simd/
mod.rs

1use crate::util;
2
3/// Fallback functions, used for the last chunk not divisible by the chunk sice
4pub mod fallback;
5/// nightly-only functions using portable_simd
6#[cfg(feature = "simd")]
7pub mod nightly;
8/// Stable, "fallback" functions that this library uses as a fallback until portable_simd becomes stable
9#[cfg(not(feature = "simd"))]
10pub mod stable;
11
12macro_rules! decide {
13    ($nightly:expr, $stable:expr) => {{
14        #[cfg(feature = "simd")]
15        {
16            $nightly
17        }
18        #[cfg(not(feature = "simd"))]
19        {
20            $stable
21        }
22    }};
23}
24
25/// Checks if the given byte is a "closing" byte (/ or >)
26#[inline]
27pub fn is_closing(needle: u8) -> bool {
28    decide!(nightly::is_closing(needle), stable::is_closing(needle))
29}
30
31/// Searches for the first non-identifier in `haystack`
32#[inline]
33pub fn search_non_ident(haystack: &[u8]) -> Option<usize> {
34    decide!(
35        nightly::search_non_ident(haystack),
36        fallback::search_non_ident(haystack)
37    )
38}
39
40/// Searches for the first occurence in `haystack`
41#[inline]
42pub fn find4(haystack: &[u8], needle: [u8; 4]) -> Option<usize> {
43    decide!(
44        nightly::find4(haystack, needle),
45        stable::find_multi(haystack, needle)
46    )
47}
48
49/// Searches for the first occurence of `needle` in `haystack`
50#[inline]
51pub fn find(haystack: &[u8], needle: u8) -> Option<usize> {
52    decide!(
53        nightly::find(haystack, needle),
54        stable::find(haystack, needle)
55    )
56}
57
58/// Checks if the ASCII characters in `haystack` match `needle` (case insensitive)
59pub fn matches_case_insensitive<const N: usize>(haystack: &[u8], needle: [u8; N]) -> bool {
60    if haystack.len() != N {
61        return false;
62    }
63
64    // LLVM seems to already generate pretty good SIMD even without explicit use
65
66    let mut mask = true;
67    for i in 0..N {
68        mask &= util::to_lower(haystack[i]) == needle[i];
69    }
70    mask
71}