Skip to content

Commit cfbfa43

Browse files
committed
feat: add State::prefixed_range() to obtain a range of entries matching a prefix.
This makes it easier to make changes to entries of a certain prefix.
1 parent 8b689c2 commit cfbfa43

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

gix-index/src/access/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,18 @@ impl State {
184184
}
185185

186186
/// 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.
187189
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>> {
188197
if prefix.is_empty() {
189-
return Some(self.entries());
198+
return Some(0..self.entries.len());
190199
}
191200
let prefix_len = prefix.len();
192201
let mut low = self
@@ -195,7 +204,7 @@ impl State {
195204
let mut high = low
196205
+ self.entries[low..].partition_point(|e| e.path(self).get(..prefix_len).map_or(false, |p| p <= prefix));
197206

198-
let low_entry = &self.entries[low];
207+
let low_entry = &self.entries.get(low)?;
199208
if low_entry.stage() != 0 {
200209
low = self
201210
.walk_entry_stages(low_entry.path(self), low, Ordering::Less)
@@ -208,7 +217,7 @@ impl State {
208217
.unwrap_or(high);
209218
}
210219
}
211-
(low != high).then_some(low..high).map(|range| &self.entries[range])
220+
(low != high).then_some(low..high)
212221
}
213222

214223
/// Return the entry at `idx` or _panic_ if the index is out of bounds.

gix-index/tests/index/access.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ fn prefixed_entries_with_multi_stage_file() {
5050
file.entries(),
5151
"empty prefix matches all"
5252
);
53+
assert_eq!(file.prefixed_entries_range("".into()), Some(0..3));
54+
assert_eq!(file.prefixed_entries_range("foo".into()), None);
5355
}
5456

5557
#[test]

0 commit comments

Comments
 (0)