-
Notifications
You must be signed in to change notification settings - Fork 366
/
slashes.rs
65 lines (59 loc) · 1.57 KB
/
slashes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pub(crate) enum Slashes {
Unix,
Windows,
}
impl Slashes {
pub fn here() -> Slashes {
if cfg!(windows) {
Slashes::Windows
} else if cfg!(unix) {
Slashes::Unix
} else {
panic!("Unknown operating system")
}
}
fn slashes(&self) -> &'static [char] {
match self {
Slashes::Unix => &['/'],
Slashes::Windows => &['/', '\\'],
}
}
fn _is_slash(&self, c: char) -> bool {
self.slashes().contains(&c)
}
pub fn norm_path(&self, path: &str) -> String {
match self {
Slashes::Unix => path.to_owned(),
Slashes::Windows => path.replace('\\', "/"),
}
}
fn remove_dot_slash<'a>(&self, path: &'a str) -> &'a str {
if path == "." {
""
} else if path.starts_with(".") {
let mut temp_path = &path[1..];
let mut at_least_one_slash = false;
while temp_path.starts_with(self.slashes()) {
temp_path = &temp_path[1..];
at_least_one_slash = true;
}
if at_least_one_slash {
temp_path
} else {
path
}
} else {
path
}
}
/// Remove leading ./ from path
pub fn remove_dot_slashes<'a>(&self, mut path: &'a str) -> &'a str {
loop {
let new_path = self.remove_dot_slash(path);
if new_path == path {
return new_path;
}
path = new_path;
}
}
}