#path #clean

path-clean

A Rust implementation of cleanname or path.Clean

1 unstable release

Uses old Rust 2015

0.1.0 Nov 6, 2018

#207 in Filesystem

Download history 3101/week @ 2021-05-30 3444/week @ 2021-06-06 2824/week @ 2021-06-13 3629/week @ 2021-06-20 3505/week @ 2021-06-27 5284/week @ 2021-07-04 3762/week @ 2021-07-11 5483/week @ 2021-07-18 4962/week @ 2021-07-25 6028/week @ 2021-08-01 7308/week @ 2021-08-08 7131/week @ 2021-08-15 8253/week @ 2021-08-22 7905/week @ 2021-08-29 7214/week @ 2021-09-05 8492/week @ 2021-09-12

15,187 downloads per month
Used in 72 crates (31 directly)

MIT/Apache

10KB
142 lines

path-clean

crates.io version build status docs.rs docs license

Installation

cargo add path-clean

Usage

use std::path::PathBuf;
use path_clean::{clean, PathClean};
assert_eq!(clean("hello/world/.."), "hello");
assert_eq!(
  PathBuf::from("/test/../path/").clean(),
  PathBuf::from("/path")
);

About

path-clean is a Rust port of the the cleanname procedure from the Plan 9 C library, and is similar to path.Clean from the Go standard library. It works as follows:

  1. Reduce multiple slashes to a single slash.
  2. Eliminate . path name elements (the current directory).
  3. Eliminate .. path name elements (the parent directory) and the non-. non-.., element that precedes them.
  4. Eliminate .. elements that begin a rooted path, that is, replace /.. by / at the beginning of a path.
  5. Leave intact .. elements that begin a non-rooted path.

If the result of this process is an empty string, return the string ".", representing the current directory.

It performs this transform 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".

For convenience, the [PathClean] trait is exposed and comes implemented for [std::path::PathBuf].

License

MIT OR Apache-2.0

No runtime deps