const_str/__ctfe/
eq_ignore_ascii_case.rs

1pub struct EqIgnoreAsciiCase<T1, T2>(pub T1, pub T2);
2
3const fn eq_ignore_ascii_case(lhs: &[u8], rhs: &[u8]) -> bool {
4    if lhs.len() != rhs.len() {
5        return false;
6    }
7    let mut i = 0;
8    while i < lhs.len() {
9        let l = lhs[i].to_ascii_lowercase();
10        let r = rhs[i].to_ascii_lowercase();
11        if l != r {
12            return false;
13        }
14        i += 1;
15    }
16    true
17}
18
19impl EqIgnoreAsciiCase<&[u8], &[u8]> {
20    pub const fn const_eval(&self) -> bool {
21        eq_ignore_ascii_case(self.0, self.1)
22    }
23}
24
25impl EqIgnoreAsciiCase<&str, &str> {
26    pub const fn const_eval(&self) -> bool {
27        eq_ignore_ascii_case(self.0.as_bytes(), self.1.as_bytes())
28    }
29}
30
31impl<const N1: usize, const N2: usize> EqIgnoreAsciiCase<&[u8; N1], &[u8; N2]> {
32    pub const fn const_eval(&self) -> bool {
33        eq_ignore_ascii_case(self.0.as_slice(), self.1.as_slice())
34    }
35}
36
37/// Checks that two (string) slices are an ASCII case-insensitive match.
38///
39/// The input type must be one of:
40/// + [`&str`](str)
41/// + [`&[u8]`](slice)
42/// + [`&[u8; N]`](array)
43///
44/// The output type is [`bool`].
45///
46/// This macro is [const-fn compatible](./index.html#const-fn-compatible).
47///
48/// # Examples
49///
50/// ```
51/// use const_str::eq_ignore_ascii_case;
52///
53/// const _: () = {
54///     assert!(eq_ignore_ascii_case!("Ferris", "FERRIS"));     // true
55///     assert!(!eq_ignore_ascii_case!(b"Ferris", b"FERRI"));   // false
56///
57///     assert!(eq_ignore_ascii_case!("Ferrös", "FERRöS"));     // true
58///     //                              ^^^ ^     ^^^ ^     
59///
60///     assert!(!eq_ignore_ascii_case!("Ferrös", "FERRÖS"));    // false
61///     //                                  ^         ^
62/// };
63/// ```
64#[macro_export]
65macro_rules! eq_ignore_ascii_case {
66    ($lhs:expr, $rhs:expr) => {
67        $crate::__ctfe::EqIgnoreAsciiCase($lhs, $rhs).const_eval()
68    };
69}