@@ -21,129 +21,6 @@ pub struct NextPath {
21
21
pub mode : Mode ,
22
22
}
23
23
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
-
147
24
impl Direction {
148
25
fn for_ordering ( self , ordering : Ordering ) -> Ordering {
149
26
match self {
@@ -218,13 +95,13 @@ impl<T: AsRef<str>> Eq for HumanCompare<T> {}
218
95
219
96
impl < T : AsRef < str > , U : AsRef < str > > PartialOrd < HumanCompare < U > > for HumanCompare < T > {
220
97
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 ( ) ) )
222
99
}
223
100
}
224
101
225
102
impl < T : AsRef < str > > Ord for HumanCompare < T > {
226
103
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 ( ) )
228
105
}
229
106
}
230
107
0 commit comments