Skip to content

Commit 13cfcd0

Browse files
tertsdiepraamsylvestre
authored andcommitted
csplit: suppress lines matched with line numbers even if they have already passed
1 parent 8a9fb84 commit 13cfcd0

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/uu/csplit/src/csplit.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,7 @@ impl<'a> SplitWriter<'a> {
317317
while let Some((ln, line)) = input_iter.next() {
318318
let l = line?;
319319
match n.cmp(&(&ln + 1)) {
320-
Ordering::Less => {
321-
assert!(
322-
input_iter.add_line_to_buffer(ln, l).is_none(),
323-
"the buffer is big enough to contain 1 line"
324-
);
325-
ret = Ok(());
326-
break;
327-
}
328-
Ordering::Equal => {
320+
Ordering::Less | Ordering::Equal => {
329321
assert!(
330322
self.options.suppress_matched
331323
|| input_iter.add_line_to_buffer(ln, l).is_none(),

tests/by-util/test_csplit.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,43 @@ fn test_up_to_match_offset_option_suppress_matched() {
467467
assert_eq!(at.read("xx01"), generate(14, 51));
468468
}
469469

470+
#[test]
471+
fn test_line_number_after_match_suppressed() {
472+
let (at, mut ucmd) = at_and_ucmd!();
473+
ucmd.args(&["numbers50.txt", "--suppress-matched", "/10/", "9"])
474+
.succeeds()
475+
.stdout_only("18\n0\n117\n");
476+
477+
let count = glob(&at.plus_as_string("xx*"))
478+
.expect("there should be splits created")
479+
.count();
480+
assert_eq!(count, 3);
481+
assert_eq!(at.read("xx00"), generate(1, 10));
482+
assert_eq!(at.read("xx01"), "");
483+
484+
// The 9 has suppressed line 11
485+
assert_eq!(at.read("xx02"), generate(12, 51));
486+
}
487+
488+
#[test]
489+
fn test_same_number_suppressed() {
490+
let (at, mut ucmd) = at_and_ucmd!();
491+
ucmd.args(&["numbers50.txt", "--suppress-matched", "5", "5", "5"])
492+
.succeeds()
493+
.stdout_only("8\n0\n0\n127\n");
494+
495+
let count = glob(&at.plus_as_string("xx*"))
496+
.expect("there should be splits created")
497+
.count();
498+
assert_eq!(count, 3);
499+
assert_eq!(at.read("xx00"), generate(1, 5));
500+
assert_eq!(at.read("xx01"), "");
501+
assert_eq!(at.read("xx02"), "");
502+
503+
// The fives suppress lines 5, 6 and 7.
504+
assert_eq!(at.read("xx02"), generate(8, 51));
505+
}
506+
470507
#[test]
471508
fn test_up_to_match_negative_offset_option_suppress_matched() {
472509
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)