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

Various fixes before landing csplit

This commit is contained in:
Sylvestre Ledru 2020-12-27 15:33:20 +01:00
parent 89bf7a726e
commit 3a1eb1e05f
8 changed files with 33 additions and 35 deletions

View file

@ -289,9 +289,9 @@ Utilities
| Done | Semi-Done | To Do | | Done | Semi-Done | To Do |
|-----------|-----------|--------| |-----------|-----------|--------|
| arch | cp | chcon | | arch | cp | chcon |
| base32 | expr | csplit | | base32 | expr | dd |
| base64 | install | dd | | base64 | install | numfmt |
| basename | ls | numfmt | | basename | ls | stty |
| cat | more | pr | | cat | more | pr |
| chgrp | od (`--strings` and 128-bit data types missing) | runcon | | chgrp | od (`--strings` and 128-bit data types missing) | runcon |
| chmod | printf | stty | | chmod | printf | stty |
@ -299,9 +299,10 @@ Utilities
| chroot | split | | | chroot | split | |
| cksum | tail | | | cksum | tail | |
| comm | test | | | comm | test | |
| cut | date | | | csplit | date | |
| dircolors | join | | | cut | join | |
| dirname | df | | | dircolors | df | |
| dirname | | |
| du | | | | du | | |
| echo | | | | echo | | |
| env | | | | env | | |

View file

@ -21,6 +21,7 @@ failure_derive = "0.1.1"
regex = "1.0.0" regex = "1.0.0"
glob = "0.2.11" glob = "0.2.11"
uucore = { version=">=0.0.4", package="uucore", path="../../uucore", features=["entries", "fs"] } uucore = { version=">=0.0.4", package="uucore", path="../../uucore", features=["entries", "fs"] }
uucore_procs = { version=">=0.0.4", package="uucore_procs", path="../../uucore_procs" }
[[bin]] [[bin]]
name = "csplit" name = "csplit"

View file

@ -16,26 +16,26 @@ mod patterns;
*/ */
mod splitname; mod splitname;
mod patterns; mod patterns;
mod csplitError; mod csplit_error;
use crate::splitname::SplitName; use crate::splitname::SplitName;
use crate::csplitError::CsplitError; use crate::csplit_error::CsplitError;
//mod split_name; //mod split_name;
//mod csplit; //mod csplit;
static SYNTAX: &'static str = "[OPTION]... FILE PATTERN..."; static SYNTAX: &str = "[OPTION]... FILE PATTERN...";
static SUMMARY: &'static str = "split a file into sections determined by context lines"; static SUMMARY: &str = "split a file into sections determined by context lines";
static LONG_HELP: &'static str = "Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output."; static LONG_HELP: &str = "Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output.";
static SUFFIX_FORMAT_OPT: &'static str = "suffix-format"; static SUFFIX_FORMAT_OPT: &str = "suffix-format";
static SUPPRESS_MATCHED_OPT: &'static str = "suppress-matched"; static SUPPRESS_MATCHED_OPT: &str = "suppress-matched";
static DIGITS_OPT: &'static str = "digits"; static DIGITS_OPT: &str = "digits";
static PREFIX_OPT: &'static str = "prefix"; static PREFIX_OPT: &str = "prefix";
static KEEP_FILES_OPT: &'static str = "keep-files"; static KEEP_FILES_OPT: &str = "keep-files";
static QUIET_OPT: &'static str = "quiet"; static QUIET_OPT: &str = "quiet";
static ELIDE_EMPTY_FILES_OPT: &'static str = "elide-empty-files"; static ELIDE_EMPTY_FILES_OPT: &str = "elide-empty-files";
/// Command line options for csplit. /// Command line options for csplit.
pub struct CsplitOptions { pub struct CsplitOptions {
@ -250,7 +250,7 @@ impl<'a> SplitWriter<'a> {
Some(ref mut current_writer) => { Some(ref mut current_writer) => {
let bytes = line.as_bytes(); let bytes = line.as_bytes();
current_writer.write_all(bytes)?; current_writer.write_all(bytes)?;
current_writer.write(b"\n")?; current_writer.write_all(b"\n")?;
self.size += bytes.len() + 1; self.size += bytes.len() + 1;
} }
None => panic!("trying to write to a split that was not created"), None => panic!("trying to write to a split that was not created"),
@ -274,7 +274,7 @@ impl<'a> SplitWriter<'a> {
println!("{}", self.size); println!("{}", self.size);
} }
} }
return Ok(()); Ok(())
} }
/// Removes all the split files that were created. /// Removes all the split files that were created.
@ -325,10 +325,8 @@ impl<'a> SplitWriter<'a> {
ret = Ok(()); ret = Ok(());
break; break;
} else if ln + 1 == n { } else if ln + 1 == n {
if !self.options.suppress_matched { if !self.options.suppress_matched && input_iter.add_line_to_buffer(ln, l).is_some() {
if input_iter.add_line_to_buffer(ln, l).is_some() { panic!("the buffer is big enough to contain 1 line");
panic!("the buffer is big enough to contain 1 line");
}
} }
ret = Ok(()); ret = Ok(());
break; break;

View file

@ -1,2 +1 @@
uucore_procs::main!(uu_csplit); // spell-checker:ignore procs uucore uucore_procs::main!(uu_csplit); // spell-checker:ignore procs uucore

View file

@ -1,5 +1,5 @@
use regex::Regex; use regex::Regex;
use crate::csplitError::CsplitError; use crate::csplit_error::CsplitError;
/// The definition of a pattern to match on a line. /// The definition of a pattern to match on a line.
#[derive(Debug)] #[derive(Debug)]
@ -147,7 +147,7 @@ fn extract_patterns(args: &[String]) -> Result<Vec<Pattern>, CsplitError> {
}; };
patterns.push(Pattern::SkipToMatch(pattern, offset, execute_ntimes)); patterns.push(Pattern::SkipToMatch(pattern, offset, execute_ntimes));
} }
} else if let Some(line_number) = arg.parse::<usize>().ok() { } else if let Ok(line_number) = arg.parse::<usize>() {
patterns.push(Pattern::UpToLine(line_number, execute_ntimes)); patterns.push(Pattern::UpToLine(line_number, execute_ntimes));
} else { } else {
return Err(CsplitError::InvalidPattern(arg.to_string())); return Err(CsplitError::InvalidPattern(arg.to_string()));
@ -319,7 +319,7 @@ mod tests {
fn line_number_zero() { fn line_number_zero() {
let patterns = vec![Pattern::UpToLine(0, ExecutePattern::Times(1))]; let patterns = vec![Pattern::UpToLine(0, ExecutePattern::Times(1))];
match validate_line_numbers(&patterns) { match validate_line_numbers(&patterns) {
Err(::CsplitError::LineNumberIsZero) => (), Err(CsplitError::LineNumberIsZero) => (),
_ => panic!("expected LineNumberIsZero error"), _ => panic!("expected LineNumberIsZero error"),
} }
} }
@ -328,7 +328,7 @@ mod tests {
fn line_number_smaller_than_previous() { fn line_number_smaller_than_previous() {
let input: Vec<String> = vec!["10".to_string(), "5".to_string()]; let input: Vec<String> = vec!["10".to_string(), "5".to_string()];
match get_patterns(input.as_slice()) { match get_patterns(input.as_slice()) {
Err(::CsplitError::LineNumberSmallerThanPrevious(5, 10)) => (), Err(CsplitError::LineNumberSmallerThanPrevious(5, 10)) => (),
_ => panic!("expected LineNumberSmallerThanPrevious error"), _ => panic!("expected LineNumberSmallerThanPrevious error"),
} }
} }
@ -337,7 +337,7 @@ mod tests {
fn line_number_smaller_than_previous_separate() { fn line_number_smaller_than_previous_separate() {
let input: Vec<String> = vec!["10".to_string(), "/20/".to_string(), "5".to_string()]; let input: Vec<String> = vec!["10".to_string(), "/20/".to_string(), "5".to_string()];
match get_patterns(input.as_slice()) { match get_patterns(input.as_slice()) {
Err(::CsplitError::LineNumberSmallerThanPrevious(5, 10)) => (), Err(CsplitError::LineNumberSmallerThanPrevious(5, 10)) => (),
_ => panic!("expected LineNumberSmallerThanPrevious error"), _ => panic!("expected LineNumberSmallerThanPrevious error"),
} }
} }
@ -346,7 +346,7 @@ mod tests {
fn line_number_zero_separate() { fn line_number_zero_separate() {
let input: Vec<String> = vec!["10".to_string(), "/20/".to_string(), "0".to_string()]; let input: Vec<String> = vec!["10".to_string(), "/20/".to_string(), "0".to_string()];
match get_patterns(input.as_slice()) { match get_patterns(input.as_slice()) {
Err(::CsplitError::LineNumberIsZero) => (), Err(CsplitError::LineNumberIsZero) => (),
_ => panic!("expected LineNumberIsZero error"), _ => panic!("expected LineNumberIsZero error"),
} }
} }

View file

@ -1,7 +1,6 @@
use regex::Regex; use regex::Regex;
//mod csplit; use crate::csplit_error::CsplitError;
use crate::CsplitError;
/// Computes the filename of a split, taking into consideration a possible user-defined suffix /// Computes the filename of a split, taking into consideration a possible user-defined suffix
/// format. /// format.

View file

@ -323,8 +323,8 @@ fn test_install_copy_file() {
assert!(at.file_exists(file2)); assert!(at.file_exists(file2));
} }
#[test] /*#[test]
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]*/
fn test_install_target_file_dev_null() { fn test_install_target_file_dev_null() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let file1 = "/dev/null"; let file1 = "/dev/null";