Repository: bitcoin/bitcoin
File: src/util/string.h (C/C++)

Code before:
            if (*it == '%') {
                ++it;
            } else {
                continue;
            }

            if (*it == '%') {
                // Percent escape: %%
                continue;
            }

            auto add_arg = [&] {
                unsigned maybe_num{0};
                while ('0' <= *it && *it <= '9') {
                    maybe_num *= 10;
                    maybe_num += *it - '0';
                    ++it;
                }

                if (*it == '$') {
                    ++it;
                    // Positional specifier, like %8$s
                    if (maybe_num == 0) throw "Positional format specifier must have position of at least 1";
                    count_pos = std::max(count_pos, maybe_num);
                } else {
                    // Non-positional specifier, like %s
                    ++count_normal;
                }
            };

            // Increase argument count and consume positional specifier, if present.
            add_arg();

            // Consume flags.
            while (*it == '#' || *it == '0' || *it == '-' || *it == ' ' || *it == '+') ++it;

            auto parse_size = [&] {
                if (*it == '*') {
                    ++it;
                    add_arg();
                } else {
                    while ('0' <= *it && *it <= '9') ++it;
                }
            };

            // Consume dynamic or static width value.
            parse_size();

            // Consume dynamic or static precision value.
            if (*it == '.') {
                ++it;

Review comment (refactor):
In C++23 this could be a simple `.contains`, but even in C++20 we should be able to group the flags to something like:
```suggestion
            while ("#0- +"sv.find(*it) != std::string_view::npos) ++it;
```
(we could even extract the flag in which case we could get rid of the comment)

Code after:
            if (*it != '%' || *++it == '%') continue; // Skip escaped %%

            auto add_arg = [&] {
                unsigned maybe_num{0};
                while ('0' <= *it && *it <= '9') {
                    maybe_num *= 10;
                    maybe_num += *it - '0';
                    ++it;
                }

                if (*it == '$') {
                    ++it;
                    // Positional specifier, like %8$s
                    if (maybe_num == 0) throw "Positional format specifier must have position of at least 1";
                    count_pos = std::max(count_pos, maybe_num);
                } else {
                    // Non-positional specifier, like %s
                    ++count_normal;
                }
            };

            // Increase argument count and consume positional specifier, if present.
            add_arg();

            // Consume flags.
            while (*it == '#' || *it == '0' || *it == '-' || *it == ' ' || *it == '+') ++it;

            auto parse_size = [&] {
                if (*it == '*') {
                    ++it;
                    add_arg();
                } else {
                    while ('0' <= *it && *it <= '9') ++it;
                }
            };

            // Consume dynamic or static width value.
            parse_size();

            // Consume dynamic or static precision value.
            if (*it == '.') {
                ++it;
                parse_size();
            }

            if (*it == '\0') throw "Format specifier incorrectly terminated by end of string";

            // Length and type in "[flags][width][.precision][length]type"
            // is not checked. Parsing continues with the next '%'.
        }
        if (count_normal && count_pos) throw "Format specifiers must be all positional or all non-positional!";