From 2420a589c68485cd171228b3fa24cbd6fc6bef29 Mon Sep 17 00:00:00 2001 From: luncliff Date: Wed, 19 Dec 2018 15:52:51 +0900 Subject: [PATCH] clang-format for test files - Applied format to test files - build tested with gcc-8 / g++-8 --- include/fmt/chrono.h | 16 +- include/fmt/color.h | 52 +- include/fmt/core.h | 204 +++-- include/fmt/format-inl.h | 154 ++-- include/fmt/format.h | 455 ++++++----- include/fmt/locale.h | 51 +- test/add-subdirectory-test/main.cc | 3 +- test/assert-test.cc | 8 +- test/chrono-test.cc | 97 +-- test/core-test.cc | 130 +-- test/custom-formatter-test.cc | 8 +- test/find-package-test/main.cc | 3 +- test/format-impl-test.cc | 99 ++- test/format-test.cc | 1217 +++++++++++++++++----------- test/gtest-extra-test.cc | 173 ++-- test/gtest-extra.cc | 2 +- test/gtest-extra.h | 128 +-- test/mock-allocator.h | 14 +- test/ostream-test.cc | 53 +- test/posix-mock-test.cc | 143 ++-- test/posix-mock.h | 10 +- test/posix-test.cc | 72 +- test/printf-test.cc | 187 +++-- test/ranges-test.cc | 6 +- test/test-assert.h | 3 +- test/test-main.cc | 12 +- test/time-test.cc | 29 +- test/util.h | 7 +- 28 files changed, 1931 insertions(+), 1405 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index c44866e0..9c49fb2b 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -28,9 +28,8 @@ enum class numeric_system { // Parses a put_time-like format string and invokes handler actions. template -FMT_CONSTEXPR const Char *parse_chrono_format(const Char *begin, - const Char *end, - Handler &&handler) { +FMT_CONSTEXPR const Char *parse_chrono_format( + const Char *begin, const Char *end, Handler &&handler) { auto ptr = begin; while (ptr != end) { auto c = *ptr; @@ -214,9 +213,10 @@ struct chrono_format_checker { template inline int to_int(Int value) { - FMT_ASSERT(value >= (std::numeric_limits::min)() && - value <= (std::numeric_limits::max)(), - "invalid value"); + FMT_ASSERT( + value >= (std::numeric_limits::min)() && + value <= (std::numeric_limits::max)(), + "invalid value"); return static_cast(value); } @@ -491,8 +491,8 @@ struct formatter, Char> { format_to(buf, "{}[{}/{}]s", d.count(), Period::num, Period::den); typedef output_range range; basic_writer w(range(ctx.out())); - internal::handle_dynamic_spec(spec.width_, - width_ref, ctx); + internal::handle_dynamic_spec( + spec.width_, width_ref, ctx); w.write(buf.data(), buf.size(), spec); return w.out(); } diff --git a/include/fmt/color.h b/include/fmt/color.h index 5c0da531..eafac7fa 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -19,13 +19,13 @@ enum color { black, red, green, yellow, blue, magenta, cyan, white }; FMT_API void vprint_colored(color c, string_view format, format_args args); FMT_API void vprint_colored(color c, wstring_view format, wformat_args args); template -inline void print_colored(color c, string_view format_str, - const Args&... args) { +inline void print_colored( + color c, string_view format_str, const Args&... args) { vprint_colored(c, format_str, make_format_args(args...)); } template -inline void print_colored(color c, wstring_view format_str, - const Args&... args) { +inline void print_colored( + color c, wstring_view format_str, const Args&... args) { vprint_colored(c, format_str, make_format_args(args...)); } @@ -290,13 +290,13 @@ class text_style { background_color.value.rgb_color |= rhs.background_color.value.rgb_color; } - ems = static_cast(static_cast(ems) | - static_cast(rhs.ems)); + ems = static_cast( + static_cast(ems) | static_cast(rhs.ems)); return *this; } - friend FMT_CONSTEXPR text_style operator|(text_style lhs, - const text_style &rhs) { + friend FMT_CONSTEXPR text_style + operator|(text_style lhs, const text_style &rhs) { return lhs |= rhs; } @@ -319,13 +319,13 @@ class text_style { background_color.value.rgb_color &= rhs.background_color.value.rgb_color; } - ems = static_cast(static_cast(ems) & - static_cast(rhs.ems)); + ems = static_cast( + static_cast(ems) & static_cast(rhs.ems)); return *this; } - friend FMT_CONSTEXPR text_style operator&(text_style lhs, - const text_style &rhs) { + friend FMT_CONSTEXPR text_style + operator&(text_style lhs, const text_style &rhs) { return lhs &= rhs; } @@ -352,11 +352,10 @@ class text_style { } private: - FMT_CONSTEXPR text_style(bool is_foreground, - internal::color_type text_color) FMT_NOEXCEPT - : set_foreground_color(), - set_background_color(), - ems() { + FMT_CONSTEXPR text_style(bool is_foreground, internal::color_type text_color) + FMT_NOEXCEPT : set_foreground_color(), + set_background_color(), + ems() { if (is_foreground) { foreground_color = text_color; set_foreground_color = true; @@ -394,8 +393,8 @@ namespace internal { template struct ansi_color_escape { - FMT_CONSTEXPR ansi_color_escape(internal::color_type text_color, - const char *esc) FMT_NOEXCEPT { + FMT_CONSTEXPR ansi_color_escape( + internal::color_type text_color, const char *esc) FMT_NOEXCEPT { // If we have a terminal color, we need to output another escape code // sequence. if (!text_color.is_rgb) { @@ -459,8 +458,8 @@ struct ansi_color_escape { private: Char buffer[7 + 3 * 4 + 1]; - static FMT_CONSTEXPR void to_esc(uint8_t c, Char *out, - char delimiter) FMT_NOEXCEPT { + static FMT_CONSTEXPR void to_esc(uint8_t c, Char *out, char delimiter) + FMT_NOEXCEPT { out[0] = static_cast('0' + c / 100); out[1] = static_cast('0' + c / 10 % 10); out[2] = static_cast('0' + c % 10); @@ -516,8 +515,11 @@ struct is_string : std::false_type {}; } // namespace internal template ::type> -void vprint(std::FILE *f, const text_style &ts, const S &format, - basic_format_args::type> args) { +void vprint( + std::FILE *f, + const text_style &ts, + const S &format, + basic_format_args::type> args) { bool has_style = false; if (ts.has_emphasis()) { has_style = true; @@ -548,7 +550,9 @@ void vprint(std::FILE *f, const text_style &ts, const S &format, */ template typename std::enable_if::value>::type print( - std::FILE *f, const text_style &ts, const String &format_str, + std::FILE *f, + const text_style &ts, + const String &format_str, const Args &... args) { internal::check_format_string(format_str); typedef typename internal::char_t::type char_t; diff --git a/include/fmt/core.h b/include/fmt/core.h index ca5157ce..51323d79 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -178,8 +178,9 @@ #endif // libc++ supports string_view in pre-c++17. -#if (FMT_HAS_INCLUDE() && \ - (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \ +#if ( \ + FMT_HAS_INCLUDE() && \ + (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \ (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) #include #define FMT_STRING_VIEW std::basic_string_view @@ -232,10 +233,10 @@ class basic_buffer { // Don't initialize ptr_ since it is not accessed to save a few cycles. basic_buffer(std::size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {} - basic_buffer(T *p = FMT_NULL, std::size_t sz = 0, - std::size_t cap = 0) FMT_NOEXCEPT : ptr_(p), - size_(sz), - capacity_(cap) {} + basic_buffer(T *p = FMT_NULL, std::size_t sz = 0, std::size_t cap = 0) + FMT_NOEXCEPT : ptr_(p), + size_(sz), + capacity_(cap) {} /** Sets the buffer data and capacity. */ void set(T *buf_data, std::size_t buf_capacity) FMT_NOEXCEPT { @@ -491,8 +492,10 @@ struct compile_string {}; template struct is_compile_string : std::is_base_of {}; -template ::value>::type> +template < + typename S, + typename Enable = + typename std::enable_if::value>::type> FMT_CONSTEXPR basic_string_view to_string_view( const S &s) { return s; @@ -521,8 +524,10 @@ struct formatter { template struct convert_to_int - : std::integral_constant::value && - std::is_convertible::value> {}; + : std::integral_constant< + bool, + !std::is_arithmetic::value && std::is_convertible::value> { +}; namespace internal { @@ -534,11 +539,11 @@ using fmt::v5::to_string_view; // Specifies whether S is a string type convertible to fmt::basic_string_view. template -struct is_string - : std::integral_constant< - bool, !std::is_same()))>::value> { -}; +struct is_string : std::integral_constant< + bool, + !std::is_same< + dummy_string_view, + decltype(to_string_view(declval()))>::value> {}; template struct char_t { @@ -623,13 +628,13 @@ class value { value(long double val) { long_double_value = val; } value(const char_type *val) { string.value = val; } value(const signed char *val) { - static_assert(std::is_same::value, - "incompatible string types"); + static_assert( + std::is_same::value, "incompatible string types"); sstring.value = val; } value(const unsigned char *val) { - static_assert(std::is_same::value, - "incompatible string types"); + static_assert( + std::is_same::value, "incompatible string types"); ustring.value = val; } value(basic_string_view val) { @@ -697,13 +702,16 @@ FMT_MAKE_VALUE_SAME(uint_type, unsigned) // either to int or to long long depending on its size. typedef std::conditional::type long_type; -FMT_MAKE_VALUE((sizeof(long) == sizeof(int) ? int_type : long_long_type), long, - long_type) -typedef std::conditional::type ulong_type; -FMT_MAKE_VALUE((sizeof(unsigned long) == sizeof(unsigned) ? uint_type - : ulong_long_type), - unsigned long, ulong_type) +FMT_MAKE_VALUE( + (sizeof(long) == sizeof(int) ? int_type : long_long_type), long, long_type) +typedef std::conditional< + sizeof(unsigned long) == sizeof(unsigned), + unsigned, + unsigned long long>::type ulong_type; +FMT_MAKE_VALUE( + (sizeof(unsigned long) == sizeof(unsigned) ? uint_type : ulong_long_type), + unsigned long, + ulong_type) FMT_MAKE_VALUE_SAME(long_long_type, long long) FMT_MAKE_VALUE_SAME(ulong_long_type, unsigned long long) @@ -712,18 +720,18 @@ FMT_MAKE_VALUE(uint_type, unsigned char, unsigned) // This doesn't use FMT_MAKE_VALUE because of ambiguity in gcc 4.4. template -FMT_CONSTEXPR - typename std::enable_if::value, - init>::type - make_value(Char val) { +FMT_CONSTEXPR typename std::enable_if< + std::is_same::value, + init>::type +make_value(Char val) { return val; } template -FMT_CONSTEXPR - typename std::enable_if::value, - init>::type - make_value(char val) { +FMT_CONSTEXPR typename std::enable_if< + !std::is_same::value, + init>::type +make_value(char val) { return val; } @@ -733,21 +741,24 @@ FMT_MAKE_VALUE_SAME(long_double_type, long double) // Formatting of wide strings into a narrow buffer and multibyte strings // into a wide buffer is disallowed (https://github.com/fmtlib/fmt/pull/606). -FMT_MAKE_VALUE(cstring_type, typename C::char_type *, - const typename C::char_type *) -FMT_MAKE_VALUE(cstring_type, const typename C::char_type *, - const typename C::char_type *) +FMT_MAKE_VALUE( + cstring_type, typename C::char_type *, const typename C::char_type *) +FMT_MAKE_VALUE( + cstring_type, const typename C::char_type *, const typename C::char_type *) FMT_MAKE_VALUE(cstring_type, signed char *, const signed char *) FMT_MAKE_VALUE_SAME(cstring_type, const signed char *) FMT_MAKE_VALUE(cstring_type, unsigned char *, const unsigned char *) FMT_MAKE_VALUE_SAME(cstring_type, const unsigned char *) FMT_MAKE_VALUE_SAME(string_type, basic_string_view) -FMT_MAKE_VALUE(string_type, - typename basic_string_view::type, - basic_string_view) -FMT_MAKE_VALUE(string_type, const std::basic_string &, - basic_string_view) +FMT_MAKE_VALUE( + string_type, + typename basic_string_view::type, + basic_string_view) +FMT_MAKE_VALUE( + string_type, + const std::basic_string &, + basic_string_view) FMT_MAKE_VALUE(pointer_type, void *, const void *) FMT_MAKE_VALUE_SAME(pointer_type, const void *) @@ -766,11 +777,10 @@ make_value(const T *) { } template -inline - typename std::enable_if::value && - convert_to_int::value, - init>::type - make_value(const T &val) { +inline typename std::enable_if< + std::is_enum::value && convert_to_int::value, + init>::type +make_value(const T &val) { return static_cast(val); } @@ -810,9 +820,10 @@ FMT_CONSTEXPR11 typename std::enable_if< init, string_type>>::type make_value(const S &val) { // Handle adapted strings. - static_assert(std::is_same::type>::value, - "mismatch between char-types of context and argument"); + static_assert( + std::is_same::type>:: + value, + "mismatch between char-types of context and argument"); return to_string_view(val); } @@ -906,8 +917,8 @@ FMT_CONSTEXPR typename internal::result_of::type visit_format_arg( case internal::cstring_type: return vis(arg.value_.string.value); case internal::string_type: - return vis(basic_string_view(arg.value_.string.value, - arg.value_.string.size)); + return vis(basic_string_view( + arg.value_.string.value, arg.value_.string.size)); case internal::pointer_type: return vis(arg.value_.pointer); case internal::custom_type: @@ -934,8 +945,8 @@ class basic_parse_context : private ErrorHandler { typedef Char char_type; typedef typename basic_string_view::iterator iterator; - explicit FMT_CONSTEXPR basic_parse_context(basic_string_view format_str, - ErrorHandler eh = ErrorHandler()) + explicit FMT_CONSTEXPR basic_parse_context( + basic_string_view format_str, ErrorHandler eh = ErrorHandler()) : ErrorHandler(eh), format_str_(format_str), next_arg_id_(0) {} // Returns an iterator to the beginning of the format string range being @@ -1049,9 +1060,11 @@ class context_base { typedef Char char_type; typedef basic_format_arg format_arg; - context_base(OutputIt out, basic_string_view format_str, - basic_format_args ctx_args, - locale_ref loc = locale_ref()) + context_base( + OutputIt out, + basic_string_view format_str, + basic_format_args ctx_args, + locale_ref loc = locale_ref()) : parse_context_(format_str), out_(out), args_(ctx_args), loc_(loc) {} // Returns the argument with specified index. @@ -1131,8 +1144,8 @@ make_arg(const T &value) { // Formatting context. template class basic_format_context - : public internal::context_base< - OutputIt, basic_format_context, Char> { + : public internal:: + context_base, Char> { public: /** The character type for the output. */ typedef Char char_type; @@ -1160,9 +1173,11 @@ class basic_format_context Constructs a ``basic_format_context`` object. References to the arguments are stored in the object so make sure they have appropriate lifetimes. */ - basic_format_context(OutputIt out, basic_string_view format_str, - basic_format_args ctx_args, - internal::locale_ref loc = internal::locale_ref()) + basic_format_context( + OutputIt out, + basic_string_view format_str, + basic_format_args ctx_args, + internal::locale_ref loc = internal::locale_ref()) : base(out, format_str, ctx_args, loc) {} format_arg next_arg() { @@ -1178,7 +1193,8 @@ class basic_format_context template struct buffer_context { typedef basic_format_context< - std::back_insert_iterator>, Char> + std::back_insert_iterator>, + Char> type; }; typedef buffer_context::type format_context; @@ -1199,8 +1215,10 @@ class format_arg_store { // Packed is a macro on MinGW so use IS_PACKED instead. static const bool IS_PACKED = NUM_ARGS < internal::max_packed_args; - typedef typename std::conditional, - basic_format_arg>::type value_type; + typedef typename std::conditional< + IS_PACKED, + internal::value, + basic_format_arg>::type value_type; // If the arguments are not packed, add one more element to mark the end. static const size_t DATA_SIZE = @@ -1279,8 +1297,8 @@ class basic_format_args { typename internal::type type(unsigned index) const { unsigned shift = index * 4; - return static_cast((types_ & (0xfull << shift)) >> - shift); + return static_cast( + (types_ & (0xfull << shift)) >> shift); } friend class internal::arg_map; @@ -1366,13 +1384,14 @@ struct wformat_args : basic_format_args { #if FMT_USE_ALIAS_TEMPLATES /** String's character type. */ template -using char_t = FMT_ENABLE_IF_T(internal::is_string::value, - typename internal::char_t::type); +using char_t = FMT_ENABLE_IF_T( + internal::is_string::value, typename internal::char_t::type); #define FMT_CHAR(S) fmt::char_t #else template -struct char_t : std::enable_if::value, - typename internal::char_t::type> {}; +struct char_t : std::enable_if< + internal::is_string::value, + typename internal::char_t::type> {}; #define FMT_CHAR(S) typename char_t::type #endif @@ -1430,7 +1449,8 @@ std::basic_string vformat( template typename buffer_context::type::iterator vformat_to( - internal::basic_buffer &buf, basic_string_view format_str, + internal::basic_buffer &buf, + basic_string_view format_str, basic_format_args::type> args); } // namespace internal @@ -1468,21 +1488,26 @@ struct is_contiguous> : std::true_type {}; /** Formats a string and writes the output to ``out``. */ template -typename std::enable_if::value, - std::back_insert_iterator>::type -vformat_to(std::back_insert_iterator out, const S &format_str, - basic_format_args::type> args) { +typename std::enable_if< + is_contiguous::value, + std::back_insert_iterator>::type +vformat_to( + std::back_insert_iterator out, + const S &format_str, + basic_format_args::type> args) { internal::container_buffer buf(internal::get_container(out)); internal::vformat_to(buf, to_string_view(format_str), args); return out; } template -inline typename std::enable_if::value && - internal::is_string::value, - std::back_insert_iterator>::type -format_to(std::back_insert_iterator out, const S &format_str, - const Args &... args) { +inline typename std::enable_if< + is_contiguous::value && internal::is_string::value, + std::back_insert_iterator>::type +format_to( + std::back_insert_iterator out, + const S &format_str, + const Args &... args) { internal::checked_args ca(format_str, args...); return vformat_to(out, to_string_view(format_str), *ca); } @@ -1505,8 +1530,8 @@ inline std::basic_string vformat( \endrst */ template -inline std::basic_string format(const S &format_str, - const Args &... args) { +inline std::basic_string format( + const S &format_str, const Args &... args) { return internal::vformat( to_string_view(format_str), *internal::checked_args(format_str, args...)); @@ -1529,8 +1554,10 @@ FMT_API void vprint(std::FILE *f, wstring_view format_str, wformat_args args); template inline FMT_ENABLE_IF_T(internal::is_string::value, void) print(std::FILE *f, const S &format_str, const Args &... args) { - vprint(f, to_string_view(format_str), - internal::checked_args(format_str, args...)); + vprint( + f, + to_string_view(format_str), + internal::checked_args(format_str, args...)); } FMT_API void vprint(string_view format_str, format_args args); @@ -1548,8 +1575,9 @@ FMT_API void vprint(wstring_view format_str, wformat_args args); template inline FMT_ENABLE_IF_T(internal::is_string::value, void) print(const S &format_str, const Args &... args) { - vprint(to_string_view(format_str), - internal::checked_args(format_str, args...)); + vprint( + to_string_view(format_str), + internal::checked_args(format_str, args...)); } FMT_END_NAMESPACE diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index eb5e3fb8..df1b2cf8 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -95,8 +95,8 @@ typedef void (*FormatFunc)(internal::buffer &, int, string_view); // ERANGE - buffer is not large enough to store the error message // other - failure // Buffer should be at least of size 1. -int safe_strerror(int error_code, char *&buffer, - std::size_t buffer_size) FMT_NOEXCEPT { +int safe_strerror(int error_code, char *&buffer, std::size_t buffer_size) + FMT_NOEXCEPT { FMT_ASSERT(buffer != FMT_NULL && buffer_size != 0, "invalid buffer"); class dispatcher { @@ -153,8 +153,8 @@ int safe_strerror(int error_code, char *&buffer, return dispatcher(error_code, buffer, buffer_size).run(); } -void format_error_code(internal::buffer &out, int error_code, - string_view message) FMT_NOEXCEPT { +void format_error_code( + internal::buffer &out, int error_code, string_view message) FMT_NOEXCEPT { // Report error code making sure that the output fits into // inline_buffer_size to avoid dynamic memory allocation and potential // bad_alloc. @@ -180,8 +180,8 @@ void format_error_code(internal::buffer &out, int error_code, assert(out.size() <= inline_buffer_size); } -void report_error(FormatFunc func, int error_code, - string_view message) FMT_NOEXCEPT { +void report_error(FormatFunc func, int error_code, string_view message) + FMT_NOEXCEPT { memory_buffer full_message; func(full_message, error_code, message); // Use Writer::data instead of Writer::c_str to avoid potential memory @@ -228,8 +228,8 @@ FMT_FUNC Char internal::thousands_sep_impl(locale_ref) { } #endif -FMT_FUNC void system_error::init(int err_code, string_view format_str, - format_args args) { +FMT_FUNC void system_error::init( + int err_code, string_view format_str, format_args args) { error_code_ = err_code; memory_buffer buffer; format_system_error(buffer, err_code, vformat(format_str, args)); @@ -239,17 +239,19 @@ FMT_FUNC void system_error::init(int err_code, string_view format_str, namespace internal { template -int char_traits::format_float(char *buf, std::size_t size, - const char *format, int precision, - T value) { +int char_traits::format_float( + char *buf, std::size_t size, const char *format, int precision, T value) { return precision < 0 ? FMT_SNPRINTF(buf, size, format, value) : FMT_SNPRINTF(buf, size, format, precision, value); } template -int char_traits::format_float(wchar_t *buf, std::size_t size, - const wchar_t *format, int precision, - T value) { +int char_traits::format_float( + wchar_t *buf, + std::size_t size, + const wchar_t *format, + int precision, + T value) { return precision < 0 ? FMT_SWPRINTF(buf, size, format, value) : FMT_SWPRINTF(buf, size, format, precision, value); } @@ -276,7 +278,9 @@ const uint32_t basic_data::ZERO_OR_POWERS_OF_10_32[] = {0, template const uint64_t basic_data::ZERO_OR_POWERS_OF_10_64[] = { - 0, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ull), + 0, + FMT_POWERS_OF_10(1), + FMT_POWERS_OF_10(1000000000ull), 10000000000000000000ull}; // Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340. @@ -451,9 +455,15 @@ FMT_FUNC fp get_cached_power(int min_exponent, int &pow10_exponent) { return fp(data::POW10_SIGNIFICANDS[index], data::POW10_EXPONENTS[index]); } -FMT_FUNC bool grisu2_round(char *buf, ptrdiff_t &size, size_t max_digits, - uint64_t delta, uint64_t remainder, uint64_t exp, - uint64_t diff, int &exp10) { +FMT_FUNC bool grisu2_round( + char *buf, + ptrdiff_t &size, + size_t max_digits, + uint64_t delta, + uint64_t remainder, + uint64_t exp, + uint64_t diff, + int &exp10) { while ( remainder < diff && delta - remainder >= exp && (remainder + exp < diff || diff - remainder > remainder + exp - diff)) { @@ -470,10 +480,16 @@ FMT_FUNC bool grisu2_round(char *buf, ptrdiff_t &size, size_t max_digits, } // Generates output using Grisu2 digit-gen algorithm. -FMT_FUNC bool grisu2_gen_digits(char *buf, ptrdiff_t &size, uint32_t hi, - uint64_t lo, int &exp, uint64_t delta, - const fp &one, const fp &diff, - size_t max_digits) { +FMT_FUNC bool grisu2_gen_digits( + char *buf, + ptrdiff_t &size, + uint32_t hi, + uint64_t lo, + int &exp, + uint64_t delta, + const fp &one, + const fp &diff, + size_t max_digits) { // Generate digits for the most significant part (hi). while (exp > 0) { uint32_t digit = 0; @@ -529,8 +545,13 @@ FMT_FUNC bool grisu2_gen_digits(char *buf, ptrdiff_t &size, uint32_t hi, uint64_t remainder = (static_cast(hi) << -one.e) + lo; if (remainder <= delta || size > static_cast(max_digits)) { return grisu2_round( - buf, size, max_digits, delta, remainder, - static_cast(data::POWERS_OF_10_32[exp]) << -one.e, diff.f, + buf, + size, + max_digits, + delta, + remainder, + static_cast(data::POWERS_OF_10_32[exp]) << -one.e, + diff.f, exp); } } @@ -544,8 +565,15 @@ FMT_FUNC bool grisu2_gen_digits(char *buf, ptrdiff_t &size, uint32_t hi, lo &= one.f - 1; --exp; if (lo < delta || size > static_cast(max_digits)) { - return grisu2_round(buf, size, max_digits, delta, lo, one.f, - diff.f * data::POWERS_OF_10_32[-exp], exp); + return grisu2_round( + buf, + size, + max_digits, + delta, + lo, + one.f, + diff.f * data::POWERS_OF_10_32[-exp], + exp); } } } @@ -636,8 +664,8 @@ struct fill { // The number is given as v = f * pow(10, exp), where f has size digits. template -FMT_FUNC void grisu2_prettify(const gen_digits_params ¶ms, int size, - int exp, Handler &&handler) { +FMT_FUNC void grisu2_prettify( + const gen_digits_params ¶ms, int size, int exp, Handler &&handler) { if (!params.fixed) { // Insert a decimal point after the first digit and add an exponent. handler.insert(1, '.'); @@ -692,8 +720,8 @@ struct char_counter { // Converts format specifiers into parameters for digit generation and computes // output buffer size for a number in the range [pow(10, exp - 1), pow(10, exp) // or 0 if exp == 1. -FMT_FUNC gen_digits_params process_specs(const core_format_specs &specs, - int exp, buffer &buf) { +FMT_FUNC gen_digits_params +process_specs(const core_format_specs &specs, int exp, buffer &buf) { auto params = gen_digits_params(); int num_digits = specs.precision >= 0 ? specs.precision : 6; switch (specs.type) { @@ -754,7 +782,8 @@ grisu2_format(Double value, buffer &buf, core_format_specs specs) { const int min_exp = -60; // alpha in Grisu. int cached_exp = 0; // K in Grisu. auto cached_pow = get_cached_power( // \tilde{c}_{-k} in Grisu. - min_exp - (upper.e + fp::significand_size), cached_exp); + min_exp - (upper.e + fp::significand_size), + cached_exp); cached_exp = -cached_exp; upper = upper * cached_pow; // \tilde{M}^+ in Grisu. --upper.f; // \tilde{M}^+ - 1 ulp -> M^+_{\downarrow}. @@ -774,8 +803,8 @@ grisu2_format(Double value, buffer &buf, core_format_specs specs) { // lo = supper % one. uint64_t lo = upper.f & (one.f - 1); ptrdiff_t size = 0; - if (!grisu2_gen_digits(buf.data(), size, hi, lo, exp, delta, one, diff, - params.num_digits)) { + if (!grisu2_gen_digits( + buf.data(), size, hi, lo, exp, delta, one, diff, params.num_digits)) { buf.clear(); return false; } @@ -784,8 +813,8 @@ grisu2_format(Double value, buffer &buf, core_format_specs specs) { } template -void sprintf_format(Double value, internal::buffer &buf, - core_format_specs spec) { +void sprintf_format( + Double value, internal::buffer &buf, core_format_specs spec) { // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail. FMT_ASSERT(buf.capacity() != 0, "empty buffer"); @@ -842,13 +871,13 @@ FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) { return; } - int length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), - s_size, FMT_NULL, 0); + int length = MultiByteToWideChar( + CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, FMT_NULL, 0); if (length == 0) FMT_THROW(windows_error(GetLastError(), ERROR_MSG)); buffer_.resize(length + 1); - length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, - &buffer_[0], length); + length = MultiByteToWideChar( + CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, &buffer_[0], length); if (length == 0) FMT_THROW(windows_error(GetLastError(), ERROR_MSG)); buffer_[length] = 0; @@ -856,8 +885,8 @@ FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) { FMT_FUNC internal::utf16_to_utf8::utf16_to_utf8(wstring_view s) { if (int error_code = convert(s)) { - FMT_THROW(windows_error(error_code, - "cannot convert string from UTF-16 to UTF-8")); + FMT_THROW(windows_error( + error_code, "cannot convert string from UTF-16 to UTF-8")); } } @@ -872,21 +901,21 @@ FMT_FUNC int internal::utf16_to_utf8::convert(wstring_view s) { return 0; } - int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, FMT_NULL, 0, - FMT_NULL, FMT_NULL); + int length = WideCharToMultiByte( + CP_UTF8, 0, s.data(), s_size, FMT_NULL, 0, FMT_NULL, FMT_NULL); if (length == 0) return GetLastError(); buffer_.resize(length + 1); - length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, &buffer_[0], - length, FMT_NULL, FMT_NULL); + length = WideCharToMultiByte( + CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, FMT_NULL, FMT_NULL); if (length == 0) return GetLastError(); buffer_[length] = 0; return 0; } -FMT_FUNC void windows_error::init(int err_code, string_view format_str, - format_args args) { +FMT_FUNC void windows_error::init( + int err_code, string_view format_str, format_args args) { error_code_ = err_code; memory_buffer buffer; internal::format_windows_error(buffer, err_code, vformat(format_str, args)); @@ -894,18 +923,21 @@ FMT_FUNC void windows_error::init(int err_code, string_view format_str, base = std::runtime_error(to_string(buffer)); } -FMT_FUNC void internal::format_windows_error(internal::buffer &out, - int error_code, - string_view message) FMT_NOEXCEPT { +FMT_FUNC void internal::format_windows_error( + internal::buffer &out, int error_code, string_view message) FMT_NOEXCEPT { FMT_TRY { wmemory_buffer buf; buf.resize(inline_buffer_size); for (;;) { wchar_t *system_message = &buf[0]; int result = FormatMessageW( - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, FMT_NULL, - error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message, - static_cast(buf.size()), FMT_NULL); + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + FMT_NULL, + error_code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + system_message, + static_cast(buf.size()), + FMT_NULL); if (result != 0) { utf16_to_utf8 utf8_message; if (utf8_message.convert(system_message) == ERROR_SUCCESS) { @@ -928,8 +960,8 @@ FMT_FUNC void internal::format_windows_error(internal::buffer &out, #endif // FMT_USE_WINDOWS_H -FMT_FUNC void format_system_error(internal::buffer &out, int error_code, - string_view message) FMT_NOEXCEPT { +FMT_FUNC void format_system_error( + internal::buffer &out, int error_code, string_view message) FMT_NOEXCEPT { FMT_TRY { memory_buffer buf; buf.resize(inline_buffer_size); @@ -956,22 +988,22 @@ FMT_FUNC void internal::error_handler::on_error(const char *message) { FMT_THROW(format_error(message)); } -FMT_FUNC void report_system_error(int error_code, - fmt::string_view message) FMT_NOEXCEPT { +FMT_FUNC void report_system_error(int error_code, fmt::string_view message) + FMT_NOEXCEPT { report_error(format_system_error, error_code, message); } #if FMT_USE_WINDOWS_H -FMT_FUNC void report_windows_error(int error_code, - fmt::string_view message) FMT_NOEXCEPT { +FMT_FUNC void report_windows_error(int error_code, fmt::string_view message) + FMT_NOEXCEPT { report_error(internal::format_windows_error, error_code, message); } #endif FMT_FUNC void vprint(std::FILE *f, string_view format_str, format_args args) { memory_buffer buffer; - internal::vformat_to(buffer, format_str, - basic_format_args::type>(args)); + internal::vformat_to( + buffer, format_str, basic_format_args::type>(args)); std::fwrite(buffer.data(), 1, buffer.size(), f); } diff --git a/include/fmt/format.h b/include/fmt/format.h index ba60fdb5..1671dfbb 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -130,8 +130,9 @@ FMT_END_NAMESPACE #ifndef FMT_USE_USER_DEFINED_LITERALS // For Intel's compiler and NVIDIA's compiler both it and the system gcc/msc // must support UDLs. -#if (FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 407 || \ - FMT_MSC_VER >= 1900) && \ +#if ( \ + FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 407 || \ + FMT_MSC_VER >= 1900) && \ (!(FMT_ICC_VERSION || FMT_CUDA_VERSION) || FMT_ICC_VERSION >= 1500 || \ FMT_CUDA_VERSION >= 700) #define FMT_USE_USER_DEFINED_LITERALS 1 @@ -398,8 +399,8 @@ struct checked { // Make a checked iterator to avoid warnings on MSVC. template -inline stdext::checked_array_iterator make_checked(T *p, - std::size_t size) { +inline stdext::checked_array_iterator make_checked( + T *p, std::size_t size) { return {p, size}; } #else @@ -418,8 +419,8 @@ template void basic_buffer::append(const U *begin, const U *end) { std::size_t new_size = size_ + internal::to_unsigned(end - begin); reserve(new_size); - std::uninitialized_copy(begin, end, - internal::make_checked(ptr_, capacity_) + size_); + std::uninitialized_copy( + begin, end, internal::make_checked(ptr_, capacity_) + size_); size_ = new_size; } } // namespace internal @@ -439,8 +440,8 @@ class u8string_view : public basic_string_view { u8string_view(const char *s) : basic_string_view(reinterpret_cast(s)) {} u8string_view(const char *s, size_t count) FMT_NOEXCEPT - : basic_string_view(reinterpret_cast(s), - count) {} + : basic_string_view( + reinterpret_cast(s), count) {} }; #if FMT_USE_USER_DEFINED_LITERALS @@ -484,8 +485,10 @@ enum { inline_buffer_size = 500 }; The output can be converted to an ``std::string`` with ``to_string(out)``. \endrst */ -template > +template < + typename T, + std::size_t SIZE = inline_buffer_size, + typename Allocator = std::allocator> class basic_memory_buffer : private Allocator, public internal::basic_buffer { private: @@ -517,8 +520,10 @@ class basic_memory_buffer : private Allocator, std::size_t size = other.size(), capacity = other.capacity(); if (data == other.store_) { this->set(store_, capacity); - std::uninitialized_copy(other.store_, other.store_ + size, - internal::make_checked(store_, capacity)); + std::uninitialized_copy( + other.store_, + other.store_ + size, + internal::make_checked(store_, capacity)); } else { this->set(data, capacity); // Set pointer to the inline array so that delete is not called @@ -562,8 +567,10 @@ void basic_memory_buffer::grow(std::size_t size) { T *old_data = this->data(); T *new_data = internal::allocate(*this, new_capacity); // The following code doesn't throw, so the raw pointer above doesn't leak. - std::uninitialized_copy(old_data, old_data + this->size(), - internal::make_checked(new_data, new_capacity)); + std::uninitialized_copy( + old_data, + old_data + this->size(), + internal::make_checked(new_data, new_capacity)); this->set(new_data, new_capacity); // deallocate must not throw according to the standard, but even if it does, // the buffer already uses the new storage and will deallocate it in @@ -584,33 +591,50 @@ template <> struct char_traits { // Formats a floating-point number. template - FMT_API static int format_float(char *buffer, std::size_t size, - const char *format, int precision, T value); + FMT_API static int format_float( + char *buffer, + std::size_t size, + const char *format, + int precision, + T value); }; template <> struct char_traits { template - FMT_API static int format_float(wchar_t *buffer, std::size_t size, - const wchar_t *format, int precision, - T value); + FMT_API static int format_float( + wchar_t *buffer, + std::size_t size, + const wchar_t *format, + int precision, + T value); }; #if FMT_USE_EXTERN_TEMPLATES -extern template int char_traits::format_float(char *buffer, - std::size_t size, - const char *format, - int precision, - double value); +extern template int char_traits::format_float( + char *buffer, + std::size_t size, + const char *format, + int precision, + double value); extern template int char_traits::format_float( - char *buffer, std::size_t size, const char *format, int precision, + char *buffer, + std::size_t size, + const char *format, + int precision, long double value); extern template int char_traits::format_float( - wchar_t *buffer, std::size_t size, const wchar_t *format, int precision, + wchar_t *buffer, + std::size_t size, + const wchar_t *format, + int precision, double value); extern template int char_traits::format_float( - wchar_t *buffer, std::size_t size, const wchar_t *format, int precision, + wchar_t *buffer, + std::size_t size, + const wchar_t *format, + int precision, long double value); #endif @@ -694,9 +718,10 @@ class truncating_iterator_base { // An output iterator that truncates the output and counts the number of objects // written to it. -template ::value_type>::type> +template < + typename OutputIt, + typename Enable = typename std::is_void< + typename std::iterator_traits::value_type>::type> class truncating_iterator; template @@ -768,8 +793,10 @@ template struct int_traits { // Smallest of uint32_t and uint64_t that is large enough to represent // all values of T. - typedef typename std::conditional::digits <= 32, - uint32_t, uint64_t>::type main_type; + typedef typename std::conditional< + std::numeric_limits::digits <= 32, + uint32_t, + uint64_t>::type main_type; }; // Static data is placed in this class template to allow header-only @@ -838,21 +865,23 @@ inline char8_t to_char8_t(char c) { return static_cast(c); } template struct needs_conversion : std::integral_constant< - bool, std::is_same::value_type, - char>::value && - std::is_same::value> {}; + bool, + std::is_same< + typename std::iterator_traits::value_type, + char>::value && + std::is_same::value> {}; template -typename std::enable_if::value, - OutputIt>::type -copy_str(InputIt begin, InputIt end, OutputIt it) { +typename std::enable_if::value, OutputIt>:: + type + copy_str(InputIt begin, InputIt end, OutputIt it) { return std::copy(begin, end, it); } template -typename std::enable_if::value, - OutputIt>::type -copy_str(InputIt begin, InputIt end, OutputIt it) { +typename std::enable_if::value, OutputIt>:: + type + copy_str(InputIt begin, InputIt end, OutputIt it) { return std::transform(begin, end, it, to_char8_t); } @@ -971,8 +1000,10 @@ class add_thousands_sep { if (++digit_index_ % 3 != 0) return; buffer -= sep_.size(); - std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(), - internal::make_checked(buffer, sep_.size())); + std::uninitialized_copy( + sep_.data(), + sep_.data() + sep_.size(), + internal::make_checked(buffer, sep_.size())); } enum { size = 1 }; @@ -995,8 +1026,8 @@ inline wchar_t thousands_sep(locale_ref loc) { // thousands_sep is a functor that is called after writing each char to // add a thousands separator if necessary. template -inline Char *format_decimal(Char *buffer, UInt value, unsigned num_digits, - ThousandsSep thousands_sep) { +inline Char *format_decimal( + Char *buffer, UInt value, unsigned num_digits, ThousandsSep thousands_sep) { buffer += num_digits; Char *end = buffer; while (value >= 100) { @@ -1021,10 +1052,13 @@ inline Char *format_decimal(Char *buffer, UInt value, unsigned num_digits, return end; } -template -inline Iterator format_decimal(Iterator out, UInt value, unsigned num_digits, - ThousandsSep sep) { +template < + typename OutChar, + typename UInt, + typename Iterator, + typename ThousandsSep> +inline Iterator format_decimal( + Iterator out, UInt value, unsigned num_digits, ThousandsSep sep) { typedef typename ThousandsSep::char_type char_type; // Buffer should be large enough to hold all digits (<= digits10 + 1). enum { max_size = std::numeric_limits::digits10 + 1 }; @@ -1040,8 +1074,8 @@ inline It format_decimal(It out, UInt value, unsigned num_digits) { } template -inline Char *format_uint(Char *buffer, UInt value, unsigned num_digits, - bool upper = false) { +inline Char *format_uint( + Char *buffer, UInt value, unsigned num_digits, bool upper = false) { buffer += num_digits; Char *end = buffer; do { @@ -1053,8 +1087,8 @@ inline Char *format_uint(Char *buffer, UInt value, unsigned num_digits, } template -inline It format_uint(It out, UInt value, unsigned num_digits, - bool upper = false) { +inline It format_uint( + It out, UInt value, unsigned num_digits, bool upper = false) { // Buffer should be large enough to hold all digits (digits / BASE_BITS + 1) // and null. char buffer[std::numeric_limits::digits / BASE_BITS + 2]; @@ -1105,8 +1139,10 @@ class utf16_to_utf8 { FMT_API int convert(wstring_view s); }; -FMT_API void format_windows_error(fmt::internal::buffer &out, int error_code, - fmt::string_view message) FMT_NOEXCEPT; +FMT_API void format_windows_error( + fmt::internal::buffer &out, + int error_code, + fmt::string_view message) FMT_NOEXCEPT; #endif template @@ -1232,8 +1268,8 @@ FMT_CONSTEXPR void handle_float_type_spec(char spec, Handler &&handler) { } template -FMT_CONSTEXPR void handle_char_specs(const basic_format_specs *specs, - Handler &&handler) { +FMT_CONSTEXPR void handle_char_specs( + const basic_format_specs *specs, Handler &&handler) { if (!specs) return handler.on_char(); if (specs->type && specs->type != 'c') @@ -1420,9 +1456,9 @@ class arg_formatter_base { } template - typename std::enable_if::value || - std::is_same::value, - iterator>::type + typename std::enable_if< + std::is_integral::value || std::is_same::value, + iterator>::type operator()(T value) { // MSVC2013 fails to compile separate overloads for bool and char_type so // use std::is_same instead. @@ -1476,8 +1512,8 @@ class arg_formatter_base { iterator operator()(const char_type *value) { if (!specs_) return write(value), out(); - internal::handle_cstring_type_spec(specs_->type, - cstring_spec_handler(*this, value)); + internal::handle_cstring_type_spec( + specs_->type, cstring_spec_handler(*this, value)); return out(); } @@ -1507,9 +1543,8 @@ FMT_CONSTEXPR bool is_name_start(Char c) { // Parses the range [begin, end) as an unsigned integer. This function assumes // that the range is non-empty and the first character is a digit. template -FMT_CONSTEXPR unsigned parse_nonnegative_int(const Char *&begin, - const Char *end, - ErrorHandler &&eh) { +FMT_CONSTEXPR unsigned parse_nonnegative_int( + const Char *&begin, const Char *end, ErrorHandler &&eh) { assert(begin != end && '0' <= *begin && *begin <= '9'); if (*begin == '0') { ++begin; @@ -1712,10 +1747,13 @@ class specs_checker : public Handler { internal::type arg_type_; }; -template