use std::path::Path;
pub fn sanitize_filename(name: &str) -> String {
let mut result = String::new();
for c in name.chars() {
match c {
'/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => {
result.push('_');
}
'\0' => {
}
_ => {
result.push(c);
}
}
}
result.trim().trim_matches('.').to_string()
}
pub fn sanitize_path(path: &str) -> String {
path.replace("..", "").replace('\0', "")
}
pub fn escape_html(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
.replace('\'', "'")
}
pub fn unescape_html(s: &str) -> String {
s.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace(""", "\"")
.replace("'", "'")
}
pub fn escape_shell_arg(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
pub fn truncate(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else if max_len > 3 {
format!("{}...", &s[..max_len - 3])
} else {
s[..max_len].to_string()
}
}
pub fn normalize_whitespace(s: &str) -> String {
let mut result = String::new();
let mut last_was_space = false;
for c in s.chars() {
if c.is_whitespace() {
if !last_was_space {
result.push(' ');
last_was_space = true;
}
} else {
result.push(c);
last_was_space = false;
}
}
result.trim().to_string()
}
pub fn is_ascii(s: &str) -> bool {
s.chars().all(|c| c.is_ascii())
}