|
2 | 2 |
|
3 | 3 | use crate::ascii;
|
4 | 4 | use crate::fmt::{self, Write};
|
| 5 | +use crate::intrinsics; |
5 | 6 | use crate::iter;
|
6 | 7 | use crate::mem;
|
7 | 8 | use crate::ops;
|
@@ -52,10 +53,33 @@ impl [u8] {
|
52 | 53 | /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
|
53 | 54 | /// but without allocating and copying temporaries.
|
54 | 55 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
| 56 | + #[rustc_const_unstable(feature = "const_slice_eq_ignore_ascii_case", issue = "none")] |
55 | 57 | #[must_use]
|
56 | 58 | #[inline]
|
57 |
| - pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { |
58 |
| - self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b)) |
| 59 | + pub const fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { |
| 60 | + const fn eq_ignore_ascii_case_ct(a: &[u8], b: &[u8]) -> bool { |
| 61 | + a.len() == b.len() && { |
| 62 | + let mut i = 0; |
| 63 | + while i < a.len() { |
| 64 | + if !u8::eq_ignore_ascii_case(&a[i], &b[i]) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + i += 1; |
| 68 | + } |
| 69 | + true |
| 70 | + } |
| 71 | + } |
| 72 | + fn eq_ignore_ascii_case_rt(a: &[u8], b: &[u8]) -> bool { |
| 73 | + a.len() == b.len() && iter::zip(a, b).all(|(a, b)| u8::eq_ignore_ascii_case(a, b)) |
| 74 | + } |
| 75 | + // SAFETY: both branches compute the same result, the runtime one is just more optimized |
| 76 | + unsafe { |
| 77 | + intrinsics::const_eval_select( |
| 78 | + (self, other), |
| 79 | + eq_ignore_ascii_case_ct, |
| 80 | + eq_ignore_ascii_case_rt, |
| 81 | + ) |
| 82 | + } |
59 | 83 | }
|
60 | 84 |
|
61 | 85 | /// Converts this slice to its ASCII upper case equivalent in-place.
|
|
0 commit comments