schema: add const-fn helper to compare strings

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2025-02-19 12:28:07 +01:00
parent e3286d3758
commit 59a9ddbf06
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,25 @@
/// Note: this only compares *bytes* and is not strictly speaking equivalent to str::cmp!
pub const fn byte_string_cmp(a: &[u8], b: &[u8]) -> std::cmp::Ordering {
use std::cmp::Ordering::*;
// const-version of `min(a.len(), b.len())` while simultaneously remembering
// `cmp(a.len(), b.len())`.
let (end, len_result) = if a.len() < b.len() {
(a.len(), Less)
} else if a.len() > b.len() {
(b.len(), Greater)
} else {
(a.len(), Equal)
};
let mut i = 0;
while i != end {
if a[i] < b[i] {
return Less;
} else if a[i] > b[i] {
return Greater;
}
i += 1;
}
len_result
}

View File

@ -31,3 +31,5 @@ pub mod upid;
#[cfg(feature = "api-types")]
pub mod api_types;
pub(crate) mod const_test_utils;