mirror of
https://git.proxmox.com/git/proxmox
synced 2025-07-15 09:50:52 +00:00
schema: add const-fn helper to compare strings
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
e3286d3758
commit
59a9ddbf06
25
proxmox-schema/src/const_test_utils.rs
Normal file
25
proxmox-schema/src/const_test_utils.rs
Normal 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
|
||||||
|
}
|
@ -31,3 +31,5 @@ pub mod upid;
|
|||||||
|
|
||||||
#[cfg(feature = "api-types")]
|
#[cfg(feature = "api-types")]
|
||||||
pub mod api_types;
|
pub mod api_types;
|
||||||
|
|
||||||
|
pub(crate) mod const_test_utils;
|
||||||
|
Loading…
Reference in New Issue
Block a user