Patchwork [2,of,2] rust-dirstate: leverage find_dirs() iterator in DirsMultiset

login
register
mail settings
Submitter Yuya Nishihara
Date June 30, 2019, 11:05 a.m.
Message ID <d1fcd5e0c62481bb338b.1561892735@mimosa>
Download mbox | patch
Permalink /patch/40738/
State Accepted
Headers show

Comments

Yuya Nishihara - June 30, 2019, 11:05 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1561887644 -32400
#      Sun Jun 30 18:40:44 2019 +0900
# Node ID d1fcd5e0c62481bb338b2d151f43748e4cc72904
# Parent  72ab74c704053b2212b874a902574fedadad4252
rust-dirstate: leverage find_dirs() iterator in DirsMultiset

Patch

diff --git a/rust/hg-core/src/dirstate/dirs_multiset.rs b/rust/hg-core/src/dirstate/dirs_multiset.rs
--- a/rust/hg-core/src/dirstate/dirs_multiset.rs
+++ b/rust/hg-core/src/dirstate/dirs_multiset.rs
@@ -8,6 +8,8 @@ 
 //! A multiset of directory names.
 //!
 //! Used to counts the references to directories in a manifest or dirstate.
+
+use crate::utils::files;
 use std::collections::hash_map::Entry;
 use std::collections::HashMap;
 use std::ops::Deref;
@@ -58,40 +60,16 @@  impl DirsMultiset {
         multiset
     }
 
-    /// Returns the slice up to the next directory name from right to left,
-    /// without trailing slash
-    fn find_dir(path: &[u8]) -> &[u8] {
-        let mut path = path;
-        loop {
-            if let Some(new_pos) = path.len().checked_sub(1) {
-                if path[new_pos] == b'/' {
-                    break &path[..new_pos];
-                }
-                path = &path[..new_pos];
-            } else {
-                break &[];
-            }
-        }
-    }
-
     /// Increases the count of deepest directory contained in the path.
     ///
     /// If the directory is not yet in the map, adds its parents.
     pub fn add_path(&mut self, path: &[u8]) {
-        let mut pos = path.len();
-
-        loop {
-            let subpath = Self::find_dir(&path[..pos]);
+        for subpath in files::find_dirs(path) {
             if let Some(val) = self.inner.get_mut(subpath) {
                 *val += 1;
                 break;
             }
             self.inner.insert(subpath.to_owned(), 1);
-
-            pos = subpath.len();
-            if pos == 0 {
-                break;
-            }
         }
     }
 
@@ -104,10 +82,7 @@  impl DirsMultiset {
         &mut self,
         path: &[u8],
     ) -> Result<(), DirstateMapError> {
-        let mut pos = path.len();
-
-        loop {
-            let subpath = Self::find_dir(&path[..pos]);
+        for subpath in files::find_dirs(path) {
             match self.inner.entry(subpath.to_owned()) {
                 Entry::Occupied(mut entry) => {
                     let val = entry.get().clone();
@@ -120,11 +95,6 @@  impl DirsMultiset {
                 Entry::Vacant(_) => {
                     return Err(DirstateMapError::PathNotFound(path.to_owned()))
                 }
-            };
-
-            pos = subpath.len();
-            if pos == 0 {
-                break;
             }
         }