Skip to content

Commit cd7a361

Browse files
committed
Add new unstable feature const_eq_ignore_ascii_case
Mark `[u8]`, `str` `eq_ignore_ascii_case` functions const
1 parent 9322d18 commit cd7a361

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
#![feature(const_bigint_helper_methods)]
119119
#![feature(const_black_box)]
120120
#![feature(const_char_encode_utf16)]
121+
#![feature(const_eq_ignore_ascii_case)]
121122
#![feature(const_eval_select)]
122123
#![feature(const_exact_div)]
123124
#![feature(const_fmt_arguments_new)]

library/core/src/slice/ascii.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,28 @@ impl [u8] {
5151
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
5252
/// but without allocating and copying temporaries.
5353
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
54+
#[rustc_const_unstable(feature = "const_eq_ignore_ascii_case", issue = "131719")]
5455
#[must_use]
5556
#[inline]
56-
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
57-
self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b))
57+
pub const fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
58+
if self.len() != other.len() {
59+
return false;
60+
}
61+
62+
// FIXME(const-hack): Revert this implementation when `core::iter::zip` is allowed in const.
63+
let mut a = self;
64+
let mut b = other;
65+
66+
while let ([first_a, rest_a @ ..], [first_b, rest_b @ ..]) = (a, b) {
67+
if first_a.eq_ignore_ascii_case(&first_b) {
68+
a = rest_a;
69+
b = rest_b;
70+
} else {
71+
return false;
72+
}
73+
}
74+
75+
true
5876
}
5977

6078
/// Converts this slice to its ASCII upper case equivalent in-place.

library/core/src/str/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2449,9 +2449,10 @@ impl str {
24492449
/// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
24502450
/// ```
24512451
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2452+
#[rustc_const_unstable(feature = "const_eq_ignore_ascii_case", issue = "131719")]
24522453
#[must_use]
24532454
#[inline]
2454-
pub fn eq_ignore_ascii_case(&self, other: &str) -> bool {
2455+
pub const fn eq_ignore_ascii_case(&self, other: &str) -> bool {
24552456
self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
24562457
}
24572458

0 commit comments

Comments
 (0)