const_format/
slice_cmp.rs

1/// A const equivalent of `&str` equality comparison.
2///
3/// # Example
4///
5#[cfg_attr(feature = "fmt", doc = "```rust")]
6#[cfg_attr(not(feature = "fmt"), doc = "```ignore")]
7/// use const_format::utils::str_eq;
8///
9/// const STRS: &[&str] = &[
10///     "foo",
11///     "fooooo",
12///     "bar",
13///     "baz",
14/// ];
15///
16/// const ARE_0_0_EQ: bool = str_eq(STRS[0], STRS[0]);
17/// const ARE_0_1_EQ: bool = str_eq(STRS[0], STRS[1]);
18///
19/// const ARE_1_1_EQ: bool = str_eq(STRS[1], STRS[1]);
20/// const ARE_1_2_EQ: bool = str_eq(STRS[1], STRS[2]);
21///
22/// const ARE_2_2_EQ: bool = str_eq(STRS[2], STRS[2]);
23/// const ARE_2_3_EQ: bool = str_eq(STRS[2], STRS[3]);
24///
25/// assert!(  ARE_0_0_EQ );
26/// assert!( !ARE_0_1_EQ );
27///
28/// assert!(  ARE_1_1_EQ );
29/// assert!( !ARE_1_2_EQ );
30///
31/// assert!(  ARE_2_2_EQ );
32/// assert!( !ARE_2_3_EQ );
33///
34/// ```
35///
36pub const fn str_eq(left: &str, right: &str) -> bool {
37    u8_slice_eq(left.as_bytes(), right.as_bytes())
38}
39
40/// A const equivalent of `&[u8]` equality comparison.
41///
42/// # Example
43///
44#[cfg_attr(feature = "fmt", doc = "```rust")]
45#[cfg_attr(not(feature = "fmt"), doc = "```ignore")]
46/// use const_format::utils::u8_slice_eq;
47///
48/// const SLICES: &[&[u8]] = &[
49///     &[10, 20],
50///     &[10, 20, 30, 40],
51///     &[3, 5, 8, 13],
52///     &[4, 9, 16, 25],
53/// ];
54///
55/// const ARE_0_0_EQ: bool = u8_slice_eq(SLICES[0], SLICES[0]);
56/// const ARE_0_1_EQ: bool = u8_slice_eq(SLICES[0], SLICES[1]);
57///
58/// const ARE_1_1_EQ: bool = u8_slice_eq(SLICES[1], SLICES[1]);
59/// const ARE_1_2_EQ: bool = u8_slice_eq(SLICES[1], SLICES[2]);
60///
61/// const ARE_2_2_EQ: bool = u8_slice_eq(SLICES[2], SLICES[2]);
62/// const ARE_2_3_EQ: bool = u8_slice_eq(SLICES[2], SLICES[3]);
63///
64/// assert!(  ARE_0_0_EQ );
65/// assert!( !ARE_0_1_EQ );
66///
67/// assert!(  ARE_1_1_EQ );
68/// assert!( !ARE_1_2_EQ );
69///
70/// assert!(  ARE_2_2_EQ );
71/// assert!( !ARE_2_3_EQ );
72///
73/// ```
74///
75pub const fn u8_slice_eq(left: &[u8], right: &[u8]) -> bool {
76    if left.len() != right.len() {
77        return false;
78    }
79
80    let mut i = 0;
81    while i != left.len() {
82        if left[i] != right[i] {
83            return false;
84        }
85        i += 1;
86    }
87
88    true
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn slice_eq_test() {
97        assert!(u8_slice_eq(&[], &[]));
98        assert!(!u8_slice_eq(&[], &[0]));
99        assert!(!u8_slice_eq(&[0], &[]));
100        assert!(u8_slice_eq(&[0], &[0]));
101        assert!(!u8_slice_eq(&[0], &[1]));
102        assert!(!u8_slice_eq(&[1], &[0]));
103        assert!(!u8_slice_eq(&[0], &[0, 1]));
104        assert!(!u8_slice_eq(&[0, 1], &[0]));
105        assert!(u8_slice_eq(&[0, 1], &[0, 1]));
106        assert!(!u8_slice_eq(&[0, 1], &[0, 2]));
107    }
108
109    #[test]
110    fn str_eq_test() {
111        assert!(str_eq("", ""));
112        assert!(!str_eq("", "0"));
113        assert!(!str_eq("0", ""));
114        assert!(str_eq("0", "0"));
115        assert!(!str_eq("0", "1"));
116        assert!(!str_eq("1", "0"));
117        assert!(!str_eq("0", "0, 1"));
118        assert!(!str_eq("0, 1", "0"));
119        assert!(!str_eq("0, 1", "1"));
120        assert!(str_eq("0, 1", "0, 1"));
121        assert!(!str_eq("0, 1", "0, 2"));
122    }
123}