Crates.io | clean-path |
lib.rs | clean-path |
version | 0.2.1 |
source | src |
created_at | 2022-05-15 21:58:16.156758 |
updated_at | 2022-06-02 19:34:53.177334 |
description | A safe fork of the `path-clean` crate |
homepage | |
repository | https://gitlab.com/foo-jin/clean-path |
max_upload_size | |
id | 587358 |
size | 24,603 |
clean-path
is a safe fork of the
path-clean
crate.
cargo add clean-path
use std::path::{Path, PathBuf};
use clean_path::{clean, Clean};
assert_eq!(clean("foo/../../bar"), PathBuf::from("../bar"));
assert_eq!(Path::new("hello/world/..").clean(), PathBuf::from("hello"));
assert_eq!(
PathBuf::from("/test/../path/").clean(),
PathBuf::from("/path")
);
This fork aims to provide the same utility as
path-clean
, without using unsafe. Additionally, the api
is improved ([clean
] takes AsRef<Path>
instead of just &str
) and Clean
is implemented on
Path
in addition to just PathBuf
.
The main cleaning procedure is implemented using the methods provided by PathBuf
, thus it should
bring portability benefits over path-clean
w.r.t. correctly
handling cross-platform filepaths.
Additionally, the original implementation in path-clean
is
rather inscrutible, and as such if being able to inspect and understand the code is important to
you, this crate provides a more readable implementation.
However, the current implementation is not highly-optimized, so if performance is top-priority,
consider using path-clean
instead.
The cleaning works as follows:
.
path name elements (the current directory)...
path name elements (the parent directory) and the non-.
non-..
, element that precedes them...
elements that begin a rooted path, that is, replace /..
by /
at the beginning of a path...
elements that begin a non-rooted path.If the result of this process is an empty string, return the
string "."
, representing the current directory.
This transformation is performed lexically, without touching the filesystem. Therefore it doesn't do any symlink resolution or absolute path resolution. For more information you can see "Getting Dot-Dot Right".
This functionality is exposed in the [clean
] function and [Clean
] trait implemented for
[std::path::PathBuf
] and [std::path::Path
].
MIT OR Apache-2.0