Skip to content

Commit

Permalink
Rollup merge of rust-lang#123600 - tisonkun:path_with_extension, r=dt…
Browse files Browse the repository at this point in the history
…olnay

impl PathBuf::add_extension and Path::with_added_extension

See the ACP for motivation and discussions - rust-lang/libs-team#368
  • Loading branch information
compiler-errors authored Jul 6, 2024
2 parents 625bcc4 + ba4c71a commit 1b5e5ac
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
94 changes: 94 additions & 0 deletions std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,74 @@ impl PathBuf {
true
}

/// Append [`self.extension`] with `extension`.
///
/// Returns `false` and does nothing if [`self.file_name`] is [`None`],
/// returns `true` and updates the extension otherwise.
///
/// # Caveats
///
/// The appended `extension` may contain dots and will be used in its entirety,
/// but only the part after the final dot will be reflected in
/// [`self.extension`].
///
/// See the examples below.
///
/// [`self.file_name`]: Path::file_name
/// [`self.extension`]: Path::extension
///
/// # Examples
///
/// ```
/// #![feature(path_add_extension)]
///
/// use std::path::{Path, PathBuf};
///
/// let mut p = PathBuf::from("/feel/the");
///
/// p.add_extension("formatted");
/// assert_eq!(Path::new("/feel/the.formatted"), p.as_path());
///
/// p.add_extension("dark.side");
/// assert_eq!(Path::new("/feel/the.formatted.dark.side"), p.as_path());
///
/// p.set_extension("cookie");
/// assert_eq!(Path::new("/feel/the.formatted.dark.cookie"), p.as_path());
///
/// p.set_extension("");
/// assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
///
/// p.add_extension("");
/// assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
/// ```
#[unstable(feature = "path_add_extension", issue = "127292")]
pub fn add_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
self._add_extension(extension.as_ref())
}

fn _add_extension(&mut self, extension: &OsStr) -> bool {
let file_name = match self.file_name() {
None => return false,
Some(f) => f.as_encoded_bytes(),
};

let new = extension;
if !new.is_empty() {
// truncate until right after the file name
// this is necessary for trimming the trailing slash
let end_file_name = file_name[file_name.len()..].as_ptr().addr();
let start = self.inner.as_encoded_bytes().as_ptr().addr();
self.inner.truncate(end_file_name.wrapping_sub(start));

// append the new extension
self.inner.reserve_exact(new.len() + 1);
self.inner.push(OsStr::new("."));
self.inner.push(new);
}

true
}

/// Yields a mutable reference to the underlying [`OsString`] instance.
///
/// # Examples
Expand Down Expand Up @@ -2656,6 +2724,32 @@ impl Path {
new_path
}

/// Creates an owned [`PathBuf`] like `self` but with the extension added.
///
/// See [`PathBuf::add_extension`] for more details.
///
/// # Examples
///
/// ```
/// #![feature(path_add_extension)]
///
/// use std::path::{Path, PathBuf};
///
/// let path = Path::new("foo.rs");
/// assert_eq!(path.with_added_extension("txt"), PathBuf::from("foo.rs.txt"));
///
/// let path = Path::new("foo.tar.gz");
/// assert_eq!(path.with_added_extension(""), PathBuf::from("foo.tar.gz"));
/// assert_eq!(path.with_added_extension("xz"), PathBuf::from("foo.tar.gz.xz"));
/// assert_eq!(path.with_added_extension("").with_added_extension("txt"), PathBuf::from("foo.tar.gz.txt"));
/// ```
#[unstable(feature = "path_add_extension", issue = "127292")]
pub fn with_added_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
let mut new_path = self.to_path_buf();
new_path.add_extension(extension);
new_path
}

/// Produces an iterator over the [`Component`]s of the path.
///
/// When parsing the path, there is a small amount of normalization:
Expand Down
74 changes: 74 additions & 0 deletions std/src/path/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,37 @@ pub fn test_set_extension() {
tfe!("/", "foo", "/", false);
}

#[test]
pub fn test_add_extension() {
macro_rules! tfe (
($path:expr, $ext:expr, $expected:expr, $output:expr) => ({
let mut p = PathBuf::from($path);
let output = p.add_extension($ext);
assert!(p.to_str() == Some($expected) && output == $output,
"adding extension of {:?} to {:?}: Expected {:?}/{:?}, got {:?}/{:?}",
$path, $ext, $expected, $output,
p.to_str().unwrap(), output);
});
);

tfe!("foo", "txt", "foo.txt", true);
tfe!("foo.bar", "txt", "foo.bar.txt", true);
tfe!("foo.bar.baz", "txt", "foo.bar.baz.txt", true);
tfe!(".test", "txt", ".test.txt", true);
tfe!("foo.txt", "", "foo.txt", true);
tfe!("foo", "", "foo", true);
tfe!("", "foo", "", false);
tfe!(".", "foo", ".", false);
tfe!("foo/", "bar", "foo.bar", true);
tfe!("foo/.", "bar", "foo.bar", true);
tfe!("..", "foo", "..", false);
tfe!("foo/..", "bar", "foo/..", false);
tfe!("/", "foo", "/", false);

// edge cases
tfe!("/foo.ext////", "bar", "/foo.ext.bar", true);
}

#[test]
pub fn test_with_extension() {
macro_rules! twe (
Expand Down Expand Up @@ -1441,6 +1472,49 @@ pub fn test_with_extension() {
twe!("ccc.bbb_bbb", "aaa_aaa_aaa", "ccc.aaa_aaa_aaa");
}

#[test]
pub fn test_with_added_extension() {
macro_rules! twe (
($input:expr, $extension:expr, $expected:expr) => ({
let input = Path::new($input);
let output = input.with_added_extension($extension);

assert!(
output.to_str() == Some($expected),
"calling Path::new({:?}).with_added_extension({:?}): Expected {:?}, got {:?}",
$input, $extension, $expected, output,
);
});
);

twe!("foo", "txt", "foo.txt");
twe!("foo.bar", "txt", "foo.bar.txt");
twe!("foo.bar.baz", "txt", "foo.bar.baz.txt");
twe!(".test", "txt", ".test.txt");
twe!("foo.txt", "", "foo.txt");
twe!("foo", "", "foo");
twe!("", "foo", "");
twe!(".", "foo", ".");
twe!("foo/", "bar", "foo.bar");
twe!("foo/.", "bar", "foo.bar");
twe!("..", "foo", "..");
twe!("foo/..", "bar", "foo/..");
twe!("/", "foo", "/");

// edge cases
twe!("/foo.ext////", "bar", "/foo.ext.bar");

// New extension is smaller than file name
twe!("aaa_aaa_aaa", "bbb_bbb", "aaa_aaa_aaa.bbb_bbb");
// New extension is greater than file name
twe!("bbb_bbb", "aaa_aaa_aaa", "bbb_bbb.aaa_aaa_aaa");

// New extension is smaller than previous extension
twe!("ccc.aaa_aaa_aaa", "bbb_bbb", "ccc.aaa_aaa_aaa.bbb_bbb");
// New extension is greater than previous extension
twe!("ccc.bbb_bbb", "aaa_aaa_aaa", "ccc.bbb_bbb.aaa_aaa_aaa");
}

#[test]
fn test_eq_receivers() {
use crate::borrow::Cow;
Expand Down

0 comments on commit 1b5e5ac

Please sign in to comment.