1use regex::Regex;
15
16use lazy_static::lazy_static;
17
18lazy_static! {
19 static ref RESERVED: Regex =
20 Regex::new("[<>:\"/\\\\|?*\u{0000}-\u{001F}\u{007F}\u{0080}-\u{009F}]+").unwrap();
21 static ref WINDOWS_RESERVED: Regex = Regex::new("^(con|prn|aux|nul|com\\d|lpt\\d)$").unwrap();
22 static ref OUTER_PERIODS: Regex = Regex::new("^\\.+|\\.+$").unwrap();
23}
24
25pub fn filenamify<S: AsRef<str>>(input: S) -> String {
32 let replacemant = "_";
33
34 let input = RESERVED.replace_all(input.as_ref(), replacemant);
35 let input = OUTER_PERIODS.replace_all(input.as_ref(), replacemant);
36
37 let mut result = input.into_owned();
38 if WINDOWS_RESERVED.is_match(result.as_str()) {
39 result.push_str(replacemant);
40 }
41
42 result
43}
44
45#[cfg(test)]
46mod tests {
47 use super::filenamify;
48
49 #[test]
50 fn test_filenamify() {
51 assert_eq!(filenamify("foo/bar"), "foo_bar");
52 assert_eq!(filenamify("foo//bar"), "foo_bar");
53 assert_eq!(filenamify("//foo//bar//"), "_foo_bar_");
54 assert_eq!(filenamify("foo\\bar"), "foo_bar");
55 assert_eq!(filenamify("foo\\\\\\bar"), "foo_bar");
56 assert_eq!(filenamify(r"foo\\bar"), "foo_bar");
57 assert_eq!(filenamify(r"foo\\\\\\bar"), "foo_bar");
58 assert_eq!(filenamify("////foo////bar////"), "_foo_bar_");
59 assert_eq!(filenamify("foo\u{0000}bar"), "foo_bar");
60 assert_eq!(filenamify("\"foo<>bar*"), "_foo_bar_");
61 assert_eq!(filenamify("."), "_");
62 assert_eq!(filenamify(".."), "_");
63 assert_eq!(filenamify("./"), "__");
64 assert_eq!(filenamify("../"), "__");
65 assert_eq!(filenamify("../../foo/bar"), "__.._foo_bar");
66 assert_eq!(filenamify("foo.bar."), "foo.bar_");
67 assert_eq!(filenamify("foo.bar.."), "foo.bar_");
68 assert_eq!(filenamify("foo.bar..."), "foo.bar_");
69 assert_eq!(filenamify("con"), "con_");
70 assert_eq!(filenamify("com1"), "com1_");
71 assert_eq!(filenamify(":nul|"), "_nul_");
72 assert_eq!(filenamify("foo/bar/nul"), "foo_bar_nul");
73 assert_eq!(filenamify("file:///file.tar.gz"), "file_file.tar.gz");
74 assert_eq!(filenamify("http://www.google.com"), "http_www.google.com");
75 assert_eq!(
76 filenamify("https://www.youtube.com/watch?v=dQw4w9WgXcQ"),
77 "https_www.youtube.com_watch_v=dQw4w9WgXcQ"
78 );
79 }
80}