Skip to content

Commit b0d7515

Browse files
Add str::eq_ignore_case()
With an unoptimized, non-`const` implementation for now.
1 parent a5d82ca commit b0d7515

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

library/core/src/str/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,9 @@ impl str {
28262826
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
28272827
/// but without allocating and copying temporaries.
28282828
///
2829+
/// For Unicode-aware case-insensitive matching, consider
2830+
/// [`str::eq_ignore_case`].
2831+
///
28292832
/// # Examples
28302833
///
28312834
/// ```
@@ -2841,6 +2844,38 @@ impl str {
28412844
self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
28422845
}
28432846

2847+
/// Checks that two strings are a caseless match, according to
2848+
/// [Definition 144] in Chapter 3 of the Unicode Standard.
2849+
///
2850+
/// [Definition 144]: https://www.unicode.org/versions/latest/core-spec/chapter-3/#G53513
2851+
///
2852+
/// Same as `a.to_casefold() == b.to_casefold()`,
2853+
/// but without allocating. See that method's documentation,
2854+
/// and [`char::to_casefold()`],
2855+
/// for more information about case folding.
2856+
///
2857+
/// No normalization (e.g. NFC) is performed,
2858+
/// so visually and semantically identical strings
2859+
/// might still compare unequal. In addition,
2860+
/// this method is independent of language/locale,
2861+
/// so the special behavior of I/ı/İ/i
2862+
/// in Turkish and Azeri is not handled.
2863+
///
2864+
/// # Examples
2865+
///
2866+
/// ```
2867+
/// #![feature(casefold)]
2868+
/// assert!("Ferris".eq_ignore_case("FERRIS"));
2869+
/// assert!("Ferrös".eq_ignore_case("FERRÖS"));
2870+
/// assert!("ẞ".eq_ignore_case("ss"));
2871+
/// ```
2872+
#[unstable(feature = "casefold", issue = "none")]
2873+
#[must_use]
2874+
#[inline]
2875+
pub fn eq_ignore_case(&self, other: &str) -> bool {
2876+
self.chars().flat_map(char::to_casefold).eq(other.chars().flat_map(char::to_casefold))
2877+
}
2878+
28442879
/// Converts this string to its ASCII upper case equivalent in-place.
28452880
///
28462881
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',

0 commit comments

Comments
 (0)