mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
csplit: do not emit remainder of input after an error
This commit is contained in:
parent
44fa2e960a
commit
27fd3e5d39
2 changed files with 21 additions and 26 deletions
|
@ -99,15 +99,17 @@ where
|
||||||
let patterns: Vec<patterns::Pattern> = patterns::get_patterns(&patterns[..])?;
|
let patterns: Vec<patterns::Pattern> = patterns::get_patterns(&patterns[..])?;
|
||||||
let ret = do_csplit(&mut split_writer, patterns, &mut input_iter);
|
let ret = do_csplit(&mut split_writer, patterns, &mut input_iter);
|
||||||
|
|
||||||
// consume the rest
|
// consume the rest, unless there was an error
|
||||||
input_iter.rewind_buffer();
|
if ret.is_ok() {
|
||||||
if let Some((_, line)) = input_iter.next() {
|
input_iter.rewind_buffer();
|
||||||
split_writer.new_writer()?;
|
if let Some((_, line)) = input_iter.next() {
|
||||||
split_writer.writeln(&line?)?;
|
split_writer.new_writer()?;
|
||||||
for (_, line) in input_iter {
|
|
||||||
split_writer.writeln(&line?)?;
|
split_writer.writeln(&line?)?;
|
||||||
|
for (_, line) in input_iter {
|
||||||
|
split_writer.writeln(&line?)?;
|
||||||
|
}
|
||||||
|
split_writer.finish_split();
|
||||||
}
|
}
|
||||||
split_writer.finish_split();
|
|
||||||
}
|
}
|
||||||
// delete files on error by default
|
// delete files on error by default
|
||||||
if ret.is_err() && !options.keep_files {
|
if ret.is_err() && !options.keep_files {
|
||||||
|
|
|
@ -575,7 +575,7 @@ fn test_skip_to_match_context_underflow() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
ucmd.args(&["numbers50.txt", "%5%-10"])
|
ucmd.args(&["numbers50.txt", "%5%-10"])
|
||||||
.fails()
|
.fails()
|
||||||
.stdout_is("141\n")
|
.stdout_is("")
|
||||||
.stderr_is("csplit: '%5%-10': line number out of range\n");
|
.stderr_is("csplit: '%5%-10': line number out of range\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
|
@ -586,14 +586,13 @@ fn test_skip_to_match_context_underflow() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
ucmd.args(&["numbers50.txt", "%5%-10", "-k"])
|
ucmd.args(&["numbers50.txt", "%5%-10", "-k"])
|
||||||
.fails()
|
.fails()
|
||||||
.stdout_is("141\n")
|
.stdout_is("")
|
||||||
.stderr_is("csplit: '%5%-10': line number out of range\n");
|
.stderr_is("csplit: '%5%-10': line number out of range\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
.expect("counting splits")
|
.expect("counting splits")
|
||||||
.count();
|
.count();
|
||||||
assert_eq!(count, 1);
|
assert_eq!(count, 0);
|
||||||
assert_eq!(at.read("xx00"), generate(1, 51));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1225,13 +1224,12 @@ fn test_corner_case4() {
|
||||||
assert_eq!(at.read("xx02"), generate(26, 51));
|
assert_eq!(at.read("xx02"), generate(26, 51));
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: differs from gnu's output: the empty split is not written
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_up_to_match_context_underflow() {
|
fn test_up_to_match_context_underflow() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
ucmd.args(&["numbers50.txt", "/5/-10"])
|
ucmd.args(&["numbers50.txt", "/5/-10"])
|
||||||
.fails()
|
.fails()
|
||||||
.stdout_is("0\n141\n")
|
.stdout_is("0\n")
|
||||||
.stderr_is("csplit: '/5/-10': line number out of range\n");
|
.stderr_is("csplit: '/5/-10': line number out of range\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
|
@ -1242,26 +1240,24 @@ fn test_up_to_match_context_underflow() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
ucmd.args(&["numbers50.txt", "/5/-10", "-k"])
|
ucmd.args(&["numbers50.txt", "/5/-10", "-k"])
|
||||||
.fails()
|
.fails()
|
||||||
.stdout_is("0\n141\n")
|
.stdout_is("0\n")
|
||||||
.stderr_is("csplit: '/5/-10': line number out of range\n");
|
.stderr_is("csplit: '/5/-10': line number out of range\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
.expect("counting splits")
|
.expect("counting splits")
|
||||||
.count();
|
.count();
|
||||||
assert_eq!(count, 2);
|
assert_eq!(count, 1);
|
||||||
assert_eq!(at.read("xx00"), "");
|
assert_eq!(at.read("xx00"), "");
|
||||||
assert_eq!(at.read("xx01"), generate(1, 51));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the offset is out of range because of the first pattern
|
// the offset is out of range because of the first pattern
|
||||||
// NOTE: output different than gnu's: the empty split is written but the rest of the input file is not
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_line_num_range_with_up_to_match1() {
|
fn test_line_num_range_with_up_to_match1() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
ucmd.args(&["numbers50.txt", "10", "/12/-5"])
|
ucmd.args(&["numbers50.txt", "10", "/12/-5"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_is("csplit: '/12/-5': line number out of range\n")
|
.stderr_is("csplit: '/12/-5': line number out of range\n")
|
||||||
.stdout_is("18\n0\n123\n");
|
.stdout_is("18\n0\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
.expect("there should be splits created")
|
.expect("there should be splits created")
|
||||||
|
@ -1272,26 +1268,24 @@ fn test_line_num_range_with_up_to_match1() {
|
||||||
ucmd.args(&["numbers50.txt", "10", "/12/-5", "-k"])
|
ucmd.args(&["numbers50.txt", "10", "/12/-5", "-k"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_is("csplit: '/12/-5': line number out of range\n")
|
.stderr_is("csplit: '/12/-5': line number out of range\n")
|
||||||
.stdout_is("18\n0\n123\n");
|
.stdout_is("18\n0\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
.expect("there should be splits created")
|
.expect("there should be splits created")
|
||||||
.count();
|
.count();
|
||||||
assert_eq!(count, 3);
|
assert_eq!(count, 2);
|
||||||
assert_eq!(at.read("xx00"), generate(1, 10));
|
assert_eq!(at.read("xx00"), generate(1, 10));
|
||||||
assert_eq!(at.read("xx01"), "");
|
assert_eq!(at.read("xx01"), "");
|
||||||
assert_eq!(at.read("xx02"), generate(10, 51));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the offset is out of range because more lines are needed than physically available
|
// the offset is out of range because more lines are needed than physically available
|
||||||
// NOTE: output different than gnu's: the empty split is not written but the rest of the input file is
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_line_num_range_with_up_to_match2() {
|
fn test_line_num_range_with_up_to_match2() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
ucmd.args(&["numbers50.txt", "10", "/12/-15"])
|
ucmd.args(&["numbers50.txt", "10", "/12/-15"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_is("csplit: '/12/-15': line number out of range\n")
|
.stderr_is("csplit: '/12/-15': line number out of range\n")
|
||||||
.stdout_is("18\n0\n123\n");
|
.stdout_is("18\n0\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
.expect("there should be splits created")
|
.expect("there should be splits created")
|
||||||
|
@ -1302,15 +1296,14 @@ fn test_line_num_range_with_up_to_match2() {
|
||||||
ucmd.args(&["numbers50.txt", "10", "/12/-15", "-k"])
|
ucmd.args(&["numbers50.txt", "10", "/12/-15", "-k"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_is("csplit: '/12/-15': line number out of range\n")
|
.stderr_is("csplit: '/12/-15': line number out of range\n")
|
||||||
.stdout_is("18\n0\n123\n");
|
.stdout_is("18\n0\n");
|
||||||
|
|
||||||
let count = glob(&at.plus_as_string("xx*"))
|
let count = glob(&at.plus_as_string("xx*"))
|
||||||
.expect("there should be splits created")
|
.expect("there should be splits created")
|
||||||
.count();
|
.count();
|
||||||
assert_eq!(count, 3);
|
assert_eq!(count, 2);
|
||||||
assert_eq!(at.read("xx00"), generate(1, 10));
|
assert_eq!(at.read("xx00"), generate(1, 10));
|
||||||
assert_eq!(at.read("xx01"), "");
|
assert_eq!(at.read("xx01"), "");
|
||||||
assert_eq!(at.read("xx02"), generate(10, 51));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: output different than gnu's: the pattern /10/ is matched but should not
|
// NOTE: output different than gnu's: the pattern /10/ is matched but should not
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue