mirror of
https://github.com/denoland/deno.git
synced 2024-10-31 09:14:20 -04:00
10e4b2e140
Yearly tradition of creating extra noise in git.
39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use std::path::Component;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
/// Normalize all intermediate components of the path (ie. remove "./" and "../" components).
|
|
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
|
|
///
|
|
/// Taken from Cargo
|
|
/// <https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85>
|
|
#[inline]
|
|
pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
|
|
let mut components = path.as_ref().components().peekable();
|
|
let mut ret =
|
|
if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
|
components.next();
|
|
PathBuf::from(c.as_os_str())
|
|
} else {
|
|
PathBuf::new()
|
|
};
|
|
|
|
for component in components {
|
|
match component {
|
|
Component::Prefix(..) => unreachable!(),
|
|
Component::RootDir => {
|
|
ret.push(component.as_os_str());
|
|
}
|
|
Component::CurDir => {}
|
|
Component::ParentDir => {
|
|
ret.pop();
|
|
}
|
|
Component::Normal(c) => {
|
|
ret.push(c);
|
|
}
|
|
}
|
|
}
|
|
ret
|
|
}
|