Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/uucore/src/lib/features/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,14 @@ impl Range {
/// Is guaranteed to return only disjoint ranges in a sorted order.
pub fn merge(mut ranges: Vec<Self>) -> Vec<Self> {
ranges.sort();

// merge overlapping ranges
for i in 0..ranges.len() {
let j = i + 1;

while j < ranges.len() && ranges[j].low <= ranges[i].high {
let j_high = ranges.remove(j).high;
ranges[i].high = max(ranges[i].high, j_high);
ranges.dedup_by(|a, b| {

@anastygnome anastygnome Sep 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub fn merge(mut ranges: Vec<Self>) -> Vec<Self> {
    ranges.sort_unstable_by_key(|r| r.low);
[...]
}

Is better for two reasons :

We don't need a stable sort because we merge values, unstable sort is always more efficient.
Secondly, because we use the derived Ord trait, we compare both ends when we only need compare the low one.

if a.low <= b.high {
b.high = max(b.high, a.high);
true
} else {
false
}
}
});
ranges
}
}
Expand Down
Loading