Skip to content
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

Prevent unnecessary allocation in PathBuf::set_extension. #65731

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Changes from all commits
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
26 changes: 15 additions & 11 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,20 +1363,24 @@ impl PathBuf {
}

fn _set_extension(&mut self, extension: &OsStr) -> bool {
if self.file_name().is_none() {
return false;
}

let mut stem = match self.file_stem() {
Some(stem) => stem.to_os_string(),
None => OsString::new(),
let file_stem = match self.file_stem() {
None => return false,
Some(f) => os_str_as_u8_slice(f),
};

if !os_str_as_u8_slice(extension).is_empty() {
stem.push(".");
stem.push(extension);
// truncate until right after the file stem
let end_file_stem = file_stem[file_stem.len()..].as_ptr() as usize;
let start = os_str_as_u8_slice(&self.inner).as_ptr() as usize;
let v = self.as_mut_vec();
v.truncate(end_file_stem.wrapping_sub(start));

// add the new extension, if any
let new = os_str_as_u8_slice(extension);
if !new.is_empty() {
v.reserve_exact(new.len() + 1);
v.push(b'.');
v.extend_from_slice(new);
}
self.set_file_name(&stem);

true
}
Expand Down