Skip to main content

nu_utils/
const_str.rs

1/// Check at const time the equality of two strings.
2pub const fn eq(a: &str, b: &str) -> bool {
3    let mut a_bytes = a.as_bytes();
4    let mut b_bytes = b.as_bytes();
5
6    if a_bytes.len() != b_bytes.len() {
7        return false;
8    }
9
10    while let ([a, a_rest @ ..], [b, b_rest @ ..]) = (a_bytes, b_bytes) {
11        a_bytes = a_rest;
12        b_bytes = b_rest;
13        if *a != *b {
14            return false;
15        }
16    }
17
18    true
19}