D6765: rustfilepatterns: shorter code for concatenating slices

valentin.gatienbaron (Valentin Gatien-Baron) phabricator at mercurial-scm.org
Mon Aug 26 00:16:57 EDT 2019


valentin.gatienbaron created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D6765

AFFECTED FILES
  rust/hg-core/src/filepatterns.rs

CHANGE DETAILS

diff --git a/rust/hg-core/src/filepatterns.rs b/rust/hg-core/src/filepatterns.rs
--- a/rust/hg-core/src/filepatterns.rs
+++ b/rust/hg-core/src/filepatterns.rs
@@ -158,26 +158,20 @@
             if pattern[0] == b'^' {
                 return pattern.to_owned();
             }
-            let mut res = b".*".to_vec();
-            res.extend(pattern);
-            res
+            [&b".*"[..], pattern].concat()
         }
         PatternSyntax::Path | PatternSyntax::RelPath => {
             if pattern == b"." {
                 return vec![];
             }
-            let mut pattern = escape_pattern(pattern);
-            pattern.extend(b"(?:/|$)");
-            pattern
+            [&escape_pattern(pattern), &b"(?:/|$)"[..]].concat()
         }
         PatternSyntax::RootFiles => {
             let mut res = if pattern == b"." {
                 vec![]
             } else {
                 // Pattern is a directory name.
-                let mut as_vec: Vec<u8> = escape_pattern(pattern);
-                as_vec.push(b'/');
-                as_vec
+                [&escape_pattern(pattern), &b"/"[..]].concat()
             };
 
             // Anything after the pattern must be a non-directory.
@@ -185,24 +179,16 @@
             res
         }
         PatternSyntax::RelGlob => {
-            let mut res: Vec<u8> = vec![];
             let glob_re = glob_to_re(pattern);
             if let Some(rest) = glob_re.chop_prefix(b"[^/]*") {
-                res.extend(b".*");
-                res.extend(rest);
+                [&b".*"[..], rest, globsuffix].concat()
             } else {
-                res.extend(b"(?:|.*/)");
-                res.extend(glob_re);
+                [&b"(?:|.*/)"[..], &glob_re, globsuffix].concat()
             }
-            res.extend(globsuffix.iter());
-            res
         }
         PatternSyntax::Glob
         | PatternSyntax::RootGlob => {
-            let mut res: Vec<u8> = vec![];
-            res.extend(glob_to_re(pattern));
-            res.extend(globsuffix.iter());
-            res
+            [&glob_to_re(pattern), globsuffix].concat()
         }
     }
 }



To: valentin.gatienbaron, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel


More information about the Mercurial-devel mailing list