1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cut: don't merge adjacent ranges

This commit is contained in:
Daniel Hofstetter 2024-12-22 14:53:37 +01:00
parent 913d5d413b
commit 392c48002c
2 changed files with 4 additions and 9 deletions

View file

@ -91,7 +91,7 @@ impl Range {
Ok(Self::merge(ranges)) Ok(Self::merge(ranges))
} }
/// Merge any overlapping ranges /// Merge any overlapping ranges. Adjacent ranges are *NOT* merged.
/// ///
/// Is guaranteed to return only disjoint ranges in a sorted order. /// Is guaranteed to return only disjoint ranges in a sorted order.
fn merge(mut ranges: Vec<Self>) -> Vec<Self> { fn merge(mut ranges: Vec<Self>) -> Vec<Self> {
@ -101,10 +101,7 @@ impl Range {
for i in 0..ranges.len() { for i in 0..ranges.len() {
let j = i + 1; let j = i + 1;
// The +1 is a small optimization, because we can merge adjacent Ranges. while j < ranges.len() && ranges[j].low <= ranges[i].high {
// For example (1,3) and (4,6), because in the integers, there are no
// possible values between 3 and 4, this is equivalent to (1,6).
while j < ranges.len() && ranges[j].low <= ranges[i].high + 1 {
let j_high = ranges.remove(j).high; let j_high = ranges.remove(j).high;
ranges[i].high = max(ranges[i].high, j_high); ranges[i].high = max(ranges[i].high, j_high);
} }
@ -216,8 +213,8 @@ mod test {
&[r(10, 40), r(50, 60)], &[r(10, 40), r(50, 60)],
); );
// Merge adjacent ranges // Don't merge adjacent ranges
m(vec![r(1, 3), r(4, 6)], &[r(1, 6)]); m(vec![r(1, 3), r(4, 6)], &[r(1, 3), r(4, 6)]);
} }
#[test] #[test]

View file

@ -350,7 +350,6 @@ fn test_newline_preservation_with_f1_option() {
ucmd.args(&["-f1-", "1"]).succeeds().stdout_is(expected); ucmd.args(&["-f1-", "1"]).succeeds().stdout_is(expected);
} }
#[ignore = "Not yet implemented"]
#[test] #[test]
fn test_output_delimiter_with_character_ranges() { fn test_output_delimiter_with_character_ranges() {
new_ucmd!() new_ucmd!()
@ -360,7 +359,6 @@ fn test_output_delimiter_with_character_ranges() {
.stdout_only("bc:defg\n"); .stdout_only("bc:defg\n");
} }
#[ignore = "Not yet implemented"]
#[test] #[test]
fn test_output_delimiter_with_adjacent_ranges() { fn test_output_delimiter_with_adjacent_ranges() {
new_ucmd!() new_ucmd!()