diff --git a/src/path.rs b/src/path.rs
index 06082e1b2..0cf55c527 100644
--- a/src/path.rs
+++ b/src/path.rs
@@ -1,6 +1,9 @@
//! Paths
-use core::{convert::TryFrom, fmt, iter::FusedIterator, marker::PhantomData, ops, ptr, slice, str};
+use core::{
+ cmp::Ordering, convert::TryFrom, fmt, iter::FusedIterator, marker::PhantomData, ops, ptr,
+ slice, str,
+};
use cstr_core::CStr;
use cty::{c_char, size_t};
@@ -20,6 +23,66 @@ pub struct Path {
inner: CStr,
}
+impl Path {
+ /// Compare the path using their string representation
+ /// This comarison function as would be expected for a `String` type.
+ ///
+ ///
+ /// This ordering does not match the ordering obsvered when iterating over a directory.
+ ///
+ /// See
cmp_lfs and
littlefs#923.
+ ///
+ ///
+ /// ```
+ ///# use std::cmp::Ordering;
+ ///# use littlefs2::path;
+ /// assert_eq!(path!("some_path_a").cmp_str(path!("some_path_b")), Ordering::Less);
+ /// assert_eq!(path!("some_path_b").cmp_str(path!("some_path_a")), Ordering::Greater);
+ /// assert_eq!(path!("some_path").cmp_str(path!("some_path_a")), Ordering::Less);
+ /// assert_eq!(path!("some_path").cmp_str(path!("some_path_b")), Ordering::Less);
+ /// assert_eq!(path!("some_path").cmp_str(path!("some_path")), Ordering::Equal);
+ ///```
+ pub fn cmp_str(&self, other: &Path) -> Ordering {
+ self.inner.cmp(&other.inner)
+ }
+
+ /// Compare the path using their string representation
+ ///
+ /// This comparison function matches the iteration order of `littlefs` when iterating over directory.
+ /// For more information, see [littlefs#923](https://github.com/littlefs-project/littlefs/issues/923)
+ ///
+ /// ```
+ ///# use std::cmp::Ordering;
+ ///# use littlefs2::path;
+ /// assert_eq!(path!("some_path_a").cmp_lfs(path!("some_path_b")), Ordering::Less);
+ /// assert_eq!(path!("some_path_b").cmp_lfs(path!("some_path_a")), Ordering::Greater);
+ /// assert_eq!(path!("some_path").cmp_lfs(path!("some_path_a")), Ordering::Greater);
+ /// assert_eq!(path!("some_path").cmp_lfs(path!("some_path_b")), Ordering::Greater);
+ /// assert_eq!(path!("some_path_a").cmp_lfs(path!("some_path")), Ordering::Less);
+ /// assert_eq!(path!("some_path_b").cmp_lfs(path!("some_path")), Ordering::Less);
+ /// assert_eq!(path!("some_path").cmp_lfs(path!("some_path")), Ordering::Equal);
+ ///```
+ pub fn cmp_lfs(&self, other: &Path) -> Ordering {
+ let this = self.inner.to_bytes();
+ let other = other.inner.to_bytes();
+
+ let min_len = this.len().min(other.len());
+
+ match this[0..min_len].cmp(&other[0..min_len]) {
+ Ordering::Less => Ordering::Less,
+ Ordering::Greater => Ordering::Greater,
+ Ordering::Equal if this.len() != other.len() => {
+ if this.len() < other.len() {
+ Ordering::Greater
+ } else {
+ Ordering::Less
+ }
+ }
+ Ordering::Equal => Ordering::Equal,
+ }
+ }
+}
+
/// Iterator over the ancestors of a Path
///
/// See documentation for [`Path::ancestors`][]