Debian Codegraph @

src/utils.rs

raw symbols SCIP
1 /// Returns a cross-platform-filename-safe version of any string.
2 ///
3 /// This is used internally to generate app data directories based on app
4 /// name/author. App developers can use it for consistency when dealing with
5 /// file system operations.
6 ///
7 /// Do not apply this function to full paths, as it will sanitize '/' and '\';
8 /// it should only be used on directory or file names (i.e. path segments).
9 pub fn sanitized(component: &str) -> String {
10 let mut buf = String::with_capacity(component.len());
11 for (i, c) in component.chars().enumerate() {
12 let is_lower = ('a'..='z').contains(&c);
13 let is_upper = ('A'..='Z').contains(&c);
14 let is_letter = is_upper || is_lower;
15 let is_number = ('0'..='9').contains(&c);
16 let is_space = c == ' ';
17 let is_hyphen = c == '-';
18 let is_underscore = c == '_';
19 let is_period = c == '.' && i != 0; // Disallow accidentally hidden folders
20 let is_valid =
21 is_letter || is_number || is_space || is_hyphen || is_underscore || is_period;
22 if is_valid {
23 buf.push(c);
24 } else {
25 buf.push_str(&format!(",{},", c as u32));
26 }
27 }
28 buf
29 }