ronantakizawa/codereview-bench huggingface dataset:
Repository: fmtlib/fmt
File: include/fmt/core.h (C/C++)

Code before:
  case internal::type::int128_type:
  case internal::type::uint128_type:
    break;
#endif
  case internal::type::bool_type:
    return vis(arg.value_.bool_value);
  case internal::type::char_type:
    return vis(arg.value_.char_value);
  case internal::type::float_type:
#if FMT_USE_FLOAT
    return vis(arg.value_.float_value);
#else
    break;
#endif
  case internal::type::double_type:
#if FMT_USE_DOUBLE
    return vis(arg.value_.double_value);
#else
    break;
#endif
  case internal::type::long_double_type:
#if FMT_USE_LONG_DOUBLE
    return vis(arg.value_.long_double_value);
#else
    break;
#endif
  case internal::type::cstring_type:
    return vis(arg.value_.string.data);
  case internal::type::string_type:
    return vis(basic_string_view<char_type>(arg.value_.string.data,
                                            arg.value_.string.size));
  case internal::type::pointer_type:
    return vis(arg.value_.pointer);
  case internal::type::custom_type:
    return vis(typename basic_format_arg<Context>::handle(arg.value_.custom));
  }
  return vis(monostate());
}

namespace internal {
// A map from argument names to their values for named arguments.
template <typename Context> class arg_map {
 private:
  using char_type = typename Context::char_type;

  struct entry {
    basic_string_view<char_type> name;
    basic_format_arg<Context> arg;
  };

  entry* map_;

Review comment (suggestion):
Please remove conditional compilation here. `FMT_USE_*` should be handled on the visitor side instead.

Code after:
  case internal::type::int128_type:
  case internal::type::uint128_type:
    break;
#endif
  case internal::type::bool_type:
    return vis(arg.value_.bool_value);
  case internal::type::char_type:
    return vis(arg.value_.char_value);
  case internal::type::float_type:
    return vis(arg.value_.float_value);
  case internal::type::double_type:
    return vis(arg.value_.double_value);
  case internal::type::long_double_type:
    return vis(arg.value_.long_double_value);
  case internal::type::cstring_type:
    return vis(arg.value_.string.data);
  case internal::type::string_type:
    return vis(basic_string_view<char_type>(arg.value_.string.data,
                                            arg.value_.string.size));
  case internal::type::pointer_type:
    return vis(arg.value_.pointer);
  case internal::type::custom_type:
    return vis(typename basic_format_arg<Context>::handle(arg.value_.custom));
  }
  return vis(monostate());
}

namespace internal {
// A map from argument names to their values for named arguments.
template <typename Context> class arg_map {
 private:
  using char_type = typename Context::char_type;

  struct entry {
    basic_string_view<char_type> name;
    basic_format_arg<Context> arg;
  };

  entry* map_;
  unsigned size_;

  void push_back(value<Context> val) {
    const auto& named = *val.named_arg;
    map_[size_] = {named.name, named.template deserialize<Context>()};
    ++size_;
  }

 public:
  arg_map(const arg_map&) = delete;
  void operator=(const arg_map&) = delete;
  arg_map() : map_(nullptr), size_(0) {}
Repository: fmtlib/fmt
File: include/fmt/os.h (C/C++)

Code before:
  /**
    \rst
    Constructs a string reference from an ``std::string`` object.
    \endrst
   */
  basic_cstring_view(const std::basic_string<Char>& s) : data_(s.c_str()) {}

  /** Returns the pointer to a C string. */
  const Char* c_str() const { return data_; }
};

using cstring_view = basic_cstring_view<char>;
using wcstring_view = basic_cstring_view<wchar_t>;

// An error code.
class error_code {
 private:
  int value_;

 public:
  explicit error_code(int value = 0) FMT_NOEXCEPT : value_(value) {}

  int get() const FMT_NOEXCEPT { return value_; }
};

// A formatter specialization for std::error_code.
template <typename Char> struct formatter<std::error_code, Char> {
  template <typename ParseContext>
  FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
    return ctx.begin();
  }

  template <typename FormatContext>
  FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
      -> decltype(ctx.out()) {
    auto out = ctx.out();
    out = detail::write<Char>(out, to_string_view(ec.category().name()));
    out = detail::write<Char>(out, Char(':'));
    out = detail::write<Char>(out, ec.value());
    return out;
  }
};

#ifdef _WIN32
namespace detail {
// A converter from UTF-16 to UTF-8.
// It is only provided for Windows since other systems support UTF-8 natively.
class utf16_to_utf8 {
 private:
  memory_buffer buffer_;


Review comment (refactor):
Let's remove this comment since it's obvious from the next line that this is a formatter specialization for `std::error_code`.

Code after:
  /**
    \rst
    Constructs a string reference from an ``std::string`` object.
    \endrst
   */
  basic_cstring_view(const std::basic_string<Char>& s) : data_(s.c_str()) {}

  /** Returns the pointer to a C string. */
  const Char* c_str() const { return data_; }
};

using cstring_view = basic_cstring_view<char>;
using wcstring_view = basic_cstring_view<wchar_t>;

// An error code.
class error_code {
 private:
  int value_;

 public:
  explicit error_code(int value = 0) FMT_NOEXCEPT : value_(value) {}

  int get() const FMT_NOEXCEPT { return value_; }
};

template <typename Char> struct formatter<std::error_code, Char> {
  template <typename ParseContext>
  FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
    return ctx.begin();
  }

  template <typename FormatContext>
  FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
      -> decltype(ctx.out()) {
    auto out = ctx.out();
    out = detail::write<Char>(out, to_string_view(ec.category().name()));
    out = detail::write<Char>(out, Char(':'));
    out = detail::write<Char>(out, ec.value());
    return out;
  }
};

#ifdef _WIN32
namespace detail {
// A converter from UTF-16 to UTF-8.
// It is only provided for Windows since other systems support UTF-8 natively.
class utf16_to_utf8 {
 private:
  memory_buffer buffer_;

 public:
Repository: fmtlib/fmt
File: test/args-test.cc (C++)

Code before:
  auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
    return ctx.begin();
  }
  auto format(copy_throwable, format_context& ctx) -> decltype(ctx.out()) {
    return ctx.out();
  }
};
FMT_END_NAMESPACE

TEST(args_test, throw_on_copy) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(std::string("foo"));
  try {
    store.push_back(copy_throwable());
  } catch (...) {
  }
  EXPECT_EQ(fmt::vformat("{}", store), "foo");
}

TEST(args_test, copy_constructor) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(fmt::arg("test1", "value1"));
  store.push_back(fmt::arg("test2", "value2"));
  store.push_back(fmt::arg("test3", "value3"));

  fmt::dynamic_format_arg_store<fmt::format_context> store2(store);
  store2.push_back(fmt::arg("test4", "value4"));

  auto result = fmt::vformat("{test1} {test2} {test3} {test4}", store2);
  EXPECT_EQ(result, "value1 value2 value3 value4");
}


Review comment (suggestion):
nit: let's use the same declaration format here

```c++
auto store2 = fmt::dynamic_format_arg_store<fmt::format_context>(store);
```

Code after:
  auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
    return ctx.begin();
  }
  auto format(copy_throwable, format_context& ctx) -> decltype(ctx.out()) {
    return ctx.out();
  }
};
FMT_END_NAMESPACE

TEST(args_test, throw_on_copy) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(std::string("foo"));
  try {
    store.push_back(copy_throwable());
  } catch (...) {
  }
  EXPECT_EQ(fmt::vformat("{}", store), "foo");
}

TEST(args_test, copy_constructor) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(fmt::arg("test1", "value1"));
  store.push_back(fmt::arg("test2", "value2"));
  store.push_back(fmt::arg("test3", "value3"));

  auto store2 = store;
  store2.push_back(fmt::arg("test4", "value4"));

  auto result = fmt::vformat("{test1} {test2} {test3} {test4}", store2);
  EXPECT_EQ(result, "value1 value2 value3 value4");
}
Repository: fmtlib/fmt
File: test/args-test.cc (C++)

Code before:
  auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
    return ctx.begin();
  }
  auto format(copy_throwable, format_context& ctx) -> decltype(ctx.out()) {
    return ctx.out();
  }
};
FMT_END_NAMESPACE

TEST(args_test, throw_on_copy) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(std::string("foo"));
  try {
    store.push_back(copy_throwable());
  } catch (...) {
  }
  EXPECT_EQ(fmt::vformat("{}", store), "foo");
}

TEST(args_test, copy_constructor) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(fmt::arg("test1", "value1"));
  store.push_back(fmt::arg("test2", "value2"));
  store.push_back(fmt::arg("test3", "value3"));

  auto store2 = fmt::dynamic_format_arg_store<fmt::format_context>(store);
  store2.push_back(fmt::arg("test4", "value4"));

  auto result = fmt::vformat("{test1} {test2} {test3} {test4}", store2);
  EXPECT_EQ(result, "value1 value2 value3 value4");
}


Review comment (suggestion):
To get rid of the "useless cast" gcc warning:

```c++
  auto store2 = store;
```

Code after:
  auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
    return ctx.begin();
  }
  auto format(copy_throwable, format_context& ctx) -> decltype(ctx.out()) {
    return ctx.out();
  }
};
FMT_END_NAMESPACE

TEST(args_test, throw_on_copy) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(std::string("foo"));
  try {
    store.push_back(copy_throwable());
  } catch (...) {
  }
  EXPECT_EQ(fmt::vformat("{}", store), "foo");
}

TEST(args_test, copy_constructor) {
  auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
  store.push_back(fmt::arg("test1", "value1"));
  store.push_back(fmt::arg("test2", "value2"));
  store.push_back(fmt::arg("test3", "value3"));

  auto store2 = store;
  store2.push_back(fmt::arg("test4", "value4"));

  auto result = fmt::vformat("{test1} {test2} {test3} {test4}", store2);
  EXPECT_EQ(result, "value1 value2 value3 value4");
}
Repository: fmtlib/fmt
File: test/fuzzing/chrono-timepoint.cc (C++)

Code before:
  size -= N;
  data_to_string format_str(data, size);

  std::string message = fmt::format(format_str.get(), timepoint);
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  if (size < 1) return 0;
  const auto action = data[0] & 0b1;
  data += 1;
  size -= 1;

  try {
    switch (action) {
    case 0:
      doit<std::chrono::system_clock>(data, size);
      break;
    case 1:
        // may be the same as system_clock
        doit<std::chrono::high_resolution_clock>(data, size);
      break;
    case 2:
      // won't compile
      // doit<std::chrono::steady_clock>(data,size);
      break;
    }
  } catch (...) {
  }
  return 0;
}


Review comment (refactor):
Let's remove this switch because only formatting of `system_clock` time points is supported. It doesn't make much sense to fuzz `high_resolution_clock` because it will either be an alias of `system_clock` in which case we'll be fuzzing the same thing or won't compile.

Code after:
// Copyright (c) 2021, Paul Dreik
// For license information refer to format.h.
#include <fmt/chrono.h>

#include "fuzzer-common.h"

/*
 * a fuzzer for the chrono timepoints formatters
 * C is a clock (std::chrono::system_clock etc)
 */
template <typename C> void doit(const uint8_t* data, size_t size) {
  using Rep = typename C::time_point::rep;
  constexpr auto N = sizeof(Rep);
  if (size < N) return;

  const auto x = assign_from_buf<Rep>(data);
  typename C::duration dur{x};
  typename C::time_point timepoint{dur};
  data += N;
  size -= N;
  data_to_string format_str(data, size);

  std::string message = fmt::format(format_str.get(), timepoint);
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  try {
    doit<std::chrono::system_clock>(data, size);
  } catch (...) {
  }
  return 0;
}
Repository: fmtlib/fmt
File: test/fuzzing/chrono-timepoint.cc (C++)

Code before:
// Copyright (c) 2021, Paul Dreik
// For license information refer to format.h.
#include <fmt/chrono.h>

#include "fuzzer-common.h"

/*
 * a fuzzer for the chrono timepoints formatters
 * C is a clock (std::chrono::system_clock etc)
 */
template <typename C> void doit(const uint8_t* data, size_t size) {
  using D = typename C::duration;
  using TP = typename C::time_point;
  using Rep = typename TP::rep;
  constexpr auto N = sizeof(Rep);
  if (size < N) return;

  const auto x = assign_from_buf<Rep>(data);
  D dur{x};
  TP timepoint{dur};
  data += N;
  size -= N;
  data_to_string format_str(data, size);

  std::string message = fmt::format(format_str.get(), timepoint);
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  if (size < 1) return 0;
  const auto action = data[0] & 0b1;
  data += 1;
  size -= 1;

  try {
    switch (action) {
    case 0:
      doit<std::chrono::system_clock>(data, size);
      break;
    case 1:

Review comment (suggestion):
I suggest inlining these aliases to improve readability since they are only used once or, in the case of Rep, twice. Or just keep `Rep` and inline the other two.

Code after:
// Copyright (c) 2021, Paul Dreik
// For license information refer to format.h.
#include <fmt/chrono.h>

#include "fuzzer-common.h"

/*
 * a fuzzer for the chrono timepoints formatters
 * C is a clock (std::chrono::system_clock etc)
 */
template <typename C> void doit(const uint8_t* data, size_t size) {
  using Rep = typename C::time_point::rep;
  constexpr auto N = sizeof(Rep);
  if (size < N) return;

  const auto x = assign_from_buf<Rep>(data);
  typename C::duration dur{x};
  typename C::time_point timepoint{dur};
  data += N;
  size -= N;
  data_to_string format_str(data, size);

  std::string message = fmt::format(format_str.get(), timepoint);
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  try {
    doit<std::chrono::system_clock>(data, size);
  } catch (...) {
  }
  return 0;
}
Repository: fmtlib/fmt
File: include/fmt/core.h (C/C++)

Code before:
#include <cstring>
#include <iterator>
#include <limits>
#include <string>
#include <type_traits>

// The fmt library version in the form major * 10000 + minor * 100 + patch.
#define FMT_VERSION 80001

#ifdef __clang__
#  define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
#else
#  define FMT_CLANG_VERSION 0
#endif

#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
#  define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
#  define FMT_GCC_PRAGMA(arg) _Pragma(arg)
#else
#  define FMT_GCC_VERSION 0
#  define FMT_GCC_PRAGMA(arg)
#endif

#ifdef __ICL
#  define FMT_ICC_VERSION __ICL
#  define FMT_ICC_ON_WINDOWS 1
#elif defined(__INTEL_COMPILER)
#  define FMT_ICC_VERSION __INTEL_COMPILER
#  define FMT_ICC_ON_WINDOWS 0
#else
#  define FMT_ICC_VERSION 0
#  define FMT_ICC_ON_WINDOWS 0
#endif

#ifdef __NVCC__
#  define FMT_NVCC __NVCC__
#else
#  define FMT_NVCC 0
#endif

#ifdef _MSC_VER
#  define FMT_MSC_VER _MSC_VER
#  define FMT_MSC_WARNING(...) __pragma(warning(__VA_ARGS__))
#else
#  define FMT_MSC_VER 0
#  define FMT_MSC_WARNING(...)
#endif

#ifdef __has_feature
#  define FMT_HAS_FEATURE(x) __has_feature(x)
#else

Review comment (refactor):
I suggest replacing `FMT_ICC_ON_WINDOWS` with `FMT_ICC_POSIX` defined to 1 on non-Windows platforms. This will simplify the checks.

Code after:
#include <cstring>
#include <iterator>
#include <limits>
#include <string>
#include <type_traits>

// The fmt library version in the form major * 10000 + minor * 100 + patch.
#define FMT_VERSION 80001

#ifdef __clang__
#  define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
#else
#  define FMT_CLANG_VERSION 0
#endif

#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
#  define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
#  define FMT_GCC_PRAGMA(arg) _Pragma(arg)
#else
#  define FMT_GCC_VERSION 0
#  define FMT_GCC_PRAGMA(arg)
#endif

#ifdef __ICL
#  define FMT_ICC_VERSION __ICL
#  define FMT_ICC_INTRINSIC_BUG 1
#elif defined(__INTEL_COMPILER)
#  define FMT_ICC_VERSION __INTEL_COMPILER
#  define FMT_ICC_INTRINSIC_BUG 0
#else
#  define FMT_ICC_VERSION 0
#  define FMT_ICC_INTRINSIC_BUG 0
#endif

#ifdef __NVCC__
#  define FMT_NVCC __NVCC__
#else
#  define FMT_NVCC 0
#endif

#ifdef _MSC_VER
#  define FMT_MSC_VER _MSC_VER
#  define FMT_MSC_WARNING(...) __pragma(warning(__VA_ARGS__))
#else
#  define FMT_MSC_VER 0
#  define FMT_MSC_WARNING(...)
#endif

#ifdef __has_feature
#  define FMT_HAS_FEATURE(x) __has_feature(x)
#else
Repository: fmtlib/fmt
File: include/fmt/chrono.h (C/C++)

Code before:
  out = write<Char>(out, Period::num);
  if (const_check(Period::den != 1)) {
    *out++ = '/';
    out = write<Char>(out, Period::den);
  }
  *out++ = ']';
  *out++ = 's';
  return out;
}

struct get_locale {
  explicit get_locale(bool localized, locale_ref loc_ref)
      : has_locale{localized} {
    if (localized) locale = loc_ref.template get<std::locale>();
  }
  ~get_locale() {
    if (has_locale) locale.~locale();
  }
  operator const std::locale&() const {
    return has_locale ? locale : get_classic_locale();
  }

 private:
  union {
    monostate dummy{};
    std::locale locale;
  };
  bool has_locale = false;
};

template <typename FormatContext, typename OutputIt, typename Rep,
          typename Period>
struct chrono_formatter {
  FormatContext& context;
  OutputIt out;
  int precision;
  bool localized = false;
  // rep is unsigned to avoid overflow.
  using rep =
      conditional_t<std::is_integral<Rep>::value && sizeof(Rep) < sizeof(int),
                    unsigned, typename make_unsigned_or_unchanged<Rep>::type>;
  rep val;
  using seconds = std::chrono::duration<rep>;
  seconds s;
  using milliseconds = std::chrono::duration<rep, std::milli>;
  bool negative;

  using char_type = typename FormatContext::char_type;

  explicit chrono_formatter(FormatContext& ctx, OutputIt o,
                            std::chrono::duration<Rep, Period> d)

Review comment (suggestion):
~~``monostate``  is C++17 only~~

I'm sorry!!
This is ``fmt::monostate``, not ``std::monostate``.



Code after:
    const auto w = iso_week_num(tm_yday(), tm_wday());
    if (w < 1) return iso_year_weeks(year - 1);
    if (w > iso_year_weeks(year)) return 1;
    return w;
  }

  void write1(int value) {
    *out_++ = static_cast<char>('0' + to_unsigned(value) % 10);
  }
  void write2(int value) {
    const char* d = digits2(to_unsigned(value) % 100);
    *out_++ = *d++;
    *out_++ = *d;
  }

  void write_year_extended(long long year) {
    // At least 4 characters.
    int width = 4;
    if (year < 0) {
      *out_++ = '-';
      year = 0 - year;
      --width;
    }
    uint32_or_64_or_128_t<long long> n = to_unsigned(year);
    const int num_digits = count_digits(n);
    if (width > num_digits) out_ = std::fill_n(out_, width - num_digits, '0');
    out_ = format_decimal<Char>(out_, n, num_digits).end;
  }
  void write_year(long long year) {
    if (year >= 0 && year < 10000) {
      write2(static_cast<int>(year / 100));
      write2(static_cast<int>(year % 100));
    } else {
      write_year_extended(year);
    }
  }

  void format_localized(char format, char modifier = 0) {
    out_ = write<Char>(out_, tm_, loc_, format, modifier);
  }

 public:
  explicit tm_writer(const std::locale& loc, OutputIt out, const std::tm& tm)
      : loc_(loc),
        is_classic_(loc_ == get_classic_locale()),
        out_(out),
        tm_(tm) {}

  OutputIt out() const { return out_; }

  FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
Repository: fmtlib/fmt
File: include/fmt/format.h (C/C++)

Code before:
  auto xsputn(const char_type* s, streamsize count) -> streamsize override {
    buffer_.append(s, s + count);
    return count;
  }
};

// Implementation of std::bit_cast for pre-C++20.
template <typename To, typename From>
FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
  static_assert(sizeof(To) == sizeof(From), "size mismatch");
#ifdef __cpp_lib_bit_cast
  if (is_constant_evaluated()) return std::bit_cast<To>(from);
#endif
  auto to = To();
  std::memcpy(&to, &from, sizeof(to));
  return to;
}

#ifdef _WIN32
constexpr auto is_big_endian() -> bool { return false; }
#elif defined(__BIG_ENDIAN__)
constexpr auto is_big_endian() -> bool { return true; }
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
constexpr auto is_big_endian() -> bool {
  return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
}
#else
inline auto is_big_endian() -> bool {
  struct bytes {
    char data[sizeof(int)];
  };
  return bit_cast<bytes>(1).data[0] == 0;
}
#endif

// A fallback implementation of uintptr_t for systems that lack it.
struct fallback_uintptr {
  unsigned char value[sizeof(void*)];

  fallback_uintptr() = default;
  explicit fallback_uintptr(const void* p) {
    *this = bit_cast<fallback_uintptr>(p);
    if (const_check(is_big_endian())) {
      for (size_t i = 0, j = sizeof(void*) - 1; i < j; ++i, --j)
        std::swap(value[i], value[j]);
    }
  }
};
#ifdef UINTPTR_MAX
using uintptr_t = ::uintptr_t;
inline auto to_uintptr(const void* p) -> uintptr_t {

Review comment (performance):
I suggest moving the ifdefs into the body of `is_big_endian` to avoid repeating its signature. I don't think we care about `constexpr` here, it will be inlined and optimized away.

Code after:
  auto xsputn(const char_type* s, streamsize count) -> streamsize override {
    buffer_.append(s, s + count);
    return count;
  }
};

// Implementation of std::bit_cast for pre-C++20.
template <typename To, typename From>
FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
  static_assert(sizeof(To) == sizeof(From), "size mismatch");
#ifdef __cpp_lib_bit_cast
  if (is_constant_evaluated()) return std::bit_cast<To>(from);
#endif
  auto to = To();
  std::memcpy(&to, &from, sizeof(to));
  return to;
}

inline auto is_big_endian() -> bool {
#ifdef _WIN32
  return false;
#elif defined(__BIG_ENDIAN__)
  return true;
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
  return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
#else
  struct bytes {
    char data[sizeof(int)];
  };
  return bit_cast<bytes>(1).data[0] == 0;
#endif
}

// A fallback implementation of uintptr_t for systems that lack it.
struct fallback_uintptr {
  unsigned char value[sizeof(void*)];

  fallback_uintptr() = default;
  explicit fallback_uintptr(const void* p) {
    *this = bit_cast<fallback_uintptr>(p);
    if (const_check(is_big_endian())) {
      for (size_t i = 0, j = sizeof(void*) - 1; i < j; ++i, --j)
        std::swap(value[i], value[j]);
    }
  }
};
#ifdef UINTPTR_MAX
using uintptr_t = ::uintptr_t;
inline auto to_uintptr(const void* p) -> uintptr_t {
  return bit_cast<uintptr_t>(p);
}