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

Merge pull request #4266 from cakebaker/csplit_rename_var_and_simplify_format_strings

csplit: rename var to simplify format strings
This commit is contained in:
Sylvestre Ledru 2023-01-07 16:49:44 +01:00 committed by GitHub
commit 3033d017b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -55,7 +55,7 @@ impl SplitName {
let all = captures.name("ALL").unwrap(); let all = captures.name("ALL").unwrap();
let before = custom[0..all.start()].to_owned(); let before = custom[0..all.start()].to_owned();
let after = custom[all.end()..].to_owned(); let after = custom[all.end()..].to_owned();
let n_digits = match captures.name("WIDTH") { let width = match captures.name("WIDTH") {
None => 0, None => 0,
Some(m) => m.as_str().parse::<usize>().unwrap(), Some(m) => m.as_str().parse::<usize>().unwrap(),
}; };
@ -82,47 +82,19 @@ impl SplitName {
*/ */
// decimal // decimal
("0", "d" | "i" | "u") => Box::new(move |n: usize| -> String { ("0", "d" | "i" | "u") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:0width$}{}", prefix, before, n, after)
"{}{}{:0width$}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// octal // octal
("0", "o") => Box::new(move |n: usize| -> String { ("0", "o") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:0width$o}{}", prefix, before, n, after)
"{}{}{:0width$o}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// lower hexadecimal // lower hexadecimal
("0", "x") => Box::new(move |n: usize| -> String { ("0", "x") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:0width$x}{}", prefix, before, n, after)
"{}{}{:0width$x}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// upper hexadecimal // upper hexadecimal
("0", "X") => Box::new(move |n: usize| -> String { ("0", "X") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:0width$X}{}", prefix, before, n, after)
"{}{}{:0width$X}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
/* /*
@ -130,36 +102,15 @@ impl SplitName {
*/ */
// octal // octal
("#", "o") => Box::new(move |n: usize| -> String { ("#", "o") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:>#width$o}{}", prefix, before, n, after)
"{}{}{:>#width$o}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// lower hexadecimal // lower hexadecimal
("#", "x") => Box::new(move |n: usize| -> String { ("#", "x") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:>#width$x}{}", prefix, before, n, after)
"{}{}{:>#width$x}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// upper hexadecimal // upper hexadecimal
("#", "X") => Box::new(move |n: usize| -> String { ("#", "X") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:>#width$X}{}", prefix, before, n, after)
"{}{}{:>#width$X}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
/* /*
@ -167,47 +118,19 @@ impl SplitName {
*/ */
// decimal // decimal
("-", "d" | "i" | "u") => Box::new(move |n: usize| -> String { ("-", "d" | "i" | "u") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:<#width$}{}", prefix, before, n, after)
"{}{}{:<#width$}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// octal // octal
("-", "o") => Box::new(move |n: usize| -> String { ("-", "o") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:<#width$o}{}", prefix, before, n, after)
"{}{}{:<#width$o}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// lower hexadecimal // lower hexadecimal
("-", "x") => Box::new(move |n: usize| -> String { ("-", "x") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:<#width$x}{}", prefix, before, n, after)
"{}{}{:<#width$x}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
// upper hexadecimal // upper hexadecimal
("-", "X") => Box::new(move |n: usize| -> String { ("-", "X") => Box::new(move |n: usize| -> String {
format!( format!("{}{}{:<#width$X}{}", prefix, before, n, after)
"{}{}{:<#width$X}{}",
prefix,
before,
n,
after,
width = n_digits
)
}), }),
_ => return Err(CsplitError::SuffixFormatIncorrect), _ => return Err(CsplitError::SuffixFormatIncorrect),