Implemented mining zone screen. Added a lot of user input checks.
This commit is contained in:
@@ -19,6 +19,38 @@ pub fn from_hex(string: &str) -> Result<Vec<u8>, num::ParseIntError> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn split_n(s: &str, n: usize) -> Vec<&str> {
|
||||
(0..=(s.len() - n + 1) / 2)
|
||||
.map(|i| &s[2 * i..2 * i + n])
|
||||
@@ -53,4 +85,24 @@ pub fn random_string(length: usize) -> String {
|
||||
result.push(c);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user