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

stat.rs: Refactor to remove #[allow(clippy::cognitive_complexity)] #5881 (#5924)

* stat.rs: Refactor to remove #[allow(clippy::cognitive_complexity)] #5881

* remvoe Vec when ruturing  stat.rs: Refactor to remove #[allow(clippy::cognitive_complexity)] #5881

* formmating issue  stat.rs: Refactor to remove #[allow(clippy::cognitive_complexity)] #5881

* cakebaker suggestion stat.rs: Refactor to remove #[allow(clippy::cognitive_complexity)] #5881

* sytle and lint issue  stat.rs: Refactor to remove #[allow(clippy::cognitive_complexity)] #5881

---------

Co-authored-by: biplab5464 <biplab5464@outlook.com>
This commit is contained in:
Biplab Mochan Gartia 2024-02-01 20:50:32 +05:30 committed by GitHub
parent d53077160f
commit 4a7dfa7904
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -404,32 +404,27 @@ fn print_unsigned_hex(
} }
impl Stater { impl Stater {
#[allow(clippy::cognitive_complexity)] fn handle_percent_case(
fn generate_tokens(format_str: &str, use_printf: bool) -> UResult<Vec<Token>> { chars: &[char],
let mut tokens = Vec::new(); i: &mut usize,
let bound = format_str.len(); bound: usize,
let chars = format_str.chars().collect::<Vec<char>>(); format_str: &str,
let mut i = 0; ) -> UResult<Token> {
while i < bound { let old = *i;
match chars[i] {
'%' => {
let old = i;
i += 1; *i += 1;
if i >= bound { if *i >= bound {
tokens.push(Token::Char('%')); return Ok(Token::Char('%'));
continue;
} }
if chars[i] == '%' { if chars[*i] == '%' {
tokens.push(Token::Char('%')); *i += 1;
i += 1; return Ok(Token::Char('%'));
continue;
} }
let mut flag = Flags::default(); let mut flag = Flags::default();
while i < bound { while *i < bound {
match chars[i] { match chars[*i] {
'#' => flag.alter = true, '#' => flag.alter = true,
'0' => flag.zero = true, '0' => flag.zero = true,
'-' => flag.left = true, '-' => flag.left = true,
@ -439,13 +434,13 @@ impl Stater {
'I' => unimplemented!(), 'I' => unimplemented!(),
_ => break, _ => break,
} }
i += 1; *i += 1;
} }
check_bound(format_str, bound, old, i)?; check_bound(format_str, bound, old, *i)?;
let mut width = 0; let mut width = 0;
let mut precision = None; let mut precision = None;
let mut j = i; let mut j = *i;
if let Some((field_width, offset)) = format_str[j..].scan_num::<usize>() { if let Some((field_width, offset)) = format_str[j..].scan_num::<usize>() {
width = field_width; width = field_width;
@ -469,57 +464,77 @@ impl Stater {
check_bound(format_str, bound, old, j)?; check_bound(format_str, bound, old, j)?;
} }
i = j; *i = j;
tokens.push(Token::Directive { Ok(Token::Directive {
width, width,
flag, flag,
precision, precision,
format: chars[i], format: chars[*i],
}); })
} }
'\\' => {
if use_printf { fn handle_escape_sequences(
i += 1; chars: &[char],
if i >= bound { i: &mut usize,
bound: usize,
format_str: &str,
) -> Token {
*i += 1;
if *i >= bound {
show_warning!("backslash at end of format"); show_warning!("backslash at end of format");
tokens.push(Token::Char('\\')); return Token::Char('\\');
continue;
} }
match chars[i] { match chars[*i] {
'x' if i + 1 < bound => { 'x' if *i + 1 < bound => {
if let Some((c, offset)) = format_str[i + 1..].scan_char(16) { if let Some((c, offset)) = format_str[*i + 1..].scan_char(16) {
tokens.push(Token::Char(c)); *i += offset;
i += offset; Token::Char(c)
} else { } else {
show_warning!("unrecognized escape '\\x'"); show_warning!("unrecognized escape '\\x'");
tokens.push(Token::Char('x')); Token::Char('x')
} }
} }
'0'..='7' => { '0'..='7' => {
let (c, offset) = format_str[i..].scan_char(8).unwrap(); let (c, offset) = format_str[*i..].scan_char(8).unwrap();
tokens.push(Token::Char(c)); *i += offset - 1;
i += offset - 1; Token::Char(c)
} }
'"' => tokens.push(Token::Char('"')), '"' => Token::Char('"'),
'\\' => tokens.push(Token::Char('\\')), '\\' => Token::Char('\\'),
'a' => tokens.push(Token::Char('\x07')), 'a' => Token::Char('\x07'),
'b' => tokens.push(Token::Char('\x08')), 'b' => Token::Char('\x08'),
'e' => tokens.push(Token::Char('\x1B')), 'e' => Token::Char('\x1B'),
'f' => tokens.push(Token::Char('\x0C')), 'f' => Token::Char('\x0C'),
'n' => tokens.push(Token::Char('\n')), 'n' => Token::Char('\n'),
'r' => tokens.push(Token::Char('\r')), 'r' => Token::Char('\r'),
't' => tokens.push(Token::Char('\t')), 't' => Token::Char('\t'),
'v' => tokens.push(Token::Char('\x0B')), 'v' => Token::Char('\x0B'),
c => { c => {
show_warning!("unrecognized escape '\\{}'", c); show_warning!("unrecognized escape '\\{}'", c);
tokens.push(Token::Char(c)); Token::Char(c)
} }
} }
}
fn generate_tokens(format_str: &str, use_printf: bool) -> UResult<Vec<Token>> {
let mut tokens = Vec::new();
let bound = format_str.len();
let chars = format_str.chars().collect::<Vec<char>>();
let mut i = 0;
while i < bound {
match chars[i] {
'%' => tokens.push(Self::handle_percent_case(
&chars, &mut i, bound, format_str,
)?),
'\\' => {
if use_printf {
tokens.push(Self::handle_escape_sequences(
&chars, &mut i, bound, format_str,
));
} else { } else {
tokens.push(Token::Char('\\')); tokens.push(Token::Char('\\'));
} }
} }
c => tokens.push(Token::Char(c)), c => tokens.push(Token::Char(c)),
} }
i += 1; i += 1;