You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
43 lines
1.0 KiB
pub fn normalize_uuid(u: &str) -> String {
|
|
let s = u.trim().to_ascii_lowercase();
|
|
const BASE_SUFFIX: &str = "-0000-1000-8000-00805f9b34fb";
|
|
if s.ends_with(BASE_SUFFIX) && s.len() >= BASE_SUFFIX.len() + 4 {
|
|
let start = s.len() - BASE_SUFFIX.len() - 4;
|
|
return s[start..start + 4].to_string();
|
|
}
|
|
s
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_strips_standard_base_suffix() {
|
|
assert_eq!(normalize_uuid("00001816-0000-1000-8000-00805f9b34fb"), "1816");
|
|
}
|
|
|
|
#[test]
|
|
fn test_short_uuid_is_unchanged() {
|
|
assert_eq!(normalize_uuid("1816"), "1816");
|
|
}
|
|
|
|
#[test]
|
|
fn test_trims_whitespace() {
|
|
assert_eq!(normalize_uuid(" 1816 "), "1816");
|
|
}
|
|
|
|
#[test]
|
|
fn test_lowercases_input() {
|
|
assert_eq!(normalize_uuid("181D"), "181d");
|
|
}
|
|
|
|
#[test]
|
|
fn test_custom_128bit_uuid_is_left_untouched() {
|
|
assert_eq!(
|
|
normalize_uuid("f0001816-0451-4000-b000-000000000000"),
|
|
"f0001816-0451-4000-b000-000000000000"
|
|
);
|
|
}
|
|
}
|