Simplify GenericPath::set_extension.

This commit is contained in:
Kevin Butler 2014-04-08 03:13:23 +01:00
parent d1e20488a5
commit 9b9ad9b741

View File

@ -287,42 +287,29 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
/// Fails the task if the extension contains a NUL. /// Fails the task if the extension contains a NUL.
fn set_extension<T: BytesContainer>(&mut self, extension: T) { fn set_extension<T: BytesContainer>(&mut self, extension: T) {
assert!(!contains_nul(&extension)); assert!(!contains_nul(&extension));
// borrowck causes problems here too
let val = { let val = self.filename().and_then(|name| {
match self.filename() { let dot = '.' as u8;
None => None, let extlen = extension.container_as_bytes().len();
Some(name) => { match (name.rposition_elem(&dot), extlen) {
let dot = '.' as u8; (None, 0) | (Some(0), 0) => None,
match name.rposition_elem(&dot) { (Some(idx), 0) => Some(name.slice_to(idx).to_owned()),
None | Some(0) => { (idx, extlen) => {
if extension.container_as_bytes().is_empty() { let idx = match idx {
None None | Some(0) => name.len(),
} else { Some(val) => val
let mut v; };
let extension = extension.container_as_bytes();
v = slice::with_capacity(name.len() + extension.len() + 1); let mut v;
v.push_all(name); v = slice::with_capacity(idx + extlen + 1);
v.push(dot); v.push_all(name.slice_to(idx));
v.push_all(extension); v.push(dot);
Some(v) v.push_all(extension.container_as_bytes());
} Some(v)
}
Some(idx) => {
if extension.container_as_bytes().is_empty() {
Some(name.slice_to(idx).to_owned())
} else {
let mut v;
let extension = extension.container_as_bytes();
v = slice::with_capacity(idx + extension.len() + 1);
v.push_all(name.slice_to(idx+1));
v.push_all(extension);
Some(v)
}
}
}
} }
} }
}; });
match val { match val {
None => (), None => (),
Some(v) => unsafe { self.set_filename_unchecked(v) } Some(v) => unsafe { self.set_filename_unchecked(v) }