forked from libre-chip/fayalite
51 lines
1.2 KiB
Rust
51 lines
1.2 KiB
Rust
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
// See Notices.txt for copyright information
|
|
use std::cmp::Ordering;
|
|
|
|
pub const fn const_u8_cmp(a: u8, b: u8) -> Ordering {
|
|
if a < b {
|
|
Ordering::Less
|
|
} else if a > b {
|
|
Ordering::Greater
|
|
} else {
|
|
Ordering::Equal
|
|
}
|
|
}
|
|
|
|
pub const fn const_usize_cmp(a: usize, b: usize) -> Ordering {
|
|
if a < b {
|
|
Ordering::Less
|
|
} else if a > b {
|
|
Ordering::Greater
|
|
} else {
|
|
Ordering::Equal
|
|
}
|
|
}
|
|
|
|
pub const fn const_bytes_cmp(a: &[u8], b: &[u8]) -> Ordering {
|
|
let mut i = 0;
|
|
while i < a.len() && i < b.len() {
|
|
match const_u8_cmp(a[i], b[i]) {
|
|
Ordering::Equal => {}
|
|
retval => return retval,
|
|
}
|
|
i += 1;
|
|
}
|
|
const_usize_cmp(a.len(), b.len())
|
|
}
|
|
|
|
pub const fn const_str_cmp(a: &str, b: &str) -> Ordering {
|
|
const_bytes_cmp(a.as_bytes(), b.as_bytes())
|
|
}
|
|
|
|
pub const fn const_str_array_is_strictly_ascending(a: &[&str]) -> bool {
|
|
let mut i = 1;
|
|
while i < a.len() {
|
|
match const_str_cmp(a[i - 1], a[i]) {
|
|
Ordering::Less => {}
|
|
_ => return false,
|
|
}
|
|
i += 1;
|
|
}
|
|
true
|
|
}
|