From a4955b4e06c04445d74490517c119107ae2d30b7 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sun, 16 Jan 2022 10:41:52 -0500 Subject: [PATCH] split: add support for -x option (hex suffixes) Add support for the `-x` command-line option to `split`. This option causes `split` to produce filenames with hexadecimal suffixes instead of the default alphabetic suffixes. --- src/uu/split/src/split.rs | 11 +++++++++ tests/by-util/test_split.rs | 24 ++++++++++++++++++- .../split/twohundredfortyonebytes.txt | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/split/twohundredfortyonebytes.txt diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index e9dab1725..2c344f4d3 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -32,6 +32,7 @@ static OPT_ADDITIONAL_SUFFIX: &str = "additional-suffix"; static OPT_FILTER: &str = "filter"; static OPT_NUMBER: &str = "number"; static OPT_NUMERIC_SUFFIXES: &str = "numeric-suffixes"; +static OPT_HEX_SUFFIXES: &str = "hex-suffixes"; static OPT_SUFFIX_LENGTH: &str = "suffix-length"; static OPT_DEFAULT_SUFFIX_LENGTH: &str = "0"; static OPT_VERBOSE: &str = "verbose"; @@ -140,6 +141,14 @@ pub fn uu_app<'a>() -> App<'a> { .default_value(OPT_DEFAULT_SUFFIX_LENGTH) .help("use suffixes of length N (default 2)"), ) + .arg( + Arg::new(OPT_HEX_SUFFIXES) + .short('x') + .long(OPT_HEX_SUFFIXES) + .takes_value(true) + .default_missing_value("0") + .help("use hex suffixes starting at 0, not alphabetic"), + ) .arg( Arg::new(OPT_VERBOSE) .long(OPT_VERBOSE) @@ -245,6 +254,8 @@ impl Strategy { fn suffix_type_from(matches: &ArgMatches) -> SuffixType { if matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0 { SuffixType::NumericDecimal + } else if matches.occurrences_of(OPT_HEX_SUFFIXES) > 0 { + SuffixType::NumericHexadecimal } else { SuffixType::Alphabetic } diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index 0291d1f4a..e30e29acc 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -2,7 +2,7 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore xzaaa sixhundredfiftyonebytes ninetyonebytes asciilowercase fghij klmno pqrst uvwxyz fivelines +// spell-checker:ignore xzaaa sixhundredfiftyonebytes ninetyonebytes asciilowercase fghij klmno pqrst uvwxyz fivelines twohundredfortyonebytes extern crate rand; extern crate regex; @@ -409,6 +409,28 @@ fn test_numeric_dynamic_suffix_length() { assert_eq!(file_read(&at, "x9000"), "a"); } +#[test] +fn test_hex_dynamic_suffix_length() { + let (at, mut ucmd) = at_and_ucmd!(); + // Split into chunks of one byte each, use hexadecimal digits + // instead of letters as file suffixes. + // + // The input file has (16^2) - 16 + 1 = 241 bytes. This is just + // enough to force `split` to dynamically increase the length of + // the filename for the very last chunk. + // + // x00, x01, x02, ..., xed, xee, xef, xf000 + // + ucmd.args(&["-x", "-b", "1", "twohundredfortyonebytes.txt"]) + .succeeds(); + for i in 0..240 { + let filename = format!("x{:02x}", i); + let contents = file_read(&at, &filename); + assert_eq!(contents, "a"); + } + assert_eq!(file_read(&at, "xf000"), "a"); +} + #[test] fn test_suffixes_exhausted() { new_ucmd!() diff --git a/tests/fixtures/split/twohundredfortyonebytes.txt b/tests/fixtures/split/twohundredfortyonebytes.txt new file mode 100644 index 000000000..10a53a61c --- /dev/null +++ b/tests/fixtures/split/twohundredfortyonebytes.txt @@ -0,0 +1 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \ No newline at end of file