diff options
| author | okaneco <47607823+okaneco@users.noreply.github.com> | 2024-10-14 21:02:49 -0400 |
|---|---|---|
| committer | okaneco <47607823+okaneco@users.noreply.github.com> | 2024-11-05 22:22:31 -0500 |
| commit | dedc441fa5e370d8ec4eeb6df8cb13f6ab179e13 (patch) | |
| tree | dd9f845a502d14ce6c6b4d07f41f032c272226aa /library/core/src/slice | |
| parent | 4a91ff6bb5f36a94d8ebebd9cad1f5f831abbe7a (diff) | |
| download | rust-dedc441fa5e370d8ec4eeb6df8cb13f6ab179e13.tar.gz rust-dedc441fa5e370d8ec4eeb6df8cb13f6ab179e13.zip | |
Add new unstable feature `const_eq_ignore_ascii_case`
Mark `[u8]`, `str` `eq_ignore_ascii_case` functions const
Diffstat (limited to 'library/core/src/slice')
| -rw-r--r-- | library/core/src/slice/ascii.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/library/core/src/slice/ascii.rs b/library/core/src/slice/ascii.rs index 58ba3a1573a..17ad4fd8f67 100644 --- a/library/core/src/slice/ascii.rs +++ b/library/core/src/slice/ascii.rs @@ -52,10 +52,30 @@ impl [u8] { /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`, /// but without allocating and copying temporaries. #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[rustc_const_unstable(feature = "const_eq_ignore_ascii_case", issue = "131719")] #[must_use] #[inline] - pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { - self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b)) + pub const fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { + if self.len() != other.len() { + return false; + } + + // FIXME(const-hack): This implementation can be reverted when + // `core::iter::zip` is allowed in const. The original implementation: + // self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b)) + let mut a = self; + let mut b = other; + + while let ([first_a, rest_a @ ..], [first_b, rest_b @ ..]) = (a, b) { + if first_a.eq_ignore_ascii_case(&first_b) { + a = rest_a; + b = rest_b; + } else { + return false; + } + } + + true } /// Converts this slice to its ASCII upper case equivalent in-place. |
