diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 87433e93793..0c179313553 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -439,9 +439,7 @@ impl SplitWriter<'_> { while let Some((ln, line)) = input_iter.next() { let line = line?; - let l = line - .strip_suffix(b"\r\n") - .unwrap_or_else(|| line.strip_suffix(b"\n").unwrap_or(&line)); + let l = line.strip_suffix(b"\n").unwrap_or(&line); if regex.is_match(l) { let mut next_line_suppress_matched = false; match (self.options.suppress_matched, offset) { @@ -495,9 +493,7 @@ impl SplitWriter<'_> { input_iter.set_size_of_buffer(offset_usize); while let Some((ln, line)) = input_iter.next() { let line = line?; - let l = line - .strip_suffix(b"\r\n") - .unwrap_or_else(|| line.strip_suffix(b"\n").unwrap_or(&line)); + let l = line.strip_suffix(b"\n").unwrap_or(&line); if regex.is_match(l) { if offset_usize > lines_in_split { self.finish_split()?; diff --git a/tests/by-util/test_csplit.rs b/tests/by-util/test_csplit.rs index 55f21141e2c..c75c8d7f5ec 100644 --- a/tests/by-util/test_csplit.rs +++ b/tests/by-util/test_csplit.rs @@ -131,6 +131,35 @@ fn test_binary_input_regex() { } } +#[test] +fn test_regex_does_not_ignore_carriage_return() { + for (pattern, stdout) in [("/^---$/", "9\n"), ("/^---$/-1", "9\n"), ("%^---$%", "")] { + let (at, mut ucmd) = at_and_ucmd!(); + ucmd.args(&["-", pattern]) + .pipe_in("x\n---\r\ny\n") + .fails_with_code(1) + .stdout_is(stdout) + .stderr_is(format!("csplit: '{pattern}': match not found\n")); + + assert!(!at.file_exists("xx00")); + } +} + +#[test] +fn test_regex_matches_carriage_return() { + let input = "x\n---\r\ny\n"; + for (pattern, split) in [("/^---.$/", 2), ("/^y$/-1", 2), ("/^---.$/+1", 7)] { + let (at, mut ucmd) = at_and_ucmd!(); + ucmd.args(&["-", pattern]) + .pipe_in(input) + .succeeds() + .stdout_only(format!("{split}\n{}\n", input.len() - split)); + + assert_eq!(at.read("xx00"), &input[..split]); + assert_eq!(at.read("xx01"), &input[split..]); + } +} + #[test] fn test_binary_input_skip_regex() { let (at, mut ucmd) = at_and_ucmd!();