mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
csplit: fix suffix support without flag
csplit fails when suffix has no flags:
$ csplit result.expected -f /tmp/EXPECT -b "%d" "/^## alternative ##$/" {*}
csplit: error: incorrect conversion specification in suffix
This is supported by original csplit
This commit is contained in:
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user