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 has unnecessary macros #3920

Closed
tertsdiepraam opened this issue Sep 10, 2022 · 2 comments · Fixed by #3923
Closed

stat has unnecessary macros #3920

tertsdiepraam opened this issue Sep 10, 2022 · 2 comments · Fixed by #3923
Labels

Comments

@tertsdiepraam
Copy link
Member

stat seems to have a lot of macros that could easily be refactored into functions to aid readability and compile times. See the details block below for the macros.

Macros in stat
macro_rules! check_bound {
    ($str: ident, $bound:expr, $beg: expr, $end: expr) => {
        if $end >= $bound {
            return Err(USimpleError::new(
                1,
                format!("{}: invalid directive", $str[$beg..$end].quote()),
            ));
        }
    };
}
macro_rules! fill_string {
    ($str: ident, $c: expr, $cnt: expr) => {
        iter::repeat($c)
            .take($cnt)
            .map(|c| $str.push(c))
            .all(|_| true)
    };
}
macro_rules! extend_digits {
    ($str: expr, $min: expr) => {
        if $min > $str.len() {
            let mut pad = String::with_capacity($min);
            fill_string!(pad, '0', $min - $str.len());
            pad.push_str($str);
            pad.into()
        } else {
            $str.into()
        }
    };
}
macro_rules! pad_and_print {
    ($result: ident, $str: ident, $left: expr, $width: expr, $padding: expr) => {
        if $str.len() < $width {
            if $left {
                $result.push_str($str.as_ref());
                fill_string!($result, $padding, $width - $str.len());
            } else {
                fill_string!($result, $padding, $width - $str.len());
                $result.push_str($str.as_ref());
            }
        } else {
            $result.push_str($str.as_ref());
        }
        print!("{}", $result);
    };
}
macro_rules! print_adjusted {
    ($str: ident, $left: expr, $width: expr, $padding: expr) => {
        let field_width = cmp::max($width, $str.len());
        let mut result = String::with_capacity(field_width);
        pad_and_print!(result, $str, $left, field_width, $padding);
    };
    ($str: ident, $left: expr, $need_prefix: expr, $prefix: expr, $width: expr, $padding: expr) => {
        let mut field_width = cmp::max($width, $str.len());
        let mut result = String::with_capacity(field_width + $prefix.len());
        if $need_prefix {
            result.push_str($prefix);
            field_width -= $prefix.len();
        }
        pad_and_print!(result, $str, $left, field_width, $padding);
    };
}
@snapdgn
Copy link

snapdgn commented Sep 10, 2022

would love to take this up.

@tertsdiepraam
Copy link
Member Author

Go ahead!

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