2022-02-12 10:29:38 -05:00
|
|
|
//! Common utility functions
|
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
/// Seconds since 1970.
|
2023-01-22 10:49:49 -05:00
|
|
|
#[must_use] pub fn unix_time() -> u64 {
|
2022-02-12 10:29:38 -05:00
|
|
|
SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.map(|x| x.as_secs())
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a string contains only hex characters.
|
2023-01-22 10:49:49 -05:00
|
|
|
#[must_use] pub fn is_hex(s: &str) -> bool {
|
2022-02-12 10:29:38 -05:00
|
|
|
s.chars().all(|x| char::is_ascii_hexdigit(&x))
|
|
|
|
}
|
2022-08-17 19:34:11 -04:00
|
|
|
|
|
|
|
/// Check if a string contains only lower-case hex chars.
|
2023-01-22 10:49:49 -05:00
|
|
|
#[must_use] pub fn is_lower_hex(s: &str) -> bool {
|
2022-08-17 19:34:11 -04:00
|
|
|
s.chars().all(|x| {
|
|
|
|
(char::is_ascii_lowercase(&x) || char::is_ascii_digit(&x)) && char::is_ascii_hexdigit(&x)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lower_hex() {
|
|
|
|
let hexstr = "abcd0123";
|
|
|
|
assert_eq!(is_lower_hex(hexstr), true);
|
|
|
|
}
|
|
|
|
}
|