Auto merge of #131721 - okaneco:const_eq_ignore_ascii_case, r=m-ou-se

Add new unstable feature `const_eq_ignore_ascii_case`

Tracking issue - #131719

Mark `[u8]`, `str` `eq_ignore_ascii_case` functions const

---

The codegen for this implementation matches the existing `iter::zip` implementation better than incrementing with a counter

while loop with counter - https://rust.godbolt.org/z/h9cs5zajc
while let - https://rust.godbolt.org/z/ecMeMjjEb
This commit is contained in:
bors 2024-11-06 09:08:53 +00:00
commit 2796048328
3 changed files with 25 additions and 3 deletions

View File

@ -115,6 +115,7 @@
#![feature(const_align_of_val_raw)] #![feature(const_align_of_val_raw)]
#![feature(const_alloc_layout)] #![feature(const_alloc_layout)]
#![feature(const_black_box)] #![feature(const_black_box)]
#![feature(const_eq_ignore_ascii_case)]
#![feature(const_eval_select)] #![feature(const_eval_select)]
#![feature(const_float_methods)] #![feature(const_float_methods)]
#![feature(const_heap)] #![feature(const_heap)]

View File

@ -52,10 +52,30 @@ pub const fn as_ascii(&self) -> Option<&[ascii::Char]> {
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`, /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
/// but without allocating and copying temporaries. /// but without allocating and copying temporaries.
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_eq_ignore_ascii_case", issue = "131719")]
#[must_use] #[must_use]
#[inline] #[inline]
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { pub const 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)) 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. /// Converts this slice to its ASCII upper case equivalent in-place.

View File

@ -2474,9 +2474,10 @@ pub const fn as_ascii(&self) -> Option<&[ascii::Char]> {
/// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS")); /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
/// ``` /// ```
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[rustc_const_unstable(feature = "const_eq_ignore_ascii_case", issue = "131719")]
#[must_use] #[must_use]
#[inline] #[inline]
pub fn eq_ignore_ascii_case(&self, other: &str) -> bool { pub const fn eq_ignore_ascii_case(&self, other: &str) -> bool {
self.as_bytes().eq_ignore_ascii_case(other.as_bytes()) self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
} }