diff rust/hg-core/src/utils.rs @ 42863:62eabdf91f85

rustfilepatterns: refactor the pattern of removing a prefix from a &[u8] Differential Revision: https://phab.mercurial-scm.org/D6766
author Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
date Mon, 26 Aug 2019 08:25:01 -0400
parents ce6797ef6eab
children 3fe40dd6355d
line wrap: on
line diff
--- a/rust/hg-core/src/utils.rs	Sun Aug 25 22:52:36 2019 -0400
+++ b/rust/hg-core/src/utils.rs	Mon Aug 26 08:25:01 2019 -0400
@@ -40,6 +40,7 @@
     fn trim_end(&self) -> &Self;
     fn trim_start(&self) -> &Self;
     fn trim(&self) -> &Self;
+    fn drop_prefix(&self, needle: &Self) -> Option<&Self>;
 }
 
 fn is_not_whitespace(c: &u8) -> bool {
@@ -80,4 +81,12 @@
     fn trim(&self) -> &[u8] {
         self.trim_start().trim_end()
     }
+
+    fn drop_prefix(&self, needle: &Self) -> Option<&Self> {
+        if self.starts_with(needle) {
+            Some(&self[needle.len()..])
+        } else {
+            None
+        }
+    }
 }