Skip to content

Commit

Permalink
csplit: managed the positive and negative offset when the --suppresse…
Browse files Browse the repository at this point in the history
…d-matched flag is active
  • Loading branch information
Felle33 committed Jan 7, 2025
1 parent 000981f commit 2796cc1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/uu/csplit/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ impl SplitWriter<'_> {
while let Some((ln, line)) = input_iter.next() {
let l = line?;
if regex.is_match(&l) {
let mut next_line_suppress_matched = false;
match (self.options.suppress_matched, offset) {
// no offset, add the line to the next split
(false, 0) => {
Expand All @@ -385,6 +386,11 @@ impl SplitWriter<'_> {
}
// a positive offset, some more lines need to be added to the current split
(false, _) => self.writeln(&l)?,
// suppress matched option true, but there is a positive offset, so the line is printed
(true, 1..) => {
next_line_suppress_matched = true;
self.writeln(&l)?;
}
_ => (),
};
offset -= 1;
Expand All @@ -405,6 +411,11 @@ impl SplitWriter<'_> {
offset -= 1;
}
self.finish_split();

// if we have to suppress one line after we take the next and do nothing
if next_line_suppress_matched {
input_iter.next();
}
return Ok(());
}
self.writeln(&l)?;
Expand All @@ -430,7 +441,13 @@ impl SplitWriter<'_> {
input_iter.add_line_to_buffer(ln, l).is_none(),
"should be big enough to hold every lines"
);
} else {
// since offset_usize is for sure greater than 0
// the first element of the buffer should be removed and this
// line inserted to be coherent with GNU implementation
input_iter.add_line_to_buffer(ln, l);
}

self.finish_split();
if input_iter.buffer_len() < offset_usize {
return Err(CsplitError::LineOutOfRange(pattern_as_str.to_string()));
Expand Down

0 comments on commit 2796cc1

Please sign in to comment.