path-clean-1.0.1/.cargo_vcs_info.json0000644000000001360000000000100130470ustar { "git": { "sha1": "d8948ae69d349ec33dfe6d6b9c6a0fe30288a117" }, "path_in_vcs": "" }path-clean-1.0.1/.github/workflows/ci.yml000075500000000000000000000025410072674642500164070ustar 00000000000000name: CI on: push: branches: - master pull_request: schedule: - cron: "0 9 7 * *" # Run on at 9am on the 7th of every month env: RUSTFLAGS: -Dwarnings jobs: build_and_test: name: Build and test runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, windows-latest, macOS-latest] rust: [stable, nightly] steps: - uses: actions/checkout@master - name: Install ${{ matrix.rust }} uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.rust }} override: true - name: check uses: actions-rs/cargo@v1 with: command: check args: --all --bins --examples - name: tests uses: actions-rs/cargo@v1 with: command: test args: --all check_fmt_and_docs: name: Checking fmt, clippy, and docs runs-on: ubuntu-latest steps: - uses: actions/checkout@master - uses: actions-rs/toolchain@v1 with: toolchain: stable override: true - name: setup run: | rustup component add clippy rustfmt rustc --version - name: clippy run: cargo clippy --tests --examples -- -D warnings - name: fmt run: cargo fmt --all -- --check - name: Docs run: cargo doc --no-deps path-clean-1.0.1/.gitignore000075500000000000000000000000360072674642500136610ustar 00000000000000/target **/*.rs.bk Cargo.lock path-clean-1.0.1/Cargo.toml0000644000000016530000000000100110520ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "path-clean" version = "1.0.1" authors = ["Dan Reeves "] exclude = ["scripts/*"] description = "A Rust implementation of cleanname or path.Clean" documentation = "https://docs.rs/path-clean" readme = "README.md" keywords = ["path", "clean"] license = "MIT OR Apache-2.0" repository = "https://github.com/danreeves/path-clean" [[bench]] name = "clean_benchmark" harness = false [dependencies] [dev-dependencies.criterion] version = "0.2" path-clean-1.0.1/Cargo.toml.orig000075500000000000000000000007210072674642500145610ustar 00000000000000[package] name = "path-clean" description = "A Rust implementation of cleanname or path.Clean" version = "1.0.1" authors = ["Dan Reeves "] repository = "https://github.com/danreeves/path-clean" documentation = "https://docs.rs/path-clean" readme = "README.md" license = "MIT OR Apache-2.0" keywords = ["path", "clean"] exclude = ["scripts/*"] [dependencies] [dev-dependencies] criterion = "0.2" [[bench]] name = "clean_benchmark" harness = false path-clean-1.0.1/LICENSE-APACHE000075500000000000000000000010470072674642500136200ustar 00000000000000Copyright 2018 Dan Reeves Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. path-clean-1.0.1/LICENSE-MIT000075500000000000000000000017230072674642500133310ustar 00000000000000Copyright (c) 2018 Dan Reeves Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN path-clean-1.0.1/README.md000075500000000000000000000037140072674642500131560ustar 00000000000000# path-clean [![crates.io version][1]][2] [![build status][3]][4] [![docs.rs docs][5]][6] [![license][7]][8] ## Installation ```sh cargo add path-clean ``` ## Usage ```rust 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`](https://golang.org/pkg/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"](https://9p.io/sys/doc/lexnames.html). For convenience, the [`PathClean`] trait is exposed and comes implemented for [`std::path::PathBuf`]. ## License [MIT](./LICENSE-MIT) OR [Apache-2.0](./LICENSE-APACHE) [1]: https://img.shields.io/crates/v/path-clean.svg?style=flat-square [2]: https://crates.io/crates/path-clean [3]: https://img.shields.io/github/actions/workflow/status/danreeves/path-clean/ci.yml?style=flat-square [4]: https://github.com/danreeves/path-clean/actions [5]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square [6]: https://docs.rs/path-clean [7]: https://img.shields.io/crates/l/path-clean.svg?style=flat-square [8]: #license path-clean-1.0.1/benches/clean_benchmark.rs000075500000000000000000000005460072674642500167500ustar 00000000000000#[macro_use] extern crate criterion; extern crate path_clean; use criterion::black_box; use criterion::Criterion; use path_clean::clean; fn clean_benchmark(c: &mut Criterion) { c.bench_function("clean", |b| { b.iter(|| clean(black_box("abc/../../././../def"))) }); } criterion_group!(benches, clean_benchmark); criterion_main!(benches); path-clean-1.0.1/src/lib.rs000075500000000000000000000145340072674642500136040ustar 00000000000000//! `path-clean` is a Rust port of the the `cleanname` procedure from the Plan 9 C library, and is similar to //! [`path.Clean`](https://golang.org/pkg/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"](https://9p.io/sys/doc/lexnames.html). //! //! For convenience, the [`PathClean`] trait is exposed and comes implemented for [`std::path::{Path, PathBuf}`]. //! //! ```rust //! 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") //! ); //! ``` #![forbid(unsafe_code)] use std::path::{Component, Path, PathBuf}; /// The Clean trait implements a `clean` method. pub trait PathClean { fn clean(&self) -> PathBuf; } /// PathClean implemented for `Path` impl PathClean for Path { fn clean(&self) -> PathBuf { clean(self) } } /// PathClean implemented for `PathBuf` impl PathClean for PathBuf { fn clean(&self) -> PathBuf { clean(self) } } /// The core implementation. It performs the following, lexically: /// 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. pub fn clean

(path: P) -> PathBuf where P: AsRef, { let mut out = Vec::new(); for comp in path.as_ref().components() { match comp { Component::CurDir => (), Component::ParentDir => match out.last() { Some(Component::RootDir) => (), Some(Component::Normal(_)) => { out.pop(); } None | Some(Component::CurDir) | Some(Component::ParentDir) | Some(Component::Prefix(_)) => out.push(comp), }, comp => out.push(comp), } } if !out.is_empty() { out.iter().collect() } else { PathBuf::from(".") } } #[cfg(test)] mod tests { use super::{clean, PathClean}; use std::path::{Path, PathBuf}; #[test] fn test_empty_path_is_current_dir() { assert_eq!(clean(""), PathBuf::from(".")); } #[test] fn test_clean_paths_dont_change() { let tests = vec![(".", "."), ("..", ".."), ("/", "/")]; for test in tests { assert_eq!(clean(test.0), PathBuf::from(test.1)); } } #[test] fn test_replace_multiple_slashes() { let tests = vec![ ("/", "/"), ("//", "/"), ("///", "/"), (".//", "."), ("//..", "/"), ("..//", ".."), ("/..//", "/"), ("/.//./", "/"), ("././/./", "."), ("path//to///thing", "path/to/thing"), ("/path//to///thing", "/path/to/thing"), ]; for test in tests { assert_eq!(clean(test.0), PathBuf::from(test.1)); } } #[test] fn test_eliminate_current_dir() { let tests = vec![ ("./", "."), ("/./", "/"), ("./test", "test"), ("./test/./path", "test/path"), ("/test/./path/", "/test/path"), ("test/path/.", "test/path"), ]; for test in tests { assert_eq!(clean(test.0), PathBuf::from(test.1)); } } #[test] fn test_eliminate_parent_dir() { let tests = vec![ ("/..", "/"), ("/../test", "/test"), ("test/..", "."), ("test/path/..", "test"), ("test/../path", "path"), ("/test/../path", "/path"), ("test/path/../../", "."), ("test/path/../../..", ".."), ("/test/path/../../..", "/"), ("/test/path/../../../..", "/"), ("test/path/../../../..", "../.."), ("test/path/../../another/path", "another/path"), ("test/path/../../another/path/..", "another"), ("../test", "../test"), ("../test/", "../test"), ("../test/path", "../test/path"), ("../test/..", ".."), ]; for test in tests { assert_eq!(clean(test.0), PathBuf::from(test.1)); } } #[test] fn test_pathbuf_trait() { assert_eq!( PathBuf::from("/test/../path/").clean(), PathBuf::from("/path") ); } #[test] fn test_path_trait() { assert_eq!(Path::new("/test/../path/").clean(), PathBuf::from("/path")); } #[test] #[cfg(target_os = "windows")] fn test_windows_paths() { let tests = vec![ ("\\..", "\\"), ("\\..\\test", "\\test"), ("test\\..", "."), ("test\\path\\..\\..\\..", ".."), ("test\\path/..\\../another\\path", "another\\path"), // Mixed ("test\\path\\my/path", "test\\path\\my\\path"), // Mixed 2 ("/dir\\../otherDir/test.json", "/otherDir/test.json"), // User example ("c:\\test\\..", "c:\\"), // issue #12 ("c:/test/..", "c:/"), // issue #12 ]; for test in tests { assert_eq!(clean(test.0), PathBuf::from(test.1)); } } }