30 lines
854 B
Rust
30 lines
854 B
Rust
use regex::Regex;
|
|
|
|
pub fn realpath(path: &str) -> String {
|
|
let components: Vec<&str> = Regex::new("/+")
|
|
.expect("Error parsing regex /+.")
|
|
.split(path)
|
|
.collect();
|
|
let mut abs_path: Vec<&str> = Vec::new();
|
|
for component in components {
|
|
if (Regex::new("^\\.$")
|
|
.expect("Error parsing regex ^\\.$")
|
|
.is_match(component))
|
|
|| (Regex::new("^$")
|
|
.expect("Error parsing regex ^$.")
|
|
.is_match(component))
|
|
{
|
|
continue;
|
|
} else if Regex::new("^\\.\\.$")
|
|
.expect("Error parsing regex ^\\.\\.$.")
|
|
.is_match(component)
|
|
{
|
|
abs_path.pop();
|
|
} else {
|
|
abs_path.push(component);
|
|
}
|
|
}
|
|
let abs_path: &str = &abs_path.join("/");
|
|
["/", abs_path].join("")
|
|
}
|