From d07cc2026b822d4116910065936e528255af1a09 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 30 May 2019 07:01:31 -0700 Subject: [PATCH] FMT_EXPLICIT -> explicit, FMT_NULL -> nullptr --- include/fmt/chrono.h | 10 ++--- include/fmt/core.h | 42 +++-------------- include/fmt/format-inl.h | 22 ++++----- include/fmt/format.h | 25 +++++------ include/fmt/posix.h | 10 ++--- include/fmt/prepare.h | 2 +- include/fmt/ranges.h | 4 +- src/posix.cc | 4 +- test/chrono-test.cc | 6 +-- test/core-test.cc | 14 +++--- test/custom-formatter-test.cc | 4 +- test/format | 8 ++-- test/format-impl-test.cc | 6 +-- test/format-test.cc | 36 +++++++-------- test/grisu-test.cc | 4 +- test/gtest-extra-test.cc | 12 ++--- test/gtest-extra.h | 2 +- test/mock-allocator.h | 4 +- test/ostream-test.cc | 2 +- test/posix-mock-test.cc | 16 +++---- test/posix-test.cc | 22 ++++----- test/prepare-test.cc | 2 +- test/printf-test.cc | 12 ++--- test/std-format-test.cc | 85 ++++++++++++++++++----------------- test/util.h | 2 +- 25 files changed, 163 insertions(+), 193 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 926600e22bb3..f57aef387c3f 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -42,7 +42,7 @@ inline std::tm localtime(std::time_t time) { return handle(localtime_r(&time_, &tm_)); } - bool handle(std::tm* tm) { return tm != FMT_NULL; } + bool handle(std::tm* tm) { return tm != nullptr; } bool handle(internal::null<>) { using namespace fmt::internal; @@ -56,7 +56,7 @@ inline std::tm localtime(std::time_t time) { using namespace fmt::internal; std::tm* tm = std::localtime(&time_); if (tm) tm_ = *tm; - return tm != FMT_NULL; + return tm != nullptr; } #endif }; @@ -79,7 +79,7 @@ inline std::tm gmtime(std::time_t time) { return handle(gmtime_r(&time_, &tm_)); } - bool handle(std::tm* tm) { return tm != FMT_NULL; } + bool handle(std::tm* tm) { return tm != nullptr; } bool handle(internal::null<>) { using namespace fmt::internal; @@ -92,7 +92,7 @@ inline std::tm gmtime(std::time_t time) { bool fallback(internal::null<>) { std::tm* tm = std::gmtime(&time_); if (tm) tm_ = *tm; - return tm != FMT_NULL; + return tm != nullptr; } #endif }; @@ -157,7 +157,7 @@ template struct formatter { namespace internal { template FMT_CONSTEXPR const char* get_units() { - return FMT_NULL; + return nullptr; } template <> FMT_CONSTEXPR const char* get_units() { return "as"; } template <> FMT_CONSTEXPR const char* get_units() { return "fs"; } diff --git a/include/fmt/core.h b/include/fmt/core.h index 4283927725e5..6c93d5f61efb 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -89,28 +89,6 @@ # endif #endif -#if FMT_HAS_FEATURE(cxx_explicit_conversions) || FMT_GCC_VERSION >= 405 || \ - FMT_MSC_VER >= 1800 -# define FMT_USE_EXPLICIT 1 -# define FMT_EXPLICIT explicit -#else -# define FMT_USE_EXPLICIT 0 -# define FMT_EXPLICIT -#endif - -#ifndef FMT_NULL -# if FMT_HAS_FEATURE(cxx_nullptr) || \ - (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600 -# define FMT_NULL nullptr -# define FMT_USE_NULLPTR 1 -# else -# define FMT_NULL NULL -# endif -#endif -#ifndef FMT_USE_NULLPTR -# define FMT_USE_NULLPTR 0 -#endif - // Check if exceptions are disabled. #ifndef FMT_EXCEPTIONS # if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || \ @@ -220,11 +198,6 @@ # define FMT_STRING_VIEW std::experimental::basic_string_view #endif -// std::result_of is defined in in gcc 4.4. -#if FMT_GCC_VERSION && FMT_GCC_VERSION <= 404 -# include -#endif - // An enable_if helper to be used in template parameters. enable_if in template // parameters results in much shorter symbols: https://godbolt.org/z/sWw4vP. #define FMT_ENABLE_IF_T(...) typename std::enable_if<(__VA_ARGS__), int>::type @@ -272,7 +245,7 @@ template class buffer { // Don't initialize ptr_ since it is not accessed to save a few cycles. buffer(std::size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {} - buffer(T* p = FMT_NULL, std::size_t sz = 0, std::size_t cap = 0) FMT_NOEXCEPT + buffer(T* p = nullptr, std::size_t sz = 0, std::size_t cap = 0) FMT_NOEXCEPT : ptr_(p), size_(sz), capacity_(cap) {} @@ -379,7 +352,7 @@ typedef char no[2]; template struct is_constructible { template static yes& test(int (*)[sizeof(new U(declval()))]); template static no& test(...); - enum { value = sizeof(test(FMT_NULL)) == sizeof(yes) }; + enum { value = sizeof(test(nullptr)) == sizeof(yes) }; }; #else template @@ -404,7 +377,7 @@ template class basic_string_view { typedef Char char_type; typedef const Char* iterator; - FMT_CONSTEXPR basic_string_view() FMT_NOEXCEPT : data_(FMT_NULL), size_(0) {} + FMT_CONSTEXPR basic_string_view() FMT_NOEXCEPT : data_(nullptr), size_(0) {} /** Constructs a string reference object from a C string and a size. */ FMT_CONSTEXPR basic_string_view(const Char* s, size_t count) FMT_NOEXCEPT @@ -850,10 +823,7 @@ 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*) - -#if FMT_USE_NULLPTR FMT_MAKE_VALUE(pointer_type, std::nullptr_t, const void*) -#endif // Formatting of arbitrary pointers is disallowed. If you want to output a // pointer cast it to "void *" or "const void *". In particular, this forbids @@ -953,7 +923,7 @@ template class basic_format_arg { FMT_CONSTEXPR basic_format_arg() : type_(internal::none_type) {} - FMT_CONSTEXPR FMT_EXPLICIT operator bool() const FMT_NOEXCEPT { + FMT_CONSTEXPR explicit operator bool() const FMT_NOEXCEPT { return type_ != internal::none_type; } @@ -1041,7 +1011,7 @@ template class arg_map { } public: - arg_map() : map_(FMT_NULL), size_(0) {} + arg_map() : map_(nullptr), size_(0) {} void init(const basic_format_args& args); ~arg_map() { delete[] map_; } @@ -1061,7 +1031,7 @@ class locale_ref { friend class locale; public: - locale_ref() : locale_(FMT_NULL) {} + locale_ref() : locale_(nullptr) {} template explicit locale_ref(const Locale& loc); template Locale get() const; diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 372d887ded7c..6c6f5223a8d6 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -98,7 +98,7 @@ typedef void (*FormatFunc)(internal::buffer&, int, string_view); // Buffer should be at least of size 1. 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"); + FMT_ASSERT(buffer != nullptr && buffer_size != 0, "invalid buffer"); class dispatcher { private: @@ -650,9 +650,9 @@ template struct grisu_shortest_handler { // Decrement the generated number approaching value from above. void round(uint64_t d, uint64_t divisor, uint64_t& remainder, uint64_t error) { - while (remainder < d && error - remainder >= divisor && - (remainder + divisor < d || - d - remainder >= remainder + divisor - d)) { + while ( + remainder < d && error - remainder >= divisor && + (remainder + divisor < d || d - remainder >= remainder + divisor - d)) { --buf[size - 1]; remainder += divisor; } @@ -782,7 +782,7 @@ void sprintf_format(Double value, internal::buffer& buf, *format_ptr = '\0'; // Format using snprintf. - char* start = FMT_NULL; + char* start = nullptr; for (;;) { std::size_t buffer_size = buf.capacity(); start = &buf[0]; @@ -839,7 +839,7 @@ FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) { } int length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), - s_size, FMT_NULL, 0); + s_size, nullptr, 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, @@ -865,12 +865,12 @@ 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, nullptr, 0, + nullptr, nullptr); 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, nullptr, nullptr); if (length == 0) return GetLastError(); buffer_[length] = 0; return 0; @@ -894,9 +894,9 @@ FMT_FUNC void internal::format_windows_error(internal::buffer& out, for (;;) { wchar_t* system_message = &buf[0]; int result = FormatMessageW( - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, FMT_NULL, + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message, - static_cast(buf.size()), FMT_NULL); + static_cast(buf.size()), nullptr); if (result != 0) { utf16_to_utf8 utf8_message; if (utf8_message.convert(system_message) == ERROR_SUCCESS) { diff --git a/include/fmt/format.h b/include/fmt/format.h index ca56fab0d3c2..8525639e8c92 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -285,8 +285,7 @@ FMT_CONSTEXPR T* end(T (&array)[N]) FMT_NOEXCEPT { } // An implementation of iterator_t for pre-C++20 compilers such as gcc 4. -template -struct iterator_t { +template struct iterator_t { typedef decltype(internal::begin(internal::declval())) type; }; @@ -2101,7 +2100,7 @@ inline bool find(const char* first, const char* last, char value, const char*& out) { out = static_cast( std::memchr(first, value, internal::to_unsigned(last - first))); - return out != FMT_NULL; + return out != nullptr; } template struct id_adapter { @@ -2123,7 +2122,7 @@ FMT_CONSTEXPR void parse_format_string(basic_string_view format_str, FMT_CONSTEXPR void operator()(const Char* begin, const Char* end) { if (begin == end) return; for (;;) { - const Char* p = FMT_NULL; + const Char* p = nullptr; if (!find(begin, end, '}', p)) return handler_.on_text(begin, end); ++p; @@ -2304,8 +2303,8 @@ class arg_formatter \endrst */ explicit arg_formatter(context_type& ctx, - basic_parse_context* parse_ctx = FMT_NULL, - format_specs* spec = FMT_NULL) + basic_parse_context* parse_ctx = nullptr, + format_specs* spec = nullptr) : base(Range(ctx.out()), spec, ctx.locale()), ctx_(ctx), parse_ctx_(parse_ctx) {} @@ -3040,7 +3039,7 @@ template struct formatter::type, T>::value>::type> { - FMT_CONSTEXPR formatter() : format_str_(FMT_NULL) {} + FMT_CONSTEXPR formatter() : format_str_(nullptr) {} // Parses format specifiers stopping either at the end of the range or at the // terminating '}'. @@ -3104,7 +3103,7 @@ struct formatter range_type; - return visit_format_arg(arg_formatter(ctx, FMT_NULL, &specs_), + return visit_format_arg(arg_formatter(ctx, nullptr, &specs_), internal::make_arg(val)); } @@ -3160,7 +3159,7 @@ template class dynamic_formatter { typedef output_range range; - visit_format_arg(arg_formatter(ctx, FMT_NULL, &specs_), + visit_format_arg(arg_formatter(ctx, nullptr, &specs_), internal::make_arg(val)); return ctx.out(); } @@ -3329,14 +3328,14 @@ arg_join join(It begin, It end, wstring_view sep) { \endrst */ template -arg_join::type, char> - join(const Range& range, string_view sep) { +arg_join::type, char> join( + const Range& range, string_view sep) { return join(internal::begin(range), internal::end(range), sep); } template -arg_join::type, wchar_t> - join(const Range& range, wstring_view sep) { +arg_join::type, wchar_t> join( + const Range& range, wstring_view sep) { return join(internal::begin(range), internal::end(range), sep); } #endif diff --git a/include/fmt/posix.h b/include/fmt/posix.h index b919e25ff16b..68f6995d8007 100644 --- a/include/fmt/posix.h +++ b/include/fmt/posix.h @@ -133,7 +133,7 @@ class buffered_file { public: // Constructs a buffered_file object which doesn't represent any file. - buffered_file() FMT_NOEXCEPT : file_(FMT_NULL) {} + buffered_file() FMT_NOEXCEPT : file_(nullptr) {} // Destroys the object closing the file it represents if any. FMT_API ~buffered_file() FMT_NOEXCEPT; @@ -144,13 +144,13 @@ class buffered_file { public: buffered_file(buffered_file&& other) FMT_NOEXCEPT : file_(other.file_) { - other.file_ = FMT_NULL; + other.file_ = nullptr; } buffered_file& operator=(buffered_file&& other) { close(); file_ = other.file_; - other.file_ = FMT_NULL; + other.file_ = nullptr; return *this; } @@ -295,7 +295,7 @@ class Locale { public: typedef locale_t Type; - Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", FMT_NULL)) { + Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", nullptr)) { if (!locale_) FMT_THROW(system_error(errno, "cannot create locale")); } ~Locale() { freelocale(locale_); } @@ -305,7 +305,7 @@ class Locale { // Converts string to floating-point number and advances str past the end // of the parsed input. double strtod(const char*& str) const { - char* end = FMT_NULL; + char* end = nullptr; double result = strtod_l(str, &end, locale_); str = end; return result; diff --git a/include/fmt/prepare.h b/include/fmt/prepare.h index 6435fb77863d..913632fd894f 100644 --- a/include/fmt/prepare.h +++ b/include/fmt/prepare.h @@ -297,7 +297,7 @@ class prepared_format { check_prepared_specs(specs, arg.type()); advance_parse_context_to_specification(parse_ctx, part); ctx.advance_to( - visit_format_arg(arg_formatter(ctx, FMT_NULL, &specs), arg)); + visit_format_arg(arg_formatter(ctx, nullptr, &specs), arg)); } break; } } diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 06fcaad53025..f061699b4249 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -82,7 +82,7 @@ template class is_like_std_string { public: static FMT_CONSTEXPR_DECL const bool value = - is_string::value || !std::is_void(FMT_NULL))>::value; + is_string::value || !std::is_void(nullptr))>::value; }; template @@ -113,7 +113,7 @@ template class is_tuple_like_ { public: static FMT_CONSTEXPR_DECL const bool value = - !std::is_void(FMT_NULL))>::value; + !std::is_void(nullptr))>::value; }; // Check for integer_sequence diff --git a/src/posix.cc b/src/posix.cc index fd6eb1695dfd..69c27819d2b3 100644 --- a/src/posix.cc +++ b/src/posix.cc @@ -73,7 +73,7 @@ buffered_file::~buffered_file() FMT_NOEXCEPT { buffered_file::buffered_file(cstring_view filename, cstring_view mode) { FMT_RETRY_VAL(file_, FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())), - FMT_NULL); + nullptr); if (!file_) FMT_THROW(system_error(errno, "cannot open file {}", filename.c_str())); } @@ -81,7 +81,7 @@ buffered_file::buffered_file(cstring_view filename, cstring_view mode) { void buffered_file::close() { if (!file_) return; int result = FMT_SYSTEM(fclose(file_)); - file_ = FMT_NULL; + file_ = nullptr; if (result != 0) FMT_THROW(system_error(errno, "cannot close file")); } diff --git a/test/chrono-test.cc b/test/chrono-test.cc index daeca5f4e0f1..abb8cb8bbe1f 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -60,7 +60,7 @@ TEST(TimeTest, GrowBuffer) { std::string s = "{:"; for (int i = 0; i < 30; ++i) s += "%c"; s += "}\n"; - std::time_t t = std::time(FMT_NULL); + std::time_t t = std::time(nullptr); fmt::format(s, *std::localtime(&t)); } @@ -83,13 +83,13 @@ static bool EqualTime(const std::tm& lhs, const std::tm& rhs) { } TEST(TimeTest, LocalTime) { - std::time_t t = std::time(FMT_NULL); + std::time_t t = std::time(nullptr); std::tm tm = *std::localtime(&t); EXPECT_TRUE(EqualTime(tm, fmt::localtime(t))); } TEST(TimeTest, GMTime) { - std::time_t t = std::time(FMT_NULL); + std::time_t t = std::time(nullptr); std::tm tm = *std::gmtime(&t); EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t))); } diff --git a/test/core-test.cc b/test/core-test.cc index 6876ca3271f6..8cd3b047fc6e 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -84,7 +84,7 @@ TEST(BufferTest, Nonmoveable) { // A test buffer with a dummy grow method. template struct test_buffer : buffer { - void grow(std::size_t capacity) { this->set(FMT_NULL, capacity); } + void grow(std::size_t capacity) { this->set(nullptr, capacity); } }; template struct mock_buffer : buffer { @@ -103,7 +103,7 @@ template struct mock_buffer : buffer { TEST(BufferTest, Ctor) { { mock_buffer buffer; - EXPECT_EQ(FMT_NULL, &buffer[0]); + EXPECT_EQ(nullptr, &buffer[0]); EXPECT_EQ(static_cast(0), buffer.size()); EXPECT_EQ(static_cast(0), buffer.capacity()); } @@ -217,7 +217,7 @@ struct custom_context { const char* format(const T&, custom_context& ctx) { ctx.called = true; - return FMT_NULL; + return nullptr; } }; }; @@ -361,8 +361,8 @@ TEST(ArgTest, WStringArg) { } TEST(ArgTest, PointerArg) { - void* p = FMT_NULL; - const void* cp = FMT_NULL; + void* p = nullptr; + const void* cp = nullptr; CHECK_ARG_(char, cp, p); CHECK_ARG_(wchar_t, cp, p); CHECK_ARG(cp, ); @@ -571,7 +571,7 @@ TEST(FormatterTest, FormatImplicitlyConvertibleToStringView) { } // std::is_constructible is broken in MSVC until version 2015. -#if FMT_USE_EXPLICIT && (!FMT_MSC_VER || FMT_MSC_VER >= 1900) +#if !FMT_MSC_VER || FMT_MSC_VER >= 1900 struct explicitly_convertible_to_string_view { explicit operator fmt::string_view() const { return "foo"; } }; @@ -593,7 +593,7 @@ struct explicitly_convertible_to_string_like { template ::value>::type> - FMT_EXPLICIT operator String() const { + explicit operator String() const { return String("foo", 3u); } }; diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index 2fb64a837cef..b9513e15bc3c 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -6,7 +6,7 @@ // For the license information refer to format.h. #ifndef _CRT_SECURE_NO_WARNINGS -# define _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS #endif #include "fmt/format.h" @@ -26,7 +26,7 @@ class custom_arg_formatter custom_arg_formatter(fmt::format_context& ctx, fmt::format_parse_context* parse_ctx, - fmt::format_specs* s = FMT_NULL) + fmt::format_specs* s = nullptr) : base(ctx, parse_ctx, s) {} using base::operator(); diff --git a/test/format b/test/format index f411c8c9ceb0..3aca2f34aff5 100644 --- a/test/format +++ b/test/format @@ -513,7 +513,7 @@ class arg_formatter *spec* contains format specifier information for standard argument types. \endrst */ - arg_formatter(format_context& ctx, parse_context* parse_ctx = FMT_NULL, fmt::format_specs* spec = FMT_NULL) + arg_formatter(format_context& ctx, parse_context* parse_ctx = nullptr, fmt::format_specs* spec = nullptr) : base(Range(ctx.out()), spec, {}), parse_ctx_(parse_ctx), ctx_(ctx) {} using base::operator(); @@ -693,13 +693,13 @@ struct formatter { template auto format(const T& val, FormatContext& ctx) -> decltype(ctx.out()) { fmt::internal::handle_dynamic_spec( - specs_.width_, specs_.width_ref, ctx, FMT_NULL); + specs_.width_, specs_.width_ref, ctx, nullptr); fmt::internal::handle_dynamic_spec( - specs_.precision, specs_.precision_ref, ctx, FMT_NULL); + specs_.precision, specs_.precision_ref, ctx, nullptr); typedef fmt::output_range range_type; - return visit_format_arg(arg_formatter(ctx, FMT_NULL, &specs_), + return visit_format_arg(arg_formatter(ctx, nullptr, &specs_), basic_format_arg(val)); } diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index f12dfe23b82a..25b5aeb0ae26 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -169,9 +169,9 @@ TEST(FormatTest, FormatNegativeNaN) { } TEST(FormatTest, StrError) { - char* message = FMT_NULL; + char* message = nullptr; char buffer[BUFFER_SIZE]; - EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = FMT_NULL, 0), + EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = nullptr, 0), "invalid buffer"); EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = buffer, 0), "invalid buffer"); @@ -259,6 +259,6 @@ TEST(UtilTest, WriteUIntPtr) { fmt::writer writer(buf); writer.write_pointer(fmt::internal::bit_cast( reinterpret_cast(0xface)), - FMT_NULL); + nullptr); EXPECT_EQ("0xface", to_string(buf)); } diff --git a/test/format-test.cc b/test/format-test.cc index 0b4b2176d409..1c187651de4e 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -63,16 +63,12 @@ TEST(FormatterTest, TestFormattersEnabled) { unsigned short, int, unsigned, long, unsigned long, long long, unsigned long long, float, double, long double, void*, const void*, char*, const char*, - std::string>(); + std::string, std::nullptr_t>(); check_enabled_formatters(); -# if FMT_USE_NULLPTR - check_enabled_formatters(); - check_enabled_formatters(); -# endif + const wchar_t*, std::wstring, std::nullptr_t>(); } #endif @@ -192,7 +188,7 @@ TEST(IteratorTest, CountingIterator) { } TEST(IteratorTest, TruncatingIterator) { - char* p = FMT_NULL; + char* p = nullptr; fmt::internal::truncating_iterator it(p, 3); auto prev = it++; EXPECT_EQ(prev.base(), p); @@ -254,7 +250,7 @@ TEST(AllocatorTest, allocator_ref) { test_allocator_ref ref2(ref); check_forwarding(alloc, ref2); test_allocator_ref ref3; - EXPECT_EQ(FMT_NULL, ref3.get()); + EXPECT_EQ(nullptr, ref3.get()); ref3 = ref; check_forwarding(alloc, ref3); } @@ -270,7 +266,7 @@ static void check_move_buffer( EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size())); EXPECT_EQ(5u, buffer2.capacity()); // Move should transfer allocator. - EXPECT_EQ(FMT_NULL, buffer.get_allocator().get()); + EXPECT_EQ(nullptr, buffer.get_allocator().get()); EXPECT_EQ(alloc, buffer2.get_allocator().get()); } @@ -353,7 +349,7 @@ TEST(MemoryBufferTest, Grow) { TEST(MemoryBufferTest, Allocator) { typedef allocator_ref> TestAllocator; basic_memory_buffer buffer; - EXPECT_EQ(FMT_NULL, buffer.get_allocator().get()); + EXPECT_EQ(nullptr, buffer.get_allocator().get()); StrictMock> alloc; char mem; { @@ -493,7 +489,7 @@ TEST(UtilTest, FormatSystemError) { fmt::print("warning: std::allocator allocates {} chars", max_size); return; } - fmt::format_system_error(message, EDOM, fmt::string_view(FMT_NULL, max_size)); + fmt::format_system_error(message, EDOM, fmt::string_view(nullptr, max_size)); EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message)); } @@ -1548,7 +1544,7 @@ TEST(FormatterTest, FormatCString) { EXPECT_EQ("test", format("{0:s}", "test")); char nonconst[] = "nonconst"; EXPECT_EQ("nonconst", format("{0}", nonconst)); - EXPECT_THROW_MSG(format("{0}", static_cast(FMT_NULL)), + EXPECT_THROW_MSG(format("{0}", static_cast(nullptr)), format_error, "string pointer is null"); } @@ -1570,7 +1566,7 @@ TEST(FormatterTest, FormatUCharString) { TEST(FormatterTest, FormatPointer) { check_unknown_types(reinterpret_cast(0x1234), "p", "pointer"); - EXPECT_EQ("0x0", format("{0}", static_cast(FMT_NULL))); + EXPECT_EQ("0x0", format("{0}", static_cast(nullptr))); EXPECT_EQ("0x1234", format("{0}", reinterpret_cast(0x1234))); EXPECT_EQ("0x1234", format("{0:p}", reinterpret_cast(0x1234))); EXPECT_EQ("0x" + std::string(sizeof(void*) * CHAR_BIT / 4, 'f'), @@ -1581,7 +1577,7 @@ TEST(FormatterTest, FormatPointer) { std::shared_ptr sp(new int(1)); EXPECT_EQ(format("{}", fmt::ptr(sp.get())), format("{}", fmt::ptr(sp))); #if FMT_USE_NULLPTR - EXPECT_EQ("0x0", format("{}", FMT_NULL)); + EXPECT_EQ("0x0", format("{}", nullptr)); #endif } @@ -1674,7 +1670,7 @@ TEST(FormatterTest, FormatExamples) { FILE* ftest = safe_fopen(filename, "r"); if (ftest) fclose(ftest); int error_code = errno; - EXPECT_TRUE(ftest == FMT_NULL); + EXPECT_TRUE(ftest == nullptr); EXPECT_SYSTEM_ERROR( { FILE* f = safe_fopen(filename, "r"); @@ -1905,7 +1901,7 @@ class mock_arg_formatter typedef buffer_range range; mock_arg_formatter(fmt::format_context& ctx, fmt::format_parse_context*, - fmt::format_specs* s = FMT_NULL) + fmt::format_specs* s = nullptr) : base(fmt::internal::get_container(ctx.out()), s, ctx.locale()) { EXPECT_CALL(*this, call(42)); } @@ -2170,8 +2166,8 @@ struct test_parse_context { FMT_CONSTEXPR unsigned next_arg_id() { return 11; } template FMT_CONSTEXPR void check_arg_id(Id) {} - FMT_CONSTEXPR const char* begin() { return FMT_NULL; } - FMT_CONSTEXPR const char* end() { return FMT_NULL; } + FMT_CONSTEXPR const char* begin() { return nullptr; } + FMT_CONSTEXPR const char* end() { return nullptr; } void on_error(const char*) {} }; @@ -2337,7 +2333,7 @@ FMT_CONSTEXPR bool equal(const char* s1, const char* s2) { template FMT_CONSTEXPR bool test_error(const char* fmt, const char* expected_error) { - const char* actual_error = FMT_NULL; + const char* actual_error = nullptr; fmt::internal::do_check_format_string( string_view(fmt, len(fmt)), test_error_handler(actual_error)); return equal(actual_error, expected_error); @@ -2349,7 +2345,7 @@ FMT_CONSTEXPR bool test_error(const char* fmt, const char* expected_error) { static_assert(test_error<__VA_ARGS__>(fmt, error), "") TEST(FormatTest, FormatStringErrors) { - EXPECT_ERROR_NOARGS("foo", FMT_NULL); + EXPECT_ERROR_NOARGS("foo", nullptr); EXPECT_ERROR_NOARGS("}", "unmatched '}' in format string"); EXPECT_ERROR("{0:s", "unknown format specifier", Date); # if FMT_MSC_VER >= 1916 diff --git a/test/grisu-test.cc b/test/grisu-test.cc index 8e3c6dc1cc56..07c344d395fb 100644 --- a/test/grisu-test.cc +++ b/test/grisu-test.cc @@ -53,6 +53,4 @@ TEST(GrisuTest, Prettify) { EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6)); } -TEST(GrisuTest, ZeroPrecision) { - EXPECT_EQ("1", fmt::format("{:.0}", 1.0)); -} +TEST(GrisuTest, ZeroPrecision) { EXPECT_EQ("1", fmt::format("{:.0}", 1.0)); } diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index eee949a22bae..3f8fd5ea4b9d 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -54,7 +54,9 @@ void do_nothing() {} FMT_NORETURN void throw_exception() { throw std::runtime_error("test"); } -FMT_NORETURN void throw_system_error() { throw fmt::system_error(EDOM, "test"); } +FMT_NORETURN void throw_system_error() { + throw fmt::system_error(EDOM, "test"); +} // Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument // exactly once. @@ -355,10 +357,10 @@ TEST(OutputRedirectTest, FlushErrorInCtor) { // Put a character in a file buffer. EXPECT_EQ('x', fputc('x', f.get())); FMT_POSIX(close(write_fd)); - std::unique_ptr redir{FMT_NULL}; + std::unique_ptr redir{nullptr}; EXPECT_SYSTEM_ERROR_NOASSERT(redir.reset(new OutputRedirect(f.get())), EBADF, "cannot flush stream"); - redir.reset(FMT_NULL); + redir.reset(nullptr); write_copy.dup2(write_fd); // "undo" close or dtor will fail } @@ -367,7 +369,7 @@ TEST(OutputRedirectTest, DupErrorInCtor) { int fd = (f.fileno)(); file copy = file::dup(fd); FMT_POSIX(close(fd)); - std::unique_ptr redir{FMT_NULL}; + std::unique_ptr redir{nullptr}; EXPECT_SYSTEM_ERROR_NOASSERT( redir.reset(new OutputRedirect(f.get())), EBADF, fmt::format("cannot duplicate file descriptor {}", fd)); @@ -420,7 +422,7 @@ TEST(OutputRedirectTest, ErrorInDtor) { // redirecting the output in EXPECT_STDERR and the second close // will break output redirection. FMT_POSIX(close(write_fd)); - SUPPRESS_ASSERT(redir.reset(FMT_NULL)); + SUPPRESS_ASSERT(redir.reset(nullptr)); }, format_system_error(EBADF, "cannot flush stream")); write_copy.dup2(write_fd); // "undo" close or dtor of buffered_file will fail diff --git a/test/gtest-extra.h b/test/gtest-extra.h index b81b11cc6734..4a693361001e 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -155,7 +155,7 @@ std::string read(fmt::file& f, std::size_t count); template struct ScopedMock : testing::StrictMock { ScopedMock() { Mock::instance = this; } - ~ScopedMock() { Mock::instance = FMT_NULL; } + ~ScopedMock() { Mock::instance = nullptr; } }; #endif // FMT_GTEST_EXTRA_H_ diff --git a/test/mock-allocator.h b/test/mock-allocator.h index 697504ce9486..41e2e270babb 100644 --- a/test/mock-allocator.h +++ b/test/mock-allocator.h @@ -26,13 +26,13 @@ template class allocator_ref { void move(allocator_ref& other) { alloc_ = other.alloc_; - other.alloc_ = FMT_NULL; + other.alloc_ = nullptr; } public: typedef typename Allocator::value_type value_type; - explicit allocator_ref(Allocator* alloc = FMT_NULL) : alloc_(alloc) {} + explicit allocator_ref(Allocator* alloc = nullptr) : alloc_(alloc) {} allocator_ref(const allocator_ref& other) : alloc_(other.alloc_) {} allocator_ref(allocator_ref&& other) { move(other); } diff --git a/test/ostream-test.cc b/test/ostream-test.cc index b566637be8c3..3527958b704a 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -167,7 +167,7 @@ TEST(OStreamTest, WriteToOStreamMaxSize) { } os(streambuf); testing::InSequence sequence; - const char* data = FMT_NULL; + const char* data = nullptr; typedef std::make_unsigned::type ustreamsize; ustreamsize size = max_size; do { diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index fc62627fd0e0..1cee9fd109fd 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -131,7 +131,7 @@ int test::dup2(int fildes, int fildes2) { } FILE* test::fdopen(int fildes, const char* mode) { - EMULATE_EINTR(fdopen, FMT_NULL); + EMULATE_EINTR(fdopen, nullptr); return ::FMT_POSIX(fdopen(fildes, mode)); } @@ -160,7 +160,7 @@ int test::pipe(int* pfds, unsigned psize, int textmode) { #endif FILE* test::fopen(const char* filename, const char* mode) { - EMULATE_EINTR(fopen, FMT_NULL); + EMULATE_EINTR(fopen, nullptr); return ::fopen(filename, mode); } @@ -214,7 +214,7 @@ TEST(UtilTest, GetPageSize) { TEST(FileTest, OpenRetry) { write_file("test", "there must be something here"); - std::unique_ptr f{FMT_NULL}; + std::unique_ptr f{nullptr}; EXPECT_RETRY(f.reset(new file("test", file::RDONLY)), open, "cannot open file test"); #ifndef _WIN32 @@ -231,7 +231,7 @@ TEST(FileTest, CloseNoRetryInDtor) { EXPECT_WRITE(stderr, { close_count = 1; - f.reset(FMT_NULL); + f.reset(nullptr); saved_close_count = close_count; close_count = 0; }, @@ -384,7 +384,7 @@ TEST(FileTest, FdopenNoRetry) { TEST(BufferedFileTest, OpenRetry) { write_file("test", "there must be something here"); - std::unique_ptr f{FMT_NULL}; + std::unique_ptr f{nullptr}; EXPECT_RETRY(f.reset(new buffered_file("test", "r")), fopen, "cannot open file test"); #ifndef _WIN32 @@ -402,7 +402,7 @@ TEST(BufferedFileTest, CloseNoRetryInDtor) { EXPECT_WRITE(stderr, { fclose_count = 1; - f.reset(FMT_NULL); + f.reset(nullptr); saved_fclose_count = fclose_count; fclose_count = 0; }, @@ -441,7 +441,7 @@ TEST(ScopedMock, Scope) { TestMock& copy = mock; static_cast(copy); } - EXPECT_EQ(FMT_NULL, TestMock::instance); + EXPECT_EQ(nullptr, TestMock::instance); } #ifdef FMT_LOCALE @@ -526,7 +526,7 @@ TEST(LocaleTest, Locale) { # endif ScopedMock mock; LocaleType impl = reinterpret_cast(42); - EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), FMT_NULL)) + EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), nullptr)) .WillOnce(Return(impl)); EXPECT_CALL(mock, freelocale(impl)); fmt::Locale locale; diff --git a/test/posix-test.cc b/test/posix-test.cc index 6026d59d2fb4..133a922ed292 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -58,26 +58,26 @@ static void write(file& f, fmt::string_view s) { TEST(BufferedFileTest, DefaultCtor) { buffered_file f; - EXPECT_TRUE(f.get() == FMT_NULL); + EXPECT_TRUE(f.get() == nullptr); } TEST(BufferedFileTest, MoveCtor) { buffered_file bf = open_buffered_file(); FILE* fp = bf.get(); - EXPECT_TRUE(fp != FMT_NULL); + EXPECT_TRUE(fp != nullptr); buffered_file bf2(std::move(bf)); EXPECT_EQ(fp, bf2.get()); - EXPECT_TRUE(bf.get() == FMT_NULL); + EXPECT_TRUE(bf.get() == nullptr); } TEST(BufferedFileTest, MoveAssignment) { buffered_file bf = open_buffered_file(); FILE* fp = bf.get(); - EXPECT_TRUE(fp != FMT_NULL); + EXPECT_TRUE(fp != nullptr); buffered_file bf2; bf2 = std::move(bf); EXPECT_EQ(fp, bf2.get()); - EXPECT_TRUE(bf.get() == FMT_NULL); + EXPECT_TRUE(bf.get() == nullptr); } TEST(BufferedFileTest, MoveAssignmentClosesFile) { @@ -89,13 +89,13 @@ TEST(BufferedFileTest, MoveAssignmentClosesFile) { } TEST(BufferedFileTest, MoveFromTemporaryInCtor) { - FILE* fp = FMT_NULL; + FILE* fp = nullptr; buffered_file f(open_buffered_file(&fp)); EXPECT_EQ(fp, f.get()); } TEST(BufferedFileTest, MoveFromTemporaryInAssignment) { - FILE* fp = FMT_NULL; + FILE* fp = nullptr; buffered_file f; f = open_buffered_file(&fp); EXPECT_EQ(fp, f.get()); @@ -126,7 +126,7 @@ TEST(BufferedFileTest, CloseErrorInDtor) { // redirecting the output in EXPECT_STDERR and the second close // will break output redirection. FMT_POSIX(close(f->fileno())); - SUPPRESS_ASSERT(f.reset(FMT_NULL)); + SUPPRESS_ASSERT(f.reset(nullptr)); }, format_system_error(EBADF, "cannot close file") + "\n"); } @@ -135,7 +135,7 @@ TEST(BufferedFileTest, Close) { buffered_file f = open_buffered_file(); int fd = f.fileno(); f.close(); - EXPECT_TRUE(f.get() == FMT_NULL); + EXPECT_TRUE(f.get() == nullptr); EXPECT_TRUE(isclosed(fd)); } @@ -143,7 +143,7 @@ TEST(BufferedFileTest, CloseError) { buffered_file f = open_buffered_file(); FMT_POSIX(close(f.fileno())); EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file"); - EXPECT_TRUE(f.get() == FMT_NULL); + EXPECT_TRUE(f.get() == nullptr); } TEST(BufferedFileTest, Fileno) { @@ -257,7 +257,7 @@ TEST(FileTest, CloseErrorInDtor) { // redirecting the output in EXPECT_STDERR and the second close // will break output redirection. FMT_POSIX(close(f->descriptor())); - SUPPRESS_ASSERT(f.reset(FMT_NULL)); + SUPPRESS_ASSERT(f.reset(nullptr)); }, format_system_error(EBADF, "cannot close file") + "\n"); } diff --git a/test/prepare-test.cc b/test/prepare-test.cc index e40b6b45c913..9985123dace8 100644 --- a/test/prepare-test.cc +++ b/test/prepare-test.cc @@ -620,7 +620,7 @@ template struct user_allocator { pointer allocate( size_type cnt, - typename std::allocator::const_pointer = FMT_NULL) { + typename std::allocator::const_pointer = nullptr) { return new value_type[cnt]; } diff --git a/test/printf-test.cc b/test/printf-test.cc index 52c7fc6ac052..5794abb43954 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -434,11 +434,11 @@ TEST(PrintfTest, Char) { TEST(PrintfTest, String) { EXPECT_PRINTF("abc", "%s", "abc"); - const char* null_str = FMT_NULL; + const char* null_str = nullptr; EXPECT_PRINTF("(null)", "%s", null_str); EXPECT_PRINTF(" (null)", "%10s", null_str); EXPECT_PRINTF(L"abc", L"%s", L"abc"); - const wchar_t* null_wstr = FMT_NULL; + const wchar_t* null_wstr = nullptr; EXPECT_PRINTF(L"(null)", L"%s", null_wstr); EXPECT_PRINTF(L" (null)", L"%10s", null_wstr); } @@ -447,22 +447,22 @@ TEST(PrintfTest, Pointer) { int n; void* p = &n; EXPECT_PRINTF(fmt::format("{}", p), "%p", p); - p = FMT_NULL; + p = nullptr; EXPECT_PRINTF("(nil)", "%p", p); EXPECT_PRINTF(" (nil)", "%10p", p); const char* s = "test"; EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s); - const char* null_str = FMT_NULL; + const char* null_str = nullptr; EXPECT_PRINTF("(nil)", "%p", null_str); p = &n; EXPECT_PRINTF(fmt::format(L"{}", p), L"%p", p); - p = FMT_NULL; + p = nullptr; EXPECT_PRINTF(L"(nil)", L"%p", p); EXPECT_PRINTF(L" (nil)", L"%10p", p); const wchar_t* w = L"test"; EXPECT_PRINTF(fmt::format(L"{:p}", w), L"%p", w); - const wchar_t* null_wstr = FMT_NULL; + const wchar_t* null_wstr = nullptr; EXPECT_PRINTF(L"(nil)", L"%p", null_wstr); } diff --git a/test/std-format-test.cc b/test/std-format-test.cc index 7fbd4607cdcf..152176a08f5f 100644 --- a/test/std-format-test.cc +++ b/test/std-format-test.cc @@ -3,36 +3,36 @@ TEST(StdFormatTest, Escaping) { using namespace std; - string s = format("{0}-{{", 8); // s == "8-{" + string s = format("{0}-{{", 8); // s == "8-{" EXPECT_EQ(s, "8-{"); } TEST(StdFormatTest, Indexing) { using namespace std; - string s0 = format("{} to {}", "a", "b"); // OK: automatic indexing - string s1 = format("{1} to {0}", "a", "b"); // OK: manual indexing + string s0 = format("{} to {}", "a", "b"); // OK: automatic indexing + string s1 = format("{1} to {0}", "a", "b"); // OK: manual indexing EXPECT_EQ(s0, "a to b"); EXPECT_EQ(s1, "b to a"); - // Error: mixing automatic and manual indexing - EXPECT_THROW(string s2 = format("{0} to {}", "a", "b"), std::format_error); - // Error: mixing automatic and manual indexing - EXPECT_THROW(string s3 = format("{} to {1}", "a", "b"), std::format_error); + // Error: mixing automatic and manual indexing + EXPECT_THROW(string s2 = format("{0} to {}", "a", "b"), std::format_error); + // Error: mixing automatic and manual indexing + EXPECT_THROW(string s3 = format("{} to {1}", "a", "b"), std::format_error); } TEST(StdFormatTest, Alignment) { using namespace std; char c = 120; - string s0 = format("{:6}", 42); // s0 == " 42" - string s1 = format("{:6}", 'x'); // s1 == "x " - string s2 = format("{:*<6}", 'x'); // s2 == "x*****" - string s3 = format("{:*>6}", 'x'); // s3 == "*****x" - string s4 = format("{:*^6}", 'x'); // s4 == "**x***" + string s0 = format("{:6}", 42); // s0 == " 42" + string s1 = format("{:6}", 'x'); // s1 == "x " + string s2 = format("{:*<6}", 'x'); // s2 == "x*****" + string s3 = format("{:*>6}", 'x'); // s3 == "*****x" + string s4 = format("{:*^6}", 'x'); // s4 == "**x***" // Error: '=' with charT and no integer presentation type EXPECT_THROW(string s5 = format("{:=6}", 'x'), std::format_error); - string s6 = format("{:6d}", c); // s6 == " 120" - string s7 = format("{:=+06d}", c); // s7 == "+00120" - string s8 = format("{:0=#6x}", 0xa); // s8 == "0x000a" - string s9 = format("{:6}", true); // s9 == "true " + string s6 = format("{:6d}", c); // s6 == " 120" + string s7 = format("{:=+06d}", c); // s7 == "+00120" + string s8 = format("{:0=#6x}", 0xa); // s8 == "0x000a" + string s9 = format("{:6}", true); // s9 == "true " EXPECT_EQ(s0, " 42"); EXPECT_EQ(s1, "x "); EXPECT_EQ(s2, "x*****"); @@ -50,8 +50,10 @@ TEST(StdFormatTest, Float) { double nan = numeric_limits::quiet_NaN(); string s0 = format("{0:} {0:+} {0:-} {0: }", 1); // s0 == "1 +1 1 1" string s1 = format("{0:} {0:+} {0:-} {0: }", -1); // s1 == "-1 -1 -1 -1" - string s2 = format("{0:} {0:+} {0:-} {0: }", inf); // s2 == "inf +inf inf inf" - string s3 = format("{0:} {0:+} {0:-} {0: }", nan); // s3 == "nan +nan nan nan" + string s2 = + format("{0:} {0:+} {0:-} {0: }", inf); // s2 == "inf +inf inf inf" + string s3 = + format("{0:} {0:+} {0:-} {0: }", nan); // s3 == "nan +nan nan nan" EXPECT_EQ(s0, "1 +1 1 1"); EXPECT_EQ(s1, "-1 -1 -1 -1"); EXPECT_EQ(s2, "inf +inf inf inf"); @@ -60,10 +62,10 @@ TEST(StdFormatTest, Float) { TEST(StdFormatTest, Int) { using namespace std; - string s0 = format("{}", 42); // s0 == "42" - string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a" - string s2 = format("{0:#x} {0:#X}", 42); // s2 == "0x2a 0X2A" - string s3 = format("{:n}", 1234); // s3 == "1,234" (depends on the locale) + string s0 = format("{}", 42); // s0 == "42" + string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a" + string s2 = format("{0:#x} {0:#X}", 42); // s2 == "0x2a 0X2A" + string s3 = format("{:n}", 1234); // s3 == "1,234" (depends on the locale) EXPECT_EQ(s0, "42"); EXPECT_EQ(s1, "101010 42 52 2a"); EXPECT_EQ(s2, "0x2a 0X2A"); @@ -74,9 +76,9 @@ TEST(StdFormatTest, Int) { enum color { red, green, blue }; -const char* color_names[] = { "red", "green", "blue" }; +const char* color_names[] = {"red", "green", "blue"}; -template<> struct std::formatter : std::formatter { +template <> struct std::formatter : std::formatter { auto format(color c, format_context& ctx) { return formatter::format(color_names[c], ctx); } @@ -85,10 +87,12 @@ template<> struct std::formatter : std::formatter { struct err {}; TEST(StdFormatTest, Formatter) { - std::string s0 = std::format("{}", 42); // OK: library-provided formatter - //std::string s1 = std::format("{}", L"foo"); // Ill-formed: disabled formatter - std::string s2 = std::format("{}", red); // OK: user-provided formatter - //std::string s3 = std::format("{}", err{}); // Ill-formed: disabled formatter + std::string s0 = std::format("{}", 42); // OK: library-provided formatter + // std::string s1 = std::format("{}", L"foo"); // Ill-formed: disabled + // formatter + std::string s2 = std::format("{}", red); // OK: user-provided formatter + // std::string s3 = std::format("{}", err{}); // Ill-formed: disabled + // formatter EXPECT_EQ(s0, "42"); EXPECT_EQ(s2, "red"); } @@ -97,15 +101,14 @@ struct S { int value; }; -template<> struct std::formatter { +template <> struct std::formatter { size_t width_arg_id = 0; // Parses a width argument id in the format { }. constexpr auto parse(format_parse_context& ctx) { auto iter = ctx.begin(); auto get_char = [&]() { return iter != ctx.end() ? *iter : 0; }; - if (get_char() != '{') - return iter; + if (get_char() != '{') return iter; ++iter; char c = get_char(); if (!isdigit(c) || (++iter, get_char()) != '}') @@ -117,20 +120,22 @@ template<> struct std::formatter { // Formats S with width given by the argument width_arg_id. auto format(S s, format_context& ctx) { - int width = visit_format_arg([](auto value) -> int { - if constexpr (!is_integral_v) - throw format_error("width is not integral"); - else if (value < 0 || value > numeric_limits::max()) - throw format_error("invalid width"); - else - return value; - }, ctx.arg(width_arg_id)); + int width = visit_format_arg( + [](auto value) -> int { + if constexpr (!is_integral_v) + throw format_error("width is not integral"); + else if (value < 0 || value > numeric_limits::max()) + throw format_error("invalid width"); + else + return value; + }, + ctx.arg(width_arg_id)); return format_to(ctx.out(), "{0:{1}}", s.value, width); } }; TEST(StdFormatTest, Parsing) { - std::string s = std::format("{0:{1}}", S{42}, 10); // s == " 42" + std::string s = std::format("{0:{1}}", S{42}, 10); // s == " 42" EXPECT_EQ(s, " 42"); } diff --git a/test/util.h b/test/util.h index a0eb25ffc32b..7aa9da23f5a7 100644 --- a/test/util.h +++ b/test/util.h @@ -35,7 +35,7 @@ std::string get_system_error(int error_code); extern const char* const FILE_CONTENT; // Opens a buffered file for reading. -fmt::buffered_file open_buffered_file(FILE** fp = FMT_NULL); +fmt::buffered_file open_buffered_file(FILE** fp = nullptr); inline FILE* safe_fopen(const char* filename, const char* mode) { #if defined(_WIN32) && !defined(__MINGW32__)