diff rust/hg-core/src/utils/hg_path.rs @ 44314:0e9ac3968b56

rust-dirs-multiset: add `DirsChildrenMultiset` In a future patch, this structure will be needed to store information needed by the (also upcoming) `IgnoreMatcher`. Differential Revision: https://phab.mercurial-scm.org/D7869
author Rapha?l Gom?s <rgomes@octobus.net>
date Tue, 14 Jan 2020 17:04:32 +0100
parents 9ab4830e9e3d
children 26114bd6ec60
line wrap: on
line diff
--- a/rust/hg-core/src/utils/hg_path.rs	Tue Jan 14 16:50:35 2020 +0100
+++ b/rust/hg-core/src/utils/hg_path.rs	Tue Jan 14 17:04:32 2020 +0100
@@ -183,6 +183,29 @@
             &self.inner[..]
         })
     }
+    /// Returns a tuple of slices `(base, filename)` resulting from the split
+    /// at the rightmost `/`, if any.
+    ///
+    /// # Examples:
+    ///
+    /// ```
+    /// use hg::utils::hg_path::HgPath;
+    ///
+    /// let path = HgPath::new(b"cool/hg/path").split_filename();
+    /// assert_eq!(path, (HgPath::new(b"cool/hg"), HgPath::new(b"path")));
+    ///
+    /// let path = HgPath::new(b"pathwithoutsep").split_filename();
+    /// assert_eq!(path, (HgPath::new(b""), HgPath::new(b"pathwithoutsep")));
+    /// ```
+    pub fn split_filename(&self) -> (&Self, &Self) {
+        match &self.inner.iter().rposition(|c| *c == b'/') {
+            None => (HgPath::new(""), &self),
+            Some(size) => (
+                HgPath::new(&self.inner[..*size]),
+                HgPath::new(&self.inner[*size + 1..]),
+            ),
+        }
+    }
     pub fn join<T: ?Sized + AsRef<Self>>(&self, other: &T) -> HgPathBuf {
         let mut inner = self.inner.to_owned();
         if inner.len() != 0 && inner.last() != Some(&b'/') {