From 1c3417ce37068936ebcb907bf125f66434d0ced2 Mon Sep 17 00:00:00 2001 From: Evan Carroll Date: Wed, 18 May 2022 01:14:28 -0500 Subject: [PATCH] add file_suffix method to std::path --- library/std/src/path.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 36d6469c02d31..ae9bb81b2ac65 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -363,7 +363,7 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) { None => return (file, None), }; let before = &slice[..i]; - let after = &slice[i + 1..]; + let after = &slice[i..]; unsafe { (u8_slice_as_os_str(before), Some(u8_slice_as_os_str(after))) } } @@ -2392,6 +2392,8 @@ impl Path { /// # See Also /// This method is similar to [`Path::file_stem`], which extracts the portion of the file name /// before the *last* `.` + /// This method is the prefix-analog of [`Path::file_suffix`], which extracts the portion of the file name + /// before the *first* `.` /// /// [`Path::file_stem`]: Path::file_stem /// @@ -2401,6 +2403,42 @@ impl Path { self.file_name().map(split_file_at_dot).and_then(|(before, _after)| Some(before)) } + /// Extracts the suffix of [`self.file_name`]. + /// + /// The suffix is: + /// + /// * [`None`], if there is no file name; + /// * [`None`] if there is no embedded `.`; + /// * The portion of the file name after and including the first non-beginning `.`; + /// * [`None`] if the file name begins with `.` and has no other `.`s within; + /// * The portion of the file name after the second `.` if the file name begins with `.` + /// + /// [`self.file_name`]: Path::file_name + /// + /// # Examples + /// + /// ``` + /// # #![feature(path_file_suffix)] + /// use std::path::Path; + /// + /// assert_eq!(".rs", Path::new("foo.rs").file_suffix().unwrap()); + /// assert_eq!(".tar.gz", Path::new("foo.tar.gz").file_suffix().unwrap()); + /// ``` + /// + /// # See Also + /// This method is similar to [`Path::extension`], which extracts the portion of the file name + /// after the *last* `.` + /// This method is the suffix-analog of [`Path::file_prefix`], which extracts the portion of the file name + /// before the *first* `.` + /// + /// [`Path::extension`]: Path::extension + /// + #[unstable(feature = "path_file_suffix", issue = "86319")] + #[must_use] + pub fn file_suffix(&self) -> Option<&OsStr> { + self.file_name().map(split_file_at_dot).and_then(|(_before, after)| after) + } + /// Extracts the extension of [`self.file_name`], if possible. /// /// The extension is: