mirror of
https://github.com/uutils/coreutils
synced 2025-01-08 11:19:03 +00:00
split: refactor handle_obsolete() function
This commit is contained in:
parent
2f35989ac3
commit
e79753c1cf
1 changed files with 40 additions and 33 deletions
|
@ -72,43 +72,50 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
/// `split -x300e -22 file` would mean `split -x -e -l 22 file` (last obsolete lines option wins)
|
/// `split -x300e -22 file` would mean `split -x -e -l 22 file` (last obsolete lines option wins)
|
||||||
/// following GNU `split` behavior
|
/// following GNU `split` behavior
|
||||||
fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {
|
fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {
|
||||||
let mut v: Vec<String> = vec![];
|
|
||||||
let mut obs_lines = None;
|
let mut obs_lines = None;
|
||||||
for arg in args.iter() {
|
let filtered_args = args
|
||||||
let slice = &arg;
|
.iter()
|
||||||
if slice.starts_with('-') && !slice.starts_with("--") {
|
.filter_map(|slice| {
|
||||||
// start of the short option string
|
if slice.starts_with('-') && !slice.starts_with("--") {
|
||||||
// extract numeric part and filter it out
|
// start of the short option string
|
||||||
let mut obs_lines_extracted: Vec<char> = vec![];
|
// extract numeric part and filter it out
|
||||||
let filtered_slice: Vec<char> = slice
|
let mut obs_lines_extracted: Vec<char> = vec![];
|
||||||
.chars()
|
let filtered_slice: Vec<char> = slice
|
||||||
.filter(|c| {
|
.chars()
|
||||||
if c.is_ascii_digit() {
|
.filter(|c| {
|
||||||
obs_lines_extracted.push(*c);
|
if c.is_ascii_digit() {
|
||||||
false
|
obs_lines_extracted.push(*c);
|
||||||
} else {
|
false
|
||||||
true
|
} else {
|
||||||
}
|
true
|
||||||
})
|
}
|
||||||
.collect();
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
if filtered_slice.get(1).is_some() {
|
if obs_lines_extracted.is_empty() {
|
||||||
// there were some short options in front of obsolete lines number
|
// no obsolete lines value found/extracted
|
||||||
// i.e. '-xd100' or similar
|
Some(slice.to_owned())
|
||||||
|
} else {
|
||||||
|
// obsolete lines value was extracted
|
||||||
|
obs_lines = Some(obs_lines_extracted.iter().collect());
|
||||||
|
if filtered_slice.get(1).is_some() {
|
||||||
|
// there were some short options in front of or after obsolete lines value
|
||||||
|
// i.e. '-xd100' or '-100de' or similar, which after extraction of obsolete lines value
|
||||||
|
// would look like '-xd' or '-de' or similar
|
||||||
|
// preserve it
|
||||||
|
Some(filtered_slice.iter().collect())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// not a short option
|
||||||
// preserve it
|
// preserve it
|
||||||
v.push(filtered_slice.iter().collect());
|
Some(slice.to_owned())
|
||||||
}
|
}
|
||||||
if !obs_lines_extracted.is_empty() {
|
})
|
||||||
// obsolete lines value was extracted
|
.collect();
|
||||||
obs_lines = Some(obs_lines_extracted.iter().collect());
|
(filtered_args, obs_lines)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// not a short option
|
|
||||||
// preserve it
|
|
||||||
v.push(arg.to_owned());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(v, obs_lines)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> Command {
|
pub fn uu_app() -> Command {
|
||||||
|
|
Loading…
Reference in a new issue