Skip to content

Commit

Permalink
Add RelativePath::is_normalized (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
DefinitelyNotRobot authored Oct 4, 2021
1 parent 41e6cae commit 7fd239b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
* Added `RelativePath::is_normalized` to check for normalization ([#28]).
* Added `impl From<&RelativePath> for Box<RelativePath>` ([#26]).
* Added `impl From<RelativePathBuf> for Box<RelativePath>` ([#26]).
* Added `impl From<&RelativePath> for Arc<RelativePath>` ([#26]).
* Added `impl From<RelativePathBuf> for Arc<RelativePath>` ([#26]).
* Added `impl From<&RelativePath> for Rc<RelativePath>` ([#26]).
* Added `impl From<RelativePathBuf> for Rc<RelativePath>` ([#26]).

## [1.5.0] - 2021-07-29

Expand Down
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,36 @@ impl RelativePath {
iter_after(self.components().rev(), child.as_ref().components().rev()).is_some()
}

/// Determines whether `self` is normalized.
///
/// # Examples
///
/// ```
/// use relative_path::RelativePath;
///
/// // These are normalized.
/// assert!(RelativePath::new("").is_normalized());
/// assert!(RelativePath::new("baz.txt").is_normalized());
/// assert!(RelativePath::new("foo/bar/baz.txt").is_normalized());
/// assert!(RelativePath::new("..").is_normalized());
/// assert!(RelativePath::new("../..").is_normalized());
/// assert!(RelativePath::new("../../foo/bar/baz.txt").is_normalized());
///
/// // These are not normalized.
/// assert!(!RelativePath::new(".").is_normalized());
/// assert!(!RelativePath::new("./baz.txt").is_normalized());
/// assert!(!RelativePath::new("foo/..").is_normalized());
/// assert!(!RelativePath::new("foo/../baz.txt").is_normalized());
/// assert!(!RelativePath::new("foo/.").is_normalized());
/// assert!(!RelativePath::new("foo/./baz.txt").is_normalized());
/// assert!(!RelativePath::new("../foo/./bar/../baz.txt").is_normalized());
/// ```
pub fn is_normalized(&self) -> bool {
self.components()
.skip_while(|c| matches!(c, Component::ParentDir))
.all(|c| matches!(c, Component::Normal(_)))
}

/// Creates an owned [RelativePathBuf] like `self` but with the given file name.
///
/// See [set_file_name] for more details.
Expand Down

0 comments on commit 7fd239b

Please sign in to comment.