1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #2558 from fdanis-oss/fix-csplit-split_name

csplit: fix suffix support without flag
This commit is contained in:
Sylvestre Ledru 2021-08-13 01:34:30 +02:00 committed by GitHub
commit 01be7b356f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,7 +47,7 @@ impl SplitName {
}), }),
Some(custom) => { Some(custom) => {
let spec = let spec =
Regex::new(r"(?P<ALL>%(?P<FLAG>[0#-])(?P<WIDTH>\d+)?(?P<TYPE>[diuoxX]))") Regex::new(r"(?P<ALL>%((?P<FLAG>[0#-])(?P<WIDTH>\d+)?)?(?P<TYPE>[diuoxX]))")
.unwrap(); .unwrap();
let mut captures_iter = spec.captures_iter(&custom); let mut captures_iter = spec.captures_iter(&custom);
let custom_fn: Box<dyn Fn(usize) -> String> = match captures_iter.next() { let custom_fn: Box<dyn Fn(usize) -> String> = match captures_iter.next() {
@ -60,6 +60,21 @@ impl SplitName {
Some(m) => m.as_str().parse::<usize>().unwrap(), Some(m) => m.as_str().parse::<usize>().unwrap(),
}; };
match (captures.name("FLAG"), captures.name("TYPE")) { match (captures.name("FLAG"), captures.name("TYPE")) {
(None, Some(ref t)) => match t.as_str() {
"d" | "i" | "u" => Box::new(move |n: usize| -> String {
format!("{}{}{}{}", prefix, before, n, after)
}),
"o" => Box::new(move |n: usize| -> String {
format!("{}{}{:o}{}", prefix, before, n, after)
}),
"x" => Box::new(move |n: usize| -> String {
format!("{}{}{:x}{}", prefix, before, n, after)
}),
"X" => Box::new(move |n: usize| -> String {
format!("{}{}{:X}{}", prefix, before, n, after)
}),
_ => return Err(CsplitError::SuffixFormatIncorrect),
},
(Some(ref f), Some(ref t)) => { (Some(ref f), Some(ref t)) => {
match (f.as_str(), t.as_str()) { match (f.as_str(), t.as_str()) {
/* /*
@ -276,6 +291,12 @@ mod tests {
assert_eq!(split_name.get(2), "xx00002"); assert_eq!(split_name.get(2), "xx00002");
} }
#[test]
fn no_padding_decimal() {
let split_name = SplitName::new(None, Some(String::from("cst-%d-")), None).unwrap();
assert_eq!(split_name.get(2), "xxcst-2-");
}
#[test] #[test]
fn zero_padding_decimal1() { fn zero_padding_decimal1() {
let split_name = SplitName::new(None, Some(String::from("cst-%03d-")), None).unwrap(); let split_name = SplitName::new(None, Some(String::from("cst-%03d-")), None).unwrap();