Przeglądaj źródła

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.
Ben S 11 lat temu
rodzic
commit
7063c21ba0
2 zmienionych plików z 10 dodań i 4 usunięć
  1. 9 3
      file.rs
  2. 1 1
      options.rs

+ 9 - 3
file.rs

@@ -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 {

+ 1 - 1
options.rs

@@ -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)
             }),