1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #6114 from BenWiederhake/dev-csplit-repeated-args

csplit: Handle repeated args, fix remainder after error
This commit is contained in:
Terts Diepraam 2024-03-25 09:27:06 +01:00 committed by GitHub
commit e4a1455af5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 58 additions and 26 deletions

View file

@ -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 {
@ -585,6 +587,7 @@ pub fn uu_app() -> Command {
.version(crate_version!()) .version(crate_version!())
.about(ABOUT) .about(ABOUT)
.override_usage(format_usage(USAGE)) .override_usage(format_usage(USAGE))
.args_override_self(true)
.infer_long_args(true) .infer_long_args(true)
.arg( .arg(
Arg::new(options::SUFFIX_FORMAT) Arg::new(options::SUFFIX_FORMAT)

View file

@ -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
@ -1376,3 +1369,39 @@ fn no_such_file() {
.fails() .fails()
.stderr_contains("cannot access 'in': No such file or directory"); .stderr_contains("cannot access 'in': No such file or directory");
} }
#[test]
fn repeat_everything() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&[
"numbers50.txt",
"--suppress-matched",
"--suppress-matched",
"-kzsn", // spell-checker:disable-line
"2",
"-szkn3", // spell-checker:disable-line
"-b",
"%03d",
"-b%03x",
"-f",
"xxy_",
"-fxxz_", // spell-checker:disable-line
"/13/",
"9",
"{5}",
])
.fails()
.no_stdout()
.code_is(1)
.stderr_only("csplit: '9': line number out of range on repetition 5\n");
let count = glob(&at.plus_as_string("xx*"))
.expect("there should be some splits created")
.count();
assert_eq!(count, 6);
assert_eq!(at.read("xxz_000"), generate(1, 12 + 1));
assert_eq!(at.read("xxz_001"), generate(14, 17 + 1)); // FIXME: GNU starts at 15
assert_eq!(at.read("xxz_002"), generate(19, 26 + 1));
assert_eq!(at.read("xxz_003"), generate(28, 35 + 1));
assert_eq!(at.read("xxz_004"), generate(37, 44 + 1));
assert_eq!(at.read("xxz_005"), generate(46, 50 + 1));
}