Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sylvestre opened this issue Jan 25, 2024 · 5 comments · Fixed by #5924
Closed

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

sylvestre opened this issue Jan 25, 2024 · 5 comments · Fixed by #5924
Labels

Comments

@sylvestre
Copy link
Contributor

sylvestre commented Jan 25, 2024

https:/uutils/coreutils/blob/main/src/uu/stat/src/stat.rs#L407
A potential way to do it:

'%' => tokens.append(&mut handle_percent_case(&chars, &mut i, bound, format_str)?),
'\\' => tokens.append(&mut handle_escape_sequences(&chars, &mut i, bound, format_str, use_printf)),
@biplab5464
Copy link
Contributor

Hi if i understand correctly i have to remove the macro #[allow(clippy::cognitive_complexity)]
move the logic to a function ?

@sylvestre
Copy link
Contributor Author

correct :)

@biplab5464
Copy link
Contributor

Thank you i am doing it

@biplab5464
Copy link
Contributor

biplab5464 commented Jan 28, 2024

fn handle_percent_case(
        chars: &Vec<char>,
        i: &mut usize,
        bound: usize,
        format_str: &str,
    ) -> UResult<Vec<Token>> {
        let old = *i;

        *i += 1;
        if *i >= bound {
            return Ok(vec![Token::Char('%')]);
        }
        if chars[*i] == '%' {
            *i += 1;
            return Ok(vec![Token::Char('%')]);
        }

        let mut flag = Flags::default();

        while *i < bound {
            match chars[*i] {
                '#' => flag.alter = true,
                '0' => flag.zero = true,
                '-' => flag.left = true,
                ' ' => flag.space = true,
                '+' => flag.sign = true,
                '\'' => flag.group = true,
                'I' => unimplemented!(),
                _ => break,
            }
            *i += 1;
        }
        check_bound(format_str, bound, old, *i)?;

        let mut width = 0;
        let mut precision = None;
        let mut j = *i;

        if let Some((field_width, offset)) = format_str[j..].scan_num::<usize>() {
            width = field_width;
            j += offset;
        }
        check_bound(format_str, bound, old, j)?;

        if chars[j] == '.' {
            j += 1;
            check_bound(format_str, bound, old, j)?;

            match format_str[j..].scan_num::<i32>() {
                Some((value, offset)) => {
                    if value >= 0 {
                        precision = Some(value as usize);
                    }
                    j += offset;
                }
                None => precision = Some(0),
            }
            check_bound(format_str, bound, old, j)?;
        }

        *i = j;
        Ok(vec![Token::Directive {
            width,
            flag,
            precision,
            format: chars[*i],
        }])
    }

    fn handle_escape_sequences(
        chars: &Vec<char>,
        i: &mut usize,
        bound: usize,
        format_str: &str,
        use_printf: bool,
    ) -> Vec<Token> {
        if use_printf {
            *i += 1;
            if *i >= bound {
                show_warning!("backslash at end of format");
                return vec![Token::Char('\\')];
            }
            //let mut tokens : Vec<Token> = Vec::new();
            match chars[*i] {
                'x' if *i + 1 < bound => {
                    if let Some((c, offset)) = format_str[*i + 1..].scan_char(16) {
                        *i += offset;
                        vec![Token::Char(c)]
                    } else {
                        show_warning!("unrecognized escape '\\x'");
                        vec![Token::Char('x')]
                    }
                }
                '0'..='7' => {
                    let (c, offset) = format_str[*i..].scan_char(8).unwrap();
                    *i += offset - 1;
                    vec![Token::Char(c)]
                }
                '"' => vec![Token::Char('"')],
                '\\' => vec![Token::Char('\\')],
                'a' => vec![Token::Char('\x07')],
                'b' => vec![Token::Char('\x08')],
                'e' => vec![Token::Char('\x1B')],
                'f' => vec![Token::Char('\x0C')],
                'n' => vec![Token::Char('\n')],
                'r' => vec![Token::Char('\r')],
                't' => vec![Token::Char('\t')],
                'v' => vec![Token::Char('\x0B')],
                c => {
                    show_warning!("unrecognized escape '\\{}'", c);
                    vec![Token::Char(c)]
                }
            }
            //tokens
        } else {
            vec![Token::Char('\\')]
        }
    }

    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.append(&mut Self::handle_percent_case(
                    &chars, &mut i, bound, format_str,
                )?),
                '\\' => tokens.append(&mut Self::handle_escape_sequences(
                    &chars, &mut i, bound, format_str, use_printf,
                )),
                c => tokens.push(Token::Char(c)),
            }
            i += 1;
        }
        if !use_printf && !format_str.ends_with('\n') {
            tokens.push(Token::Char('\n'));
        }
        Ok(tokens)
    }

Hi i have question, i have done as it was asked but
it is better if the function (handle_escape_sequences, handle_percent_case ) return " Token"
and when calling the function we used

 '%' => tokens.push(Self::handle_percent_case( &chars, &mut i, bound, format_str)?),
'\\' => tokens.push(Self::handle_escape_sequences(&chars, &mut i, bound, format_str, use_printf)),

or it is okay

biplab5464 pushed a commit to biplab5464/coreutils that referenced this issue Jan 28, 2024
@biplab5464
Copy link
Contributor

Hi @sylvestre, any comment on it

biplab5464 pushed a commit to biplab5464/coreutils that referenced this issue Jan 31, 2024
biplab5464 pushed a commit to biplab5464/coreutils that referenced this issue Jan 31, 2024
biplab5464 pushed a commit to biplab5464/coreutils that referenced this issue Feb 1, 2024
biplab5464 pushed a commit to biplab5464/coreutils that referenced this issue Feb 1, 2024
cakebaker pushed a commit that referenced this issue Feb 1, 2024
…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 <[email protected]>
ysthakur pushed a commit to ysthakur/coreutils that referenced this issue Feb 27, 2024
…utils#5881  (uutils#5924)

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

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

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

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

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

---------

Co-authored-by: biplab5464 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants