function to test for valid names of things

This commit is contained in:
Bruce Leidl 2019-01-29 11:42:27 -05:00
parent f5bf92f488
commit e4665f3f5c

View File

@ -12,6 +12,28 @@ use failure::ResultExt;
use crate::Result; use crate::Result;
pub fn is_valid_name(name: &str, maxsize: usize) -> bool {
name.len() <= maxsize &&
// Also false on empty string
is_first_char_alphabetic(name) &&
name.chars().all(is_alphanum_or_dash)
}
fn is_alphanum_or_dash(c: char) -> bool {
is_ascii(c) && (c.is_alphanumeric() || c == '-')
}
fn is_ascii(c: char) -> bool {
c as u32 <= 0x7F
}
pub fn is_first_char_alphabetic(s: &str) -> bool {
if let Some(c) = s.chars().next() {
return is_ascii(c) && c.is_alphabetic()
}
false
}
fn search_path(filename: &str) -> Result<PathBuf> { fn search_path(filename: &str) -> Result<PathBuf> {
let path_var = env::var("PATH")?; let path_var = env::var("PATH")?;
for mut path in env::split_paths(&path_var) { for mut path in env::split_paths(&path_var) {