1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
//! Utility functions
/// A const equivalent of `&str` equality comparison.
///
/// # Example
///
/// ```rust
/// use tstr::utils::str_eq;
///
/// const FOO: &str = "foo";
/// const BAR: &str = "fooooo";
/// const BAZ: &str = "bar";
///
///
/// const FOO_EQ_FOO: bool = str_eq(FOO, FOO);
/// assert!( FOO_EQ_FOO );
///
/// const FOO_EQ_BAR: bool = str_eq(FOO, BAR);
/// assert!( !FOO_EQ_BAR );
///
/// const FOO_EQ_BAZ: bool = str_eq(FOO, BAZ);
/// assert!( !FOO_EQ_BAZ );
///
/// ```
///
#[cfg(feature = "rust_1_46")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_46")))]
#[inline]
pub const fn str_eq(left: &str, right: &str) -> bool {
u8_slice_eq(left.as_bytes(), right.as_bytes())
}
/// A const equivalent of `&[u8]` equality comparison.
///
/// # Example
///
/// ```rust
/// use tstr::utils::u8_slice_eq;
///
/// const FOO: &[u8] = &[10, 20];
/// const BAR: &[u8] = &[10, 20, 30, 40];
/// const BAZ: &[u8] = &[3, 5, 8, 13];
///
/// const FOO_EQ_FOO: bool = u8_slice_eq(FOO, FOO);
/// assert!( FOO_EQ_FOO );
///
/// const FOO_EQ_BAR: bool = u8_slice_eq(FOO, BAR);
/// assert!( !FOO_EQ_BAR );
///
/// const FOO_EQ_BAZ: bool = u8_slice_eq(FOO, BAZ);
/// assert!( !FOO_EQ_BAZ );
///
///
/// ```
///
#[cfg(feature = "rust_1_46")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_46")))]
#[inline]
pub const fn u8_slice_eq(left: &[u8], right: &[u8]) -> bool {
if left.len() != right.len() {
return false;
}
let mut i = 0;
while i != left.len() {
if left[i] != right[i] {
return false;
}
i += 1;
}
true
}
#[cfg(feature = "rust_1_46")]
pub use slice_cmp::{str_cmp, u8_slice_cmp};
#[cfg(feature = "rust_1_46")]
mod slice_cmp {
use core::cmp::Ordering;
const LESS: u8 = 0;
const GREATER: u8 = 1;
const EQUAL: u8 = 2;
macro_rules! ret_if_ne {
($left:expr, $right:expr) => {{
let l = $left;
let r = $right;
if l != r {
return (l > r) as u8;
}
}};
}
const fn to_ordering(n: u8) -> Ordering {
match n {
LESS => Ordering::Less,
GREATER => Ordering::Greater,
_ => Ordering::Equal,
}
}
/// A const equivalent of `str::cmp`.
///
/// # Example
///
/// ```rust
/// use tstr::utils::str_cmp;
///
/// use std::cmp::Ordering;
///
/// const FOO: &str = "foo";
/// const BAR: &str = "fooooo";
/// const BAZ: &str = "bar";
///
///
/// const FOO_CMP_FOO: Ordering = str_cmp(FOO, FOO);
/// assert_eq!(FOO_CMP_FOO, Ordering::Equal);
///
/// const FOO_CMP_BAR: Ordering = str_cmp(FOO, BAR);
/// assert_eq!(FOO_CMP_BAR, Ordering::Less);
///
/// const FOO_CMP_BAZ: Ordering = str_cmp(FOO, BAZ);
/// assert_eq!(FOO_CMP_BAZ, Ordering::Greater);
///
/// ```
///
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_46")))]
#[inline]
pub const fn str_cmp(left: &str, right: &str) -> Ordering {
const fn str_cmp_inner(left: &[u8], right: &[u8]) -> u8 {
let left_len = left.len();
let right_len = right.len();
let (min_len, on_ne) = if left_len < right_len {
(left_len, LESS)
} else {
(right_len, GREATER)
};
let mut i = 0;
while i < min_len {
ret_if_ne! {left[i], right[i]}
i += 1;
}
if left_len == right_len {
EQUAL
} else {
on_ne
}
}
to_ordering(str_cmp_inner(left.as_bytes(), right.as_bytes()))
}
/// A const equivalent of `<[u8]>::cmp`.
///
/// # Example
///
/// ```rust
/// use tstr::utils::u8_slice_cmp;
///
/// use std::cmp::Ordering;
///
/// const FOO: &[u8] = &[10, 20];
/// const BAR: &[u8] = &[10, 20, 30, 40];
/// const BAZ: &[u8] = &[3, 5];
///
/// const FOO_CMP_FOO: Ordering = u8_slice_cmp(FOO, FOO);
/// assert_eq!(FOO_CMP_FOO, Ordering::Equal);
///
/// const FOO_CMP_BAR: Ordering = u8_slice_cmp(FOO, BAR);
/// assert_eq!(FOO_CMP_BAR, Ordering::Less);
///
/// const FOO_CMP_BAZ: Ordering = u8_slice_cmp(FOO, BAZ);
/// assert_eq!(FOO_CMP_BAZ, Ordering::Greater);
///
/// ```
///
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_46")))]
#[inline]
pub const fn u8_slice_cmp(left: &[u8], right: &[u8]) -> Ordering {
const fn u8_slice_cmp_inner(left: &[u8], right: &[u8]) -> u8 {
let left_len = left.len();
ret_if_ne! {left_len, right.len()}
let mut i = 0;
while i < left_len {
ret_if_ne! {left[i], right[i]}
i += 1;
}
EQUAL
}
to_ordering(u8_slice_cmp_inner(left, right))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(feature = "rust_1_46")]
fn slice_eq_test() {
assert!(u8_slice_eq(&[], &[]));
assert!(!u8_slice_eq(&[], &[0]));
assert!(!u8_slice_eq(&[0], &[]));
assert!(u8_slice_eq(&[0], &[0]));
assert!(!u8_slice_eq(&[0], &[1]));
assert!(!u8_slice_eq(&[1], &[0]));
assert!(!u8_slice_eq(&[0], &[0, 1]));
assert!(!u8_slice_eq(&[0, 1], &[0]));
assert!(u8_slice_eq(&[0, 1], &[0, 1]));
assert!(!u8_slice_eq(&[0, 1], &[0, 2]));
}
#[test]
#[cfg(feature = "rust_1_46")]
fn str_eq_test() {
assert!(str_eq("", ""));
assert!(!str_eq("", "0"));
assert!(!str_eq("0", ""));
assert!(str_eq("0", "0"));
assert!(!str_eq("0", "1"));
assert!(!str_eq("1", "0"));
assert!(!str_eq("0", "0, 1"));
assert!(!str_eq("0, 1", "0"));
assert!(!str_eq("0, 1", "1"));
assert!(str_eq("0, 1", "0, 1"));
assert!(!str_eq("0, 1", "0, 2"));
}
#[test]
#[cfg(feature = "rust_1_46")]
fn slice_cmp_test() {
use core::cmp::{
Ord,
Ordering::{Equal, Greater, Less},
};
macro_rules! assert_s_cmp {
($left:expr, $right:expr, $expected:expr) => {
assert_eq!(u8_slice_cmp($left, $right), $expected);
assert_eq!(<[u8]>::cmp($left, $right), $expected);
assert_eq!(u8_slice_cmp($right, $left), $expected.reverse());
assert_eq!(<[u8]>::cmp($right, $left), $expected.reverse());
};
}
assert_s_cmp!(&[], &[], Equal);
assert_s_cmp!(&[], &[0], Less);
assert_s_cmp!(&[0], &[], Greater);
assert_s_cmp!(&[0], &[0], Equal);
assert_s_cmp!(&[0], &[1], Less);
assert_s_cmp!(&[0], &[0, 1], Less);
assert_s_cmp!(&[0, 1], &[0, 1], Equal);
assert_s_cmp!(&[0, 1], &[0, 2], Less);
}
#[test]
#[cfg(feature = "rust_1_46")]
fn str_cmp_test() {
use core::cmp::{
Ord,
Ordering::{Equal, Greater, Less},
};
macro_rules! assert_s_cmp {
($left:expr, $right:expr, $expected:expr) => {
assert_eq!(str_cmp($left, $right), $expected, "A");
assert_eq!($left.cmp($right), $expected, "B");
assert_eq!(str_cmp($left, $left), Equal);
assert_eq!(str_cmp($right, $right), Equal);
assert_eq!(str_cmp($right, $left), $expected.reverse(), "cmp");
assert_eq!($right.cmp($left), $expected.reverse(), "cmp");
};
}
assert_s_cmp!("0", "", Greater);
assert_s_cmp!("0", "1", Less);
assert_s_cmp!("0", "01", Less);
assert_s_cmp!("1", "01", Greater);
assert_s_cmp!("099999", "12", Less);
assert_s_cmp!("111111", "12", Less);
assert_s_cmp!("120", "12", Greater);
assert_s_cmp!("199999", "12", Greater);
assert_s_cmp!("299999", "12", Greater);
assert_s_cmp!("01", "02", Less);
}
}