-
Notifications
You must be signed in to change notification settings - Fork 13.8k
add file_prefix method to std::path #85166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
3e2206a
b1d84f7
7465192
a889529
fcb1ebf
1e759be
51cf318
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,10 +334,10 @@ fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) { | |
} | ||
} | ||
|
||
fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) { | ||
fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) { | ||
let slice = os_str_as_u8_slice(file); | ||
if slice == b".." { | ||
return (Some(file), None); | ||
return (file, None); | ||
} | ||
|
||
// The unsafety here stems from converting between &OsStr and &[u8] | ||
|
@@ -346,11 +346,11 @@ fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) { | |
// only from ASCII-bounded slices of existing &OsStr values. | ||
let i = match slice[1..].iter().position(|b| *b == b'.') { | ||
Some(i) => i + 1, | ||
None => return (Some(file), None), | ||
None => return (file, None), | ||
}; | ||
let before = &slice[..i]; | ||
let after = &slice[i + 1..]; | ||
unsafe { (Some(u8_slice_as_os_str(before)), Some(u8_slice_as_os_str(after))) } | ||
unsafe { (u8_slice_as_os_str(before), Some(u8_slice_as_os_str(after))) } | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
@@ -2201,9 +2201,11 @@ impl Path { | |
/// assert_eq!("foo", Path::new("foo.rs").file_prefix().unwrap()); | ||
/// assert_eq!("foo", Path::new("foo.tar.gz").file_prefix().unwrap()); | ||
/// ``` | ||
#[unstable(feature = "path_file_prefix", issue = "none")] | ||
#[unstable(feature = "path_file_prefix", issue = "86319")] | ||
pub fn file_prefix(&self) -> Option<&OsStr> { | ||
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.or(after)) | ||
self.file_name() | ||
.map(split_file_at_dot) | ||
.and_then(|(before, after)| if before.is_empty() { after } else { Some(before) }) | ||
|
||
} | ||
|
||
/// Extracts the extension of [`self.file_name`], if possible. | ||
|
Uh oh!
There was an error while loading. Please reload this page.