Repository: fmtlib/fmt
File: include/fmt/ranges.h (C/C++)

Code before:
  bool is_string_format = false;
  bool is_debug = false;

 public:
  FMT_CONSTEXPR range_formatter() {}

  FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type<Char, T>& {
    return underlying_;
  }

  FMT_CONSTEXPR void set_separator(basic_string_view<Char> sep) {
    separator_ = sep;
  }

  FMT_CONSTEXPR void set_brackets(basic_string_view<Char> open,
                                  basic_string_view<Char> close) {
    opening_bracket_ = open;
    closing_bracket_ = close;
  }

  template <typename ParseContext>
  FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
    auto it = ctx.begin();
    auto end = ctx.end();

    if (it != end && *it == 'n') {
      set_brackets({}, {});
      ++it;
    } else {
      bool check_for_s = false;
      if (it != end && *it == '?') {
        ++it;
        detail::maybe_set_debug_format(underlying_, true);
        set_brackets({}, {});
        check_for_s = true;
        is_debug = true;
      }
      if (it != end && *it == 's') {
        if (!std::is_same<T, Char>::value) {
          report_error("invalid format specifier");
        }
        if (!is_debug) {
          set_brackets(detail::string_literal<Char, '"'>{},
                       detail::string_literal<Char, '"'>{});
        }
        check_for_s = false;
        is_string_format = true;
        ++it;
      }
      if (check_for_s) {
        report_error("invalid format specifier");

Review comment (refactor):
Let's move `it == end` check out of if and else branches and do early return to avoid duplicate checks and make the logic a bit simpler by switching to a switch:

```
switch (to_ascii(*it)) {
case 'n':
  ...
  break;
case '?':
  ...
  FMT_FALLTHROUGH;
case 's':
  ...
  break;
}
```

Code after:
  bool is_string_format = false;
  bool is_debug = false;

 public:
  FMT_CONSTEXPR range_formatter() {}

  FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type<Char, T>& {
    return underlying_;
  }

  FMT_CONSTEXPR void set_separator(basic_string_view<Char> sep) {
    separator_ = sep;
  }

  FMT_CONSTEXPR void set_brackets(basic_string_view<Char> open,
                                  basic_string_view<Char> close) {
    opening_bracket_ = open;
    closing_bracket_ = close;
  }

  template <typename ParseContext>
  FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
    auto it = ctx.begin();
    auto end = ctx.end();
    detail::maybe_set_debug_format(underlying_, true);
    if (it == end) {
      return underlying_.parse(ctx);
    }

    switch (detail::to_ascii(*it)) {
    case 'n':
      set_brackets({}, {});
      ++it;
      break;
    case '?':
      is_debug = true;
      set_brackets({}, {});
      ++it;
      if (it == end || *it != 's') {
        report_error("invalid format specifier");
      }
      FMT_FALLTHROUGH;
    case 's':
      if (!std::is_same<T, Char>::value) {
        report_error("invalid format specifier");
      }
      if (!is_debug) {
        set_brackets(detail::string_literal<Char, '"'>{},
                     detail::string_literal<Char, '"'>{});
        set_separator({});
        detail::maybe_set_debug_format(underlying_, false);