refactor: simplify code in rust version of path_encode
authorArseniy Alekseyev <aalekseyev@janestreet.com>
Thu, 16 Feb 2023 16:20:17 +0000
changeset 50158 0d2ec486d95c
parent 50157 fbb4c7117cf1
child 50159 96d31efd21f7
refactor: simplify code in rust version of path_encode Moving the addition of '/' separator to the end of the loop makes the rest of the logic much simpler because the first iteration is no longer special.
rust/hg-core/src/revlog/path_encode.rs
--- a/rust/hg-core/src/revlog/path_encode.rs	Mon Feb 20 23:46:20 2023 +0100
+++ b/rust/hg-core/src/revlog/path_encode.rs	Thu Feb 16 16:20:17 2023 +0000
@@ -543,25 +543,16 @@
     memcopy(Some(&mut dest), &mut destlen, b"dh/");
 
     if let Some(last_slash) = last_slash {
-        let mut first = true;
         for slice in src[..last_slash].split(|b| *b == b'/') {
             let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)];
-            if destlen + (slice.len() + if first { 0 } else { 1 })
-                > maxshortdirslen + 3
-            {
+            if destlen + slice.len() > maxshortdirslen + 3 {
                 break;
             } else {
-                if !first {
-                    charcopy(Some(&mut dest), &mut destlen, b'/')
-                };
                 memcopy(Some(&mut dest), &mut destlen, slice);
                 if dest[destlen - 1] == b'.' || dest[destlen - 1] == b' ' {
                     dest[destlen - 1] = b'_'
                 }
             }
-            first = false;
-        }
-        if !first {
             charcopy(Some(&mut dest), &mut destlen, b'/');
         }
     }