2019-12-02 15:33:19 +00:00
|
|
|
use std::num;
|
2019-12-01 21:45:25 +00:00
|
|
|
|
|
|
|
/// Convert bytes array to HEX format
|
|
|
|
pub fn to_hex(buf: &[u8]) -> String {
|
|
|
|
let mut result = String::new();
|
|
|
|
for x in buf.iter() {
|
|
|
|
result.push_str(&format!("{:01$X}", x, 2));
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:33:19 +00:00
|
|
|
pub fn from_hex(string: &str) -> Result<Vec<u8>, num::ParseIntError> {
|
|
|
|
split_n(&string.trim()[..], 2)
|
|
|
|
.iter()
|
|
|
|
.map(|b| u8::from_str_radix(b, 16))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-02-22 20:45:32 +00:00
|
|
|
pub fn check_domain(name: &str, allow_dots: bool) -> bool {
|
|
|
|
if name.starts_with('.') || name.starts_with('-') || name.ends_with('.') || name.ends_with('-') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let mut last_dot = false;
|
|
|
|
let mut last_hyphen = false;
|
|
|
|
for char in name.chars() {
|
|
|
|
if allow_dots && char == '.' {
|
|
|
|
if last_dot {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
last_dot = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if char == '-' {
|
|
|
|
if last_hyphen {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
last_hyphen = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_dot = false;
|
|
|
|
last_hyphen = false;
|
|
|
|
if !char.is_alphanumeric() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:33:19 +00:00
|
|
|
fn split_n(s: &str, n: usize) -> Vec<&str> {
|
|
|
|
(0..=(s.len() - n + 1) / 2)
|
|
|
|
.map(|i| &s[2 * i..2 * i + n])
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-02-22 20:45:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::check_domain;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_domain() {
|
|
|
|
assert!(check_domain("abc0", false));
|
|
|
|
assert!(!check_domain("ab.c", false));
|
|
|
|
assert!(check_domain("a.b.c", true));
|
|
|
|
assert!(!check_domain("ab..c", true));
|
|
|
|
assert!(check_domain("a-b.c", true));
|
|
|
|
assert!(!check_domain("a--b.c", true));
|
|
|
|
assert!(check_domain("a-0-b.c", true));
|
|
|
|
assert!(!check_domain("-ab.c", true));
|
|
|
|
assert!(!check_domain("ab.c-", true));
|
|
|
|
assert!(!check_domain(".ab.c", true));
|
|
|
|
assert!(!check_domain("ab.c-", true));
|
|
|
|
}
|
2019-12-01 21:45:25 +00:00
|
|
|
}
|