Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change return signature for split_file_at_dot
  • Loading branch information
mbhall88 committed Jun 24, 2021
commit fcb1ebf1945c616365c74e95666dcdcfdc8bbd70
14 changes: 8 additions & 6 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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))) }
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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) })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first branch on this line (when before is empty) has no test coverage. What is this case for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I had it in there for the case where the filename was a single . or empty, but this is inherently handled by file_name already, so we never actually try and split on . for these cases. I've removed the branch and all tests pass. I also added another test case.

}

/// Extracts the extension of [`self.file_name`], if possible.
Expand Down