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

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.
This commit is contained in:
Jeffrey Finkelstein 2022-01-16 10:41:52 -05:00
parent 494dc7ec57
commit a4955b4e06
3 changed files with 35 additions and 1 deletions

View file

@ -32,6 +32,7 @@ static OPT_ADDITIONAL_SUFFIX: &str = "additional-suffix";
static OPT_FILTER: &str = "filter"; static OPT_FILTER: &str = "filter";
static OPT_NUMBER: &str = "number"; static OPT_NUMBER: &str = "number";
static OPT_NUMERIC_SUFFIXES: &str = "numeric-suffixes"; static OPT_NUMERIC_SUFFIXES: &str = "numeric-suffixes";
static OPT_HEX_SUFFIXES: &str = "hex-suffixes";
static OPT_SUFFIX_LENGTH: &str = "suffix-length"; static OPT_SUFFIX_LENGTH: &str = "suffix-length";
static OPT_DEFAULT_SUFFIX_LENGTH: &str = "0"; static OPT_DEFAULT_SUFFIX_LENGTH: &str = "0";
static OPT_VERBOSE: &str = "verbose"; static OPT_VERBOSE: &str = "verbose";
@ -140,6 +141,14 @@ pub fn uu_app<'a>() -> App<'a> {
.default_value(OPT_DEFAULT_SUFFIX_LENGTH) .default_value(OPT_DEFAULT_SUFFIX_LENGTH)
.help("use suffixes of length N (default 2)"), .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(
Arg::new(OPT_VERBOSE) Arg::new(OPT_VERBOSE)
.long(OPT_VERBOSE) .long(OPT_VERBOSE)
@ -245,6 +254,8 @@ impl Strategy {
fn suffix_type_from(matches: &ArgMatches) -> SuffixType { fn suffix_type_from(matches: &ArgMatches) -> SuffixType {
if matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0 { if matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0 {
SuffixType::NumericDecimal SuffixType::NumericDecimal
} else if matches.occurrences_of(OPT_HEX_SUFFIXES) > 0 {
SuffixType::NumericHexadecimal
} else { } else {
SuffixType::Alphabetic SuffixType::Alphabetic
} }

View file

@ -2,7 +2,7 @@
// * // *
// * For the full copyright and license information, please view the LICENSE // * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code. // * 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 rand;
extern crate regex; extern crate regex;
@ -409,6 +409,28 @@ fn test_numeric_dynamic_suffix_length() {
assert_eq!(file_read(&at, "x9000"), "a"); 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] #[test]
fn test_suffixes_exhausted() { fn test_suffixes_exhausted() {
new_ucmd!() new_ucmd!()

View file

@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa