mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 15:22:38 +00:00
head: remove clippy::cognitive_complexity by moving some content in a function (#5366)
* head: remove a clippy::cognitive_complexity by moving some content into a function * Remove duplicate comment
This commit is contained in:
parent
91c8724fd5
commit
3e1d3caceb
1 changed files with 69 additions and 60 deletions
|
@ -13,7 +13,6 @@ pub enum ParseError {
|
||||||
}
|
}
|
||||||
/// Parses obsolete syntax
|
/// Parses obsolete syntax
|
||||||
/// head -NUM\[kmzv\] // spell-checker:disable-line
|
/// head -NUM\[kmzv\] // spell-checker:disable-line
|
||||||
#[allow(clippy::cognitive_complexity)]
|
|
||||||
pub fn parse_obsolete(src: &str) -> Option<Result<impl Iterator<Item = OsString>, ParseError>> {
|
pub fn parse_obsolete(src: &str) -> Option<Result<impl Iterator<Item = OsString>, ParseError>> {
|
||||||
let mut chars = src.char_indices();
|
let mut chars = src.char_indices();
|
||||||
if let Some((_, '-')) = chars.next() {
|
if let Some((_, '-')) = chars.next() {
|
||||||
|
@ -30,65 +29,7 @@ pub fn parse_obsolete(src: &str) -> Option<Result<impl Iterator<Item = OsString>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if has_num {
|
if has_num {
|
||||||
match src[1..=num_end].parse::<usize>() {
|
process_num_block(&src[1..=num_end], last_char, &mut chars)
|
||||||
Ok(num) => {
|
|
||||||
let mut quiet = false;
|
|
||||||
let mut verbose = false;
|
|
||||||
let mut zero_terminated = false;
|
|
||||||
let mut multiplier = None;
|
|
||||||
let mut c = last_char;
|
|
||||||
loop {
|
|
||||||
// not that here, we only match lower case 'k', 'c', and 'm'
|
|
||||||
match c {
|
|
||||||
// we want to preserve order
|
|
||||||
// this also saves us 1 heap allocation
|
|
||||||
'q' => {
|
|
||||||
quiet = true;
|
|
||||||
verbose = false;
|
|
||||||
}
|
|
||||||
'v' => {
|
|
||||||
verbose = true;
|
|
||||||
quiet = false;
|
|
||||||
}
|
|
||||||
'z' => zero_terminated = true,
|
|
||||||
'c' => multiplier = Some(1),
|
|
||||||
'b' => multiplier = Some(512),
|
|
||||||
'k' => multiplier = Some(1024),
|
|
||||||
'm' => multiplier = Some(1024 * 1024),
|
|
||||||
'\0' => {}
|
|
||||||
_ => return Some(Err(ParseError::Syntax)),
|
|
||||||
}
|
|
||||||
if let Some((_, next)) = chars.next() {
|
|
||||||
c = next;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut options = Vec::new();
|
|
||||||
if quiet {
|
|
||||||
options.push(OsString::from("-q"));
|
|
||||||
}
|
|
||||||
if verbose {
|
|
||||||
options.push(OsString::from("-v"));
|
|
||||||
}
|
|
||||||
if zero_terminated {
|
|
||||||
options.push(OsString::from("-z"));
|
|
||||||
}
|
|
||||||
if let Some(n) = multiplier {
|
|
||||||
options.push(OsString::from("-c"));
|
|
||||||
let num = match num.checked_mul(n) {
|
|
||||||
Some(n) => n,
|
|
||||||
None => return Some(Err(ParseError::Overflow)),
|
|
||||||
};
|
|
||||||
options.push(OsString::from(format!("{num}")));
|
|
||||||
} else {
|
|
||||||
options.push(OsString::from("-n"));
|
|
||||||
options.push(OsString::from(format!("{num}")));
|
|
||||||
}
|
|
||||||
Some(Ok(options.into_iter()))
|
|
||||||
}
|
|
||||||
Err(_) => Some(Err(ParseError::Overflow)),
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -96,6 +37,74 @@ pub fn parse_obsolete(src: &str) -> Option<Result<impl Iterator<Item = OsString>
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Processes the numeric block of the input string to generate the appropriate options.
|
||||||
|
fn process_num_block(
|
||||||
|
src: &str,
|
||||||
|
last_char: char,
|
||||||
|
chars: &mut std::str::CharIndices,
|
||||||
|
) -> Option<Result<impl Iterator<Item = OsString>, ParseError>> {
|
||||||
|
match src.parse::<usize>() {
|
||||||
|
Ok(num) => {
|
||||||
|
let mut quiet = false;
|
||||||
|
let mut verbose = false;
|
||||||
|
let mut zero_terminated = false;
|
||||||
|
let mut multiplier = None;
|
||||||
|
let mut c = last_char;
|
||||||
|
loop {
|
||||||
|
// note that here, we only match lower case 'k', 'c', and 'm'
|
||||||
|
match c {
|
||||||
|
// we want to preserve order
|
||||||
|
// this also saves us 1 heap allocation
|
||||||
|
'q' => {
|
||||||
|
quiet = true;
|
||||||
|
verbose = false;
|
||||||
|
}
|
||||||
|
'v' => {
|
||||||
|
verbose = true;
|
||||||
|
quiet = false;
|
||||||
|
}
|
||||||
|
'z' => zero_terminated = true,
|
||||||
|
'c' => multiplier = Some(1),
|
||||||
|
'b' => multiplier = Some(512),
|
||||||
|
'k' => multiplier = Some(1024),
|
||||||
|
'm' => multiplier = Some(1024 * 1024),
|
||||||
|
'\0' => {}
|
||||||
|
_ => return Some(Err(ParseError::Syntax)),
|
||||||
|
}
|
||||||
|
if let Some((_, next)) = chars.next() {
|
||||||
|
c = next;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut options = Vec::new();
|
||||||
|
if quiet {
|
||||||
|
options.push(OsString::from("-q"));
|
||||||
|
}
|
||||||
|
if verbose {
|
||||||
|
options.push(OsString::from("-v"));
|
||||||
|
}
|
||||||
|
if zero_terminated {
|
||||||
|
options.push(OsString::from("-z"));
|
||||||
|
}
|
||||||
|
if let Some(n) = multiplier {
|
||||||
|
options.push(OsString::from("-c"));
|
||||||
|
let num = match num.checked_mul(n) {
|
||||||
|
Some(n) => n,
|
||||||
|
None => return Some(Err(ParseError::Overflow)),
|
||||||
|
};
|
||||||
|
options.push(OsString::from(format!("{num}")));
|
||||||
|
} else {
|
||||||
|
options.push(OsString::from("-n"));
|
||||||
|
options.push(OsString::from(format!("{num}")));
|
||||||
|
}
|
||||||
|
Some(Ok(options.into_iter()))
|
||||||
|
}
|
||||||
|
Err(_) => Some(Err(ParseError::Overflow)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Parses an -c or -n argument,
|
/// Parses an -c or -n argument,
|
||||||
/// the bool specifies whether to read from the end
|
/// the bool specifies whether to read from the end
|
||||||
pub fn parse_num(src: &str) -> Result<(u64, bool), ParseSizeError> {
|
pub fn parse_num(src: &str) -> Result<(u64, bool), ParseSizeError> {
|
||||||
|
|
Loading…
Reference in a new issue