Skip to content

Commit

Permalink
Simplify GenericPath::set_extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryman committed Apr 11, 2014
1 parent d1e2048 commit 9b9ad9b
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions src/libstd/path/mod.rs
Expand Up @@ -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) }
Expand Down

5 comments on commit 9b9ad9b

@bors
Copy link
Contributor

@bors bors commented on 9b9ad9b Apr 11, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at Ryman@9b9ad9b

@bors
Copy link
Contributor

@bors bors commented on 9b9ad9b Apr 11, 2014

Choose a reason for hiding this comment

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

merging Ryman/rust/bytecontainer_impl_container = 9b9ad9b into auto

@bors
Copy link
Contributor

@bors bors commented on 9b9ad9b Apr 11, 2014

Choose a reason for hiding this comment

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

Ryman/rust/bytecontainer_impl_container = 9b9ad9b merged ok, testing candidate = ecc774f

@bors
Copy link
Contributor

@bors bors commented on 9b9ad9b Apr 11, 2014

@bors
Copy link
Contributor

@bors bors commented on 9b9ad9b Apr 11, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = ecc774f

Please sign in to comment.