Repository: fmtlib/fmt
File: src/os.cc (C++)

Code before:
  return 0;
}

namespace detail {

class system_message {
  system_message(const system_message&) = delete;
  void operator=(const system_message&) = delete;

  unsigned long result_;
  wchar_t* message_;

  static bool is_whitespace(wchar_t c) FMT_NOEXCEPT {
    return c == L' ' || c == L'\n' || c == L'\r' || c == L'\t' || c == L'\0';
  }

 public:
  system_message(unsigned long error_code) : result_(0), message_(nullptr) {
    result_ = FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
        nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        reinterpret_cast<wchar_t*>(&message_), 0, nullptr);
  }
  ~system_message() { LocalFree(message_); }
  unsigned long result() const FMT_NOEXCEPT { return result_; }
  operator wstring_view() const FMT_NOEXCEPT {
    return wstring_view(message_, result_);
  }
  wstring_view rtrim() const FMT_NOEXCEPT {
    auto len = result_;
    while (len != 0 && is_whitespace(message_[len - 1])) {
      --len;
    }
    return wstring_view(message_, len);
  }
};

class utf8_system_category final : public std::error_category {
 public:
  const char* name() const FMT_NOEXCEPT override { return "system"; }
  std::string message(int error_code) const override {
    system_message msg(error_code);
    if (msg.result() != 0) {
      utf16_to_utf8 utf8_message;
      if (utf8_message.convert(msg.rtrim()) == ERROR_SUCCESS) {
        return utf8_message.str();
      }
    }
    return "unknown error";
  }

Review comment (suggestion):
Let's replace this with `explicit operator bool` since callers don't need the actual result, just the success indicator.

Code after:
  return 0;
}

namespace detail {

class system_message {
  system_message(const system_message&) = delete;
  void operator=(const system_message&) = delete;

  unsigned long result_;
  wchar_t* message_;

  static bool is_whitespace(wchar_t c) FMT_NOEXCEPT {
    return c == L' ' || c == L'\n' || c == L'\r' || c == L'\t' || c == L'\0';
  }

 public:
  explicit system_message(unsigned long error_code)
      : result_(0), message_(nullptr) {
    result_ = FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
        nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        reinterpret_cast<wchar_t*>(&message_), 0, nullptr);
    if (result_ != 0) {
      while (result_ != 0 && is_whitespace(message_[result_ - 1])) {
        --result_;
      }
    }
  }
  ~system_message() { LocalFree(message_); }
  explicit operator bool() const FMT_NOEXCEPT { return result_ != 0; }
  operator wstring_view() const FMT_NOEXCEPT {
    return wstring_view(message_, result_);
  }
};

class utf8_system_category final : public std::error_category {
 public:
  const char* name() const FMT_NOEXCEPT override { return "system"; }
  std::string message(int error_code) const override {
    system_message msg(error_code);
    if (msg) {
      utf16_to_utf8 utf8_message;
      if (utf8_message.convert(msg) == ERROR_SUCCESS) {
        return utf8_message.str();
      }
    }
    return "unknown error";
  }
};