Skip to content

Commit 2796cc1

Browse files
committed
csplit: managed the positive and negative offset when the --suppressed-matched flag is active
1 parent 000981f commit 2796cc1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/uu/csplit/src/csplit.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ impl SplitWriter<'_> {
375375
while let Some((ln, line)) = input_iter.next() {
376376
let l = line?;
377377
if regex.is_match(&l) {
378+
let mut next_line_suppress_matched = false;
378379
match (self.options.suppress_matched, offset) {
379380
// no offset, add the line to the next split
380381
(false, 0) => {
@@ -385,6 +386,11 @@ impl SplitWriter<'_> {
385386
}
386387
// a positive offset, some more lines need to be added to the current split
387388
(false, _) => self.writeln(&l)?,
389+
// suppress matched option true, but there is a positive offset, so the line is printed
390+
(true, 1..) => {
391+
next_line_suppress_matched = true;
392+
self.writeln(&l)?;
393+
}
388394
_ => (),
389395
};
390396
offset -= 1;
@@ -405,6 +411,11 @@ impl SplitWriter<'_> {
405411
offset -= 1;
406412
}
407413
self.finish_split();
414+
415+
// if we have to suppress one line after we take the next and do nothing
416+
if next_line_suppress_matched {
417+
input_iter.next();
418+
}
408419
return Ok(());
409420
}
410421
self.writeln(&l)?;
@@ -430,7 +441,13 @@ impl SplitWriter<'_> {
430441
input_iter.add_line_to_buffer(ln, l).is_none(),
431442
"should be big enough to hold every lines"
432443
);
444+
} else {
445+
// since offset_usize is for sure greater than 0
446+
// the first element of the buffer should be removed and this
447+
// line inserted to be coherent with GNU implementation
448+
input_iter.add_line_to_buffer(ln, l);
433449
}
450+
434451
self.finish_split();
435452
if input_iter.buffer_len() < offset_usize {
436453
return Err(CsplitError::LineOutOfRange(pattern_as_str.to_string()));

0 commit comments

Comments
 (0)