@@ -184,9 +184,18 @@ impl State {
184
184
}
185
185
186
186
/// Return the slice of entries which all share the same `prefix`, or `None` if there isn't a single such entry.
187
+ ///
188
+ /// If `prefix` is empty, all entries are returned.
187
189
pub fn prefixed_entries ( & self , prefix : & BStr ) -> Option < & [ Entry ] > {
190
+ self . prefixed_entries_range ( prefix) . map ( |range| & self . entries [ range] )
191
+ }
192
+
193
+ /// Return the range of entries which all share the same `prefix`, or `None` if there isn't a single such entry.
194
+ ///
195
+ /// If `prefix` is empty, the range will include all entries.
196
+ pub fn prefixed_entries_range ( & self , prefix : & BStr ) -> Option < Range < usize > > {
188
197
if prefix. is_empty ( ) {
189
- return Some ( self . entries ( ) ) ;
198
+ return Some ( 0 .. self . entries . len ( ) ) ;
190
199
}
191
200
let prefix_len = prefix. len ( ) ;
192
201
let mut low = self
@@ -195,7 +204,7 @@ impl State {
195
204
let mut high = low
196
205
+ self . entries [ low..] . partition_point ( |e| e. path ( self ) . get ( ..prefix_len) . map_or ( false , |p| p <= prefix) ) ;
197
206
198
- let low_entry = & self . entries [ low] ;
207
+ let low_entry = & self . entries . get ( low) ? ;
199
208
if low_entry. stage ( ) != 0 {
200
209
low = self
201
210
. walk_entry_stages ( low_entry. path ( self ) , low, Ordering :: Less )
@@ -208,7 +217,7 @@ impl State {
208
217
. unwrap_or ( high) ;
209
218
}
210
219
}
211
- ( low != high) . then_some ( low..high) . map ( |range| & self . entries [ range ] )
220
+ ( low != high) . then_some ( low..high)
212
221
}
213
222
214
223
/// Return the entry at `idx` or _panic_ if the index is out of bounds.
0 commit comments