diff options
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index 72db4518..8a8c04b6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,6 +2,7 @@ use std::cmp::Ordering; use std::ops::Range; +use std::path::{Component, Path, PathBuf}; /// Additional methods for slices. pub trait SliceExt<T> { @@ -79,3 +80,28 @@ impl RangeExt for Range<usize> { } } } + +/// Additional methods for [`Path`]. +pub trait PathExt { + /// Lexically normalize a path. + fn normalize(&self) -> PathBuf; +} + +impl PathExt for Path { + fn normalize(&self) -> PathBuf { + let mut out = PathBuf::new(); + for component in self.components() { + match component { + Component::CurDir => {} + Component::ParentDir => match out.components().next_back() { + Some(Component::Normal(_)) => { + out.pop(); + } + _ => out.push(component), + }, + _ => out.push(component), + } + } + out + } +} |
