|
3 | 3 | use core::ascii::EscapeDefault; |
4 | 4 |
|
5 | 5 | use crate::fmt::{self, Write}; |
| 6 | +use crate::hint::assert_unchecked; |
6 | 7 | #[cfg(not(all(target_arch = "x86_64", target_feature = "sse2")))] |
7 | 8 | use crate::intrinsics::const_eval_select; |
8 | 9 | use crate::{ascii, iter, ops}; |
@@ -57,25 +58,34 @@ impl [u8] { |
57 | 58 | #[must_use] |
58 | 59 | #[inline] |
59 | 60 | pub const fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { |
| 61 | + const CHUNK_SIZE: usize = 16; |
60 | 62 | if self.len() != other.len() { |
61 | 63 | return false; |
62 | 64 | } |
63 | | - |
64 | | - // FIXME(const-hack): This implementation can be reverted when |
65 | | - // `core::iter::zip` is allowed in const. The original implementation: |
66 | | - // self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b)) |
67 | | - let mut a = self; |
68 | | - let mut b = other; |
69 | | - |
70 | | - while let ([first_a, rest_a @ ..], [first_b, rest_b @ ..]) = (a, b) { |
71 | | - if first_a.eq_ignore_ascii_case(&first_b) { |
72 | | - a = rest_a; |
73 | | - b = rest_b; |
74 | | - } else { |
| 65 | + // FIXME(const-hack): use chunks_exact or for loops. |
| 66 | + // Check in chunks for vectorization. |
| 67 | + let mut i = 0; |
| 68 | + while i + CHUNK_SIZE <= self.len() { |
| 69 | + let mut equal = true; |
| 70 | + let mut j = 0; |
| 71 | + while j < CHUNK_SIZE { |
| 72 | + // SAFETY: Outer loop stops before we could go out of bounds. |
| 73 | + unsafe { assert_unchecked(i + j < self.len()) } |
| 74 | + equal &= self[i + j].eq_ignore_ascii_case(&other[i + j]); |
| 75 | + j += 1; |
| 76 | + } |
| 77 | + if !equal { |
75 | 78 | return false; |
76 | 79 | } |
| 80 | + i += CHUNK_SIZE; |
| 81 | + } |
| 82 | + // Process the remainder that did not fit the above chunks. |
| 83 | + while i < self.len() { |
| 84 | + if !self[i].eq_ignore_ascii_case(&other[i]) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + i += 1; |
77 | 88 | } |
78 | | - |
79 | 89 | true |
80 | 90 | } |
81 | 91 |
|
|
0 commit comments