Actually, cache the calls to ext

They get computed often enough for this to be worthwhile, and I have plans for using the extension even if it's not what's being sorted by.
This commit is contained in:
Ben S 2014-05-24 02:36:00 +01:00
parent 184de0ce79
commit 7063c21ba0
2 changed files with 10 additions and 4 deletions

12
file.rs
View File

@ -11,6 +11,7 @@ use unix::{get_user_name, get_group_name};
// result around with the file for safe keeping.
pub struct File<'a> {
pub name: &'a str,
pub ext: Option<&'a str>,
pub path: &'a Path,
pub stat: io::FileStat,
}
@ -28,12 +29,17 @@ impl<'a> File<'a> {
Err(e) => fail!("Couldn't stat {}: {}", filename, e),
};
return File { path: path, stat: stat, name: filename };
return File {
path: path,
stat: stat,
name: filename,
ext: File::ext(filename),
};
}
pub fn ext(&self) -> Option<&'a str> {
fn ext(name: &'a str) -> Option<&'a str> {
let re = regex!(r"\.(.+)$");
re.captures(self.name).map(|caps| caps.at(1))
re.captures(name).map(|caps| caps.at(1))
}
pub fn is_dotfile(&self) -> bool {

View File

@ -25,7 +25,7 @@ impl SortField {
Name => files.sort_by(|a, b| a.name.cmp(&b.name)),
Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
Extension => files.sort_by(|a, b| {
let exts = a.ext().cmp(&b.ext());
let exts = a.ext.cmp(&b.ext);
let names = a.name.cmp(&b.name);
lexical_ordering(exts, names)
}),