8000 csplit: fix bug when --suppress-matched flag is active and positive/negative offset is present by Felle33 · Pull Request #7088 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

csplit: fix bug when --suppress-matched flag is active and positive/negative offset is present #7088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion src/uu/csplit/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,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 @@ -382,6 +383,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 @@ -402,6 +408,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 @@ -420,14 +431,20 @@ impl SplitWriter<'_> {
for line in input_iter.shrink_buffer_to_size() {
self.writeln(&line)?;
}
if !self.options.suppress_matched {
if self.options.suppress_matched {
// 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);
} else {
// add 1 to the buffer size to make place for the matched line
input_iter.set_size_of_buffer(offset_usize + 1);
assert!(
input_iter.add_line_to_buffer(ln, l).is_none(),
"should be big enough to hold every lines"
);
}

self.finish_split();
if input_iter.buffer_len() < offset_usize {
return Err(CsplitError::LineOutOfRange(pattern_as_str.to_string()));
Expand Down
10 changes: 5 additions & 5 deletions tests/by-util/test_csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,29 +469,29 @@ fn test_up_to_match_offset_option_suppress_matched() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&["numbers50.txt", "--suppress-matched", "/10/+4"])
.succeeds()
.stdout_only("27\n111\n");
.stdout_only("30\n108\n");

let count = glob(&at.plus_as_string("xx*"))
.expect("there should be splits created")
.count();
assert_eq!(count, 2);
assert_eq!(at.read("xx00"), generate(1, 10) + &generate(11, 14));
assert_eq!(at.read("xx01"), generate(14, 51));
assert_eq!(at.read("xx00"), generate(1, 14));
assert_eq!(at.read("xx01"), generate(15, 51));
}

#[test]
fn test_up_to_match_negative_offset_option_suppress_matched() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&["numbers50.txt", "--suppress-matched", "/10/-4"])
.succeeds()
.stdout_only("10\n128\n");
.stdout_only("10\n129\n");

let count = glob(&at.plus_as_string("xx*"))
.expect("there should be splits created")
.count();
assert_eq!(count, 2);
assert_eq!(at.read("xx00"), generate(1, 6));
assert_eq!(at.read("xx01"), generate(6, 10) + &generate(11, 51));
assert_eq!(at.read("xx01"), generate(7, 51));
}

#[test]
Expand Down
Loading
0