Skip to content

Commit 3885348

Browse files
committed
Use natord for human comparison
1 parent dc163f9 commit 3885348

File tree

3 files changed

+10
-125
lines changed

3 files changed

+10
-125
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ eframe = { version = "0.23", features = ["serde"] }
1515
egui = "0.23"
1616
humansize = "2"
1717
image = { version = "0.24", features = ["jpeg_rayon", "avif-decoder"] }
18+
natord = "1"
1819
once_cell = "1"
1920
rand = "0.8"
2021
rustc-hash = "1"

src/app/next_path.rs

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -21,129 +21,6 @@ pub struct NextPath {
2121
pub mode: Mode,
2222
}
2323

24-
#[derive(Debug)]
25-
struct Chunk<'a> {
26-
number: Option<u32>,
27-
string: &'a str,
28-
}
29-
30-
impl Ord for Chunk<'_> {
31-
fn cmp(&self, other: &Self) -> Ordering {
32-
match (self.number, other.number) {
33-
(Some(a), Some(b)) => a.cmp(&b),
34-
_ => self.string.cmp(other.string),
35-
}
36-
}
37-
}
38-
39-
impl<'a, 'b> PartialOrd<Chunk<'b>> for Chunk<'a> {
40-
fn partial_cmp(&self, other: &Chunk<'b>) -> Option<Ordering> {
41-
Some(self.cmp(other))
42-
}
43-
}
44-
45-
impl Eq for Chunk<'_> {}
46-
47-
impl<'a, 'b> PartialEq<Chunk<'b>> for Chunk<'a> {
48-
fn eq(&self, other: &Chunk<'b>) -> bool {
49-
self.cmp(other).is_eq()
50-
}
51-
}
52-
53-
fn chunks(s: &str) -> impl Iterator<Item = Chunk<'_>> {
54-
let mut want_num_chunk = false;
55-
let mut rest = s;
56-
57-
std::iter::from_fn(move || loop {
58-
if rest.is_empty() {
59-
break None;
60-
}
61-
62-
let chunk_end = rest
63-
.find(|ch: char| ch.is_ascii_digit() ^ want_num_chunk)
64-
.unwrap_or(rest.len());
65-
66-
want_num_chunk = !want_num_chunk;
67-
68-
let chunk = &rest[..chunk_end];
69-
if chunk.is_empty() {
70-
continue;
71-
}
72-
73-
rest = &rest[chunk_end..];
74-
75-
break Some(Chunk {
76-
number: chunk.parse().ok(),
77-
string: chunk,
78-
});
79-
})
80-
}
81-
82-
#[test]
83-
fn test_chunks() {
84-
assert_eq!(
85-
chunks("abc123").collect::<Vec<_>>(),
86-
[
87-
Chunk {
88-
number: None,
89-
string: "abc",
90-
},
91-
Chunk {
92-
number: Some(123),
93-
string: "123",
94-
},
95-
],
96-
);
97-
assert_eq!(
98-
chunks("123abc").collect::<Vec<_>>(),
99-
[
100-
Chunk {
101-
number: Some(123),
102-
string: "123",
103-
},
104-
Chunk {
105-
number: None,
106-
string: "abc",
107-
},
108-
],
109-
);
110-
assert_eq!(
111-
chunks("abc999999999999def").collect::<Vec<_>>(),
112-
[
113-
Chunk {
114-
number: None,
115-
string: "abc",
116-
},
117-
Chunk {
118-
number: None,
119-
string: "999999999999",
120-
},
121-
Chunk {
122-
number: None,
123-
string: "def",
124-
},
125-
],
126-
);
127-
}
128-
129-
fn human_compare(left: &str, right: &str) -> Ordering {
130-
chunks(left).cmp(chunks(right))
131-
}
132-
133-
#[test]
134-
fn test_human_compare() {
135-
use human_compare as h;
136-
137-
assert_eq!(h("a", "b"), Ordering::Less);
138-
assert_eq!(h("aa", "a"), Ordering::Greater);
139-
assert_eq!(h("abc123", "abc123"), Ordering::Equal);
140-
assert_eq!(h("abc", "abc123"), Ordering::Less);
141-
assert_eq!(h("test.dxt5", "test.jpg"), Ordering::Less);
142-
// #16
143-
assert_eq!(h("11", "100"), Ordering::Less);
144-
assert_eq!(h("a11", "a100"), Ordering::Less);
145-
}
146-
14724
impl Direction {
14825
fn for_ordering(self, ordering: Ordering) -> Ordering {
14926
match self {
@@ -218,13 +95,13 @@ impl<T: AsRef<str>> Eq for HumanCompare<T> {}
21895

21996
impl<T: AsRef<str>, U: AsRef<str>> PartialOrd<HumanCompare<U>> for HumanCompare<T> {
22097
fn partial_cmp(&self, other: &HumanCompare<U>) -> Option<Ordering> {
221-
Some(human_compare(self.0.as_ref(), other.0.as_ref()))
98+
Some(natord::compare(self.0.as_ref(), other.0.as_ref()))
22299
}
223100
}
224101

225102
impl<T: AsRef<str>> Ord for HumanCompare<T> {
226103
fn cmp(&self, other: &Self) -> Ordering {
227-
human_compare(self.0.as_ref(), other.0.as_ref())
104+
natord::compare(self.0.as_ref(), other.0.as_ref())
228105
}
229106
}
230107

0 commit comments

Comments
 (0)