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.
fn set_extension<T: BytesContainer>(&mut self, extension: T) {
assert!(!contains_nul(&extension));
// borrowck causes problems here too
let val = {
match self.filename() {
None => None,
Some(name) => {
let dot = '.' as u8;
match name.rposition_elem(&dot) {
None | Some(0) => {
if extension.container_as_bytes().is_empty() {
None
} else {
let mut v;
let extension = extension.container_as_bytes();
v = slice::with_capacity(name.len() + extension.len() + 1);
v.push_all(name);
v.push(dot);
v.push_all(extension);
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)
}
}
}
let val = self.filename().and_then(|name| {
let dot = '.' as u8;
let extlen = extension.container_as_bytes().len();
match (name.rposition_elem(&dot), extlen) {
(None, 0) | (Some(0), 0) => None,
(Some(idx), 0) => Some(name.slice_to(idx).to_owned()),
(idx, extlen) => {
let idx = match idx {
None | Some(0) => name.len(),
Some(val) => val
};
let mut v;
v = slice::with_capacity(idx + extlen + 1);
v.push_all(name.slice_to(idx));
v.push(dot);
v.push_all(extension.container_as_bytes());
Some(v)
}
}
};
});
match val {
None => (),
Some(v) => unsafe { self.set_filename_unchecked(v) }