From e4665f3f5c4503dd75a6c718447cc10489aebf91 Mon Sep 17 00:00:00 2001 From: Bruce Leidl Date: Tue, 29 Jan 2019 11:42:27 -0500 Subject: [PATCH] function to test for valid names of things --- libcitadel/src/util.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libcitadel/src/util.rs b/libcitadel/src/util.rs index 9518d1e..0b78efd 100644 --- a/libcitadel/src/util.rs +++ b/libcitadel/src/util.rs @@ -12,6 +12,28 @@ use failure::ResultExt; 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 { let path_var = env::var("PATH")?; for mut path in env::split_paths(&path_var) {