3 releases (stable)

Uses old Rust 2015

1.0.1 Feb 24, 2023
0.1.0 Nov 6, 2018

#25 in Filesystem

Download history 47507/week @ 2023-12-13 41108/week @ 2023-12-20 25170/week @ 2023-12-27 44711/week @ 2024-01-03 43739/week @ 2024-01-10 62669/week @ 2024-01-17 55450/week @ 2024-01-24 60416/week @ 2024-01-31 62736/week @ 2024-02-07 43831/week @ 2024-02-14 46756/week @ 2024-02-21 50315/week @ 2024-02-28 55063/week @ 2024-03-06 59984/week @ 2024-03-13 50935/week @ 2024-03-20 53260/week @ 2024-03-27

226,477 downloads per month
Used in 380 crates (112 directly)

MIT/Apache

10KB
145 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/.."), PathBuf::from("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