Skip to content

Commit 0782074

Browse files
committed
更新fmt
1 parent 68abdae commit 0782074

File tree

10 files changed

+300
-347
lines changed

10 files changed

+300
-347
lines changed

3rd/fmt/fmt/base.h

+71-37
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
// Use the provided definition.
147147
#elif defined(__GNUC__) && !defined(__EXCEPTIONS)
148148
# define FMT_USE_EXCEPTIONS 0
149+
#elif defined(__clang__) && !defined(__cpp_exceptions)
150+
# define FMT_USE_EXCEPTIONS 0
149151
#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS
150152
# define FMT_USE_EXCEPTIONS 0
151153
#else
@@ -332,6 +334,13 @@ struct monostate {
332334
# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0
333335
#endif
334336

337+
template <typename T> constexpr auto min_of(T a, T b) -> T {
338+
return a < b ? a : b;
339+
}
340+
template <typename T> constexpr auto max_of(T a, T b) -> T {
341+
return a > b ? a : b;
342+
}
343+
335344
namespace detail {
336345
// Suppresses "unused variable" warnings with the method described in
337346
// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/.
@@ -394,7 +403,7 @@ inline auto map(uint128_opt) -> monostate { return {}; }
394403
#endif
395404

396405
#ifndef FMT_USE_BITINT
397-
# define FMT_USE_BITINT (FMT_CLANG_VERSION >= 1400)
406+
# define FMT_USE_BITINT (FMT_CLANG_VERSION >= 1500)
398407
#endif
399408

400409
#if FMT_USE_BITINT
@@ -562,8 +571,8 @@ template <typename Char> class basic_string_view {
562571

563572
// Lexicographically compare this string reference to other.
564573
FMT_CONSTEXPR auto compare(basic_string_view other) const -> int {
565-
size_t str_size = size_ < other.size_ ? size_ : other.size_;
566-
int result = detail::compare(data_, other.data_, str_size);
574+
int result =
575+
detail::compare(data_, other.data_, min_of(size_, other.size_));
567576
if (result != 0) return result;
568577
return size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
569578
}
@@ -714,7 +723,7 @@ class basic_specs {
714723
max_fill_size = 4
715724
};
716725

717-
unsigned long data_ = 1 << fill_size_shift;
726+
size_t data_ = 1 << fill_size_shift;
718727

719728
// Character (code unit) type is erased to prevent template bloat.
720729
char fill_data_[max_fill_size] = {' '};
@@ -793,7 +802,8 @@ class basic_specs {
793802
template <typename Char> constexpr auto fill_unit() const -> Char {
794803
using uchar = unsigned char;
795804
return static_cast<Char>(static_cast<uchar>(fill_data_[0]) |
796-
(static_cast<uchar>(fill_data_[1]) << 8));
805+
(static_cast<uchar>(fill_data_[1]) << 8) |
806+
(static_cast<uchar>(fill_data_[2]) << 16));
797807
}
798808

799809
FMT_CONSTEXPR void set_fill(char c) {
@@ -809,6 +819,7 @@ class basic_specs {
809819
unsigned uchar = static_cast<detail::unsigned_char<Char>>(s[0]);
810820
fill_data_[0] = static_cast<char>(uchar);
811821
fill_data_[1] = static_cast<char>(uchar >> 8);
822+
fill_data_[2] = static_cast<char>(uchar >> 16);
812823
return;
813824
}
814825
FMT_ASSERT(size <= max_fill_size, "invalid fill");
@@ -1627,12 +1638,12 @@ template <typename... T> struct arg_pack {};
16271638
template <typename Char, int NUM_ARGS, int NUM_NAMED_ARGS, bool DYNAMIC_NAMES>
16281639
class format_string_checker {
16291640
private:
1630-
type types_[NUM_ARGS > 0 ? NUM_ARGS : 1];
1631-
named_arg_info<Char> named_args_[NUM_NAMED_ARGS > 0 ? NUM_NAMED_ARGS : 1];
1641+
type types_[max_of(1, NUM_ARGS)];
1642+
named_arg_info<Char> named_args_[max_of(1, NUM_NAMED_ARGS)];
16321643
compile_parse_context<Char> context_;
16331644

16341645
using parse_func = auto (*)(parse_context<Char>&) -> const Char*;
1635-
parse_func parse_funcs_[NUM_ARGS > 0 ? NUM_ARGS : 1];
1646+
parse_func parse_funcs_[max_of(1, NUM_ARGS)];
16361647

16371648
public:
16381649
template <typename... T>
@@ -1694,7 +1705,7 @@ template <typename T> class buffer {
16941705
protected:
16951706
// Don't initialize ptr_ since it is not accessed to save a few cycles.
16961707
FMT_MSC_WARNING(suppress : 26495)
1697-
FMT_CONSTEXPR20 buffer(grow_fun grow, size_t sz) noexcept
1708+
FMT_CONSTEXPR buffer(grow_fun grow, size_t sz) noexcept
16981709
: size_(sz), capacity_(sz), grow_(grow) {}
16991710

17001711
constexpr buffer(grow_fun grow, T* p = nullptr, size_t sz = 0,
@@ -1740,7 +1751,7 @@ template <typename T> class buffer {
17401751
// the new elements may not be initialized.
17411752
FMT_CONSTEXPR void try_resize(size_t count) {
17421753
try_reserve(count);
1743-
size_ = count <= capacity_ ? count : capacity_;
1754+
size_ = min_of(count, capacity_);
17441755
}
17451756

17461757
// Tries increasing the buffer capacity to `new_capacity`. It can increase the
@@ -1788,9 +1799,9 @@ template <typename T> class buffer {
17881799
};
17891800

17901801
struct buffer_traits {
1791-
explicit buffer_traits(size_t) {}
1792-
auto count() const -> size_t { return 0; }
1793-
auto limit(size_t size) -> size_t { return size; }
1802+
constexpr explicit buffer_traits(size_t) {}
1803+
constexpr auto count() const -> size_t { return 0; }
1804+
constexpr auto limit(size_t size) const -> size_t { return size; }
17941805
};
17951806

17961807
class fixed_buffer_traits {
@@ -1799,12 +1810,12 @@ class fixed_buffer_traits {
17991810
size_t limit_;
18001811

18011812
public:
1802-
explicit fixed_buffer_traits(size_t limit) : limit_(limit) {}
1803-
auto count() const -> size_t { return count_; }
1804-
auto limit(size_t size) -> size_t {
1813+
constexpr explicit fixed_buffer_traits(size_t limit) : limit_(limit) {}
1814+
constexpr auto count() const -> size_t { return count_; }
1815+
FMT_CONSTEXPR auto limit(size_t size) -> size_t {
18051816
size_t n = limit_ > count_ ? limit_ - count_ : 0;
18061817
count_ += size;
1807-
return size < n ? size : n;
1818+
return min_of(size, n);
18081819
}
18091820
};
18101821

@@ -1962,15 +1973,37 @@ template <typename T = char> class counting_buffer : public buffer<T> {
19621973
template <typename T>
19631974
struct is_back_insert_iterator<basic_appender<T>> : std::true_type {};
19641975

1976+
template <typename OutputIt, typename InputIt, typename = void>
1977+
struct has_back_insert_iterator_container_append : std::false_type {};
1978+
template <typename OutputIt, typename InputIt>
1979+
struct has_back_insert_iterator_container_append<
1980+
OutputIt, InputIt,
1981+
void_t<decltype(get_container(std::declval<OutputIt>())
1982+
.append(std::declval<InputIt>(),
1983+
std::declval<InputIt>()))>> : std::true_type {};
1984+
19651985
// An optimized version of std::copy with the output value type (T).
19661986
template <typename T, typename InputIt, typename OutputIt,
1967-
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value)>
1987+
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value&&
1988+
has_back_insert_iterator_container_append<
1989+
OutputIt, InputIt>::value)>
19681990
FMT_CONSTEXPR20 auto copy(InputIt begin, InputIt end, OutputIt out)
19691991
-> OutputIt {
19701992
get_container(out).append(begin, end);
19711993
return out;
19721994
}
19731995

1996+
template <typename T, typename InputIt, typename OutputIt,
1997+
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value &&
1998+
!has_back_insert_iterator_container_append<
1999+
OutputIt, InputIt>::value)>
2000+
FMT_CONSTEXPR20 auto copy(InputIt begin, InputIt end, OutputIt out)
2001+
-> OutputIt {
2002+
auto& c = get_container(out);
2003+
c.insert(c.end(), begin, end);
2004+
return out;
2005+
}
2006+
19742007
template <typename T, typename InputIt, typename OutputIt,
19752008
FMT_ENABLE_IF(!is_back_insert_iterator<OutputIt>::value)>
19762009
FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt {
@@ -2146,7 +2179,8 @@ template <typename Context> class value {
21462179
template <typename T, FMT_ENABLE_IF(is_named_arg<T>::value)>
21472180
value(const T& named_arg) : value(named_arg.value) {}
21482181

2149-
template <typename T, FMT_ENABLE_IF(use_formatter<T>::value)>
2182+
template <typename T,
2183+
FMT_ENABLE_IF(use_formatter<T>::value || !FMT_BUILTIN_TYPES)>
21502184
FMT_CONSTEXPR20 FMT_INLINE value(T& x) : value(x, custom_tag()) {}
21512185

21522186
FMT_ALWAYS_INLINE value(const named_arg_info<char_type>* args, size_t size)
@@ -2220,9 +2254,12 @@ struct locale_ref {
22202254

22212255
public:
22222256
constexpr locale_ref() : locale_(nullptr) {}
2223-
template <typename Locale> explicit locale_ref(const Locale& loc);
2224-
explicit operator bool() const noexcept { return locale_ != nullptr; }
2225-
#endif
2257+
2258+
template <typename Locale, FMT_ENABLE_IF(sizeof(Locale::collate) != 0)>
2259+
locale_ref(const Locale& loc);
2260+
2261+
inline explicit operator bool() const noexcept { return locale_ != nullptr; }
2262+
#endif // FMT_USE_LOCALE
22262263

22272264
template <typename Locale> auto get() const -> Locale;
22282265
};
@@ -2251,8 +2288,7 @@ template <typename Context, size_t NUM_ARGS, size_t NUM_NAMED_ARGS,
22512288
unsigned long long DESC>
22522289
struct named_arg_store {
22532290
// args_[0].named_args points to named_args to avoid bloating format_args.
2254-
// +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning.
2255-
arg_t<Context, NUM_ARGS> args[1 + (NUM_ARGS != 0 ? NUM_ARGS : +1)];
2291+
arg_t<Context, NUM_ARGS> args[1 + NUM_ARGS];
22562292
named_arg_info<typename Context::char_type> named_args[NUM_NAMED_ARGS];
22572293

22582294
template <typename... T>
@@ -2286,7 +2322,7 @@ struct format_arg_store {
22862322
// +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning.
22872323
using type =
22882324
conditional_t<NUM_NAMED_ARGS == 0,
2289-
arg_t<Context, NUM_ARGS>[NUM_ARGS != 0 ? NUM_ARGS : +1],
2325+
arg_t<Context, NUM_ARGS>[max_of<size_t>(1, NUM_ARGS)],
22902326
named_arg_store<Context, NUM_ARGS, NUM_NAMED_ARGS, DESC>>;
22912327
type args;
22922328
};
@@ -2372,11 +2408,6 @@ template <typename T> class basic_appender {
23722408
detail::buffer<T>* container;
23732409

23742410
public:
2375-
using iterator_category = int;
2376-
using value_type = T;
2377-
using pointer = T*;
2378-
using reference = T&;
2379-
using difference_type = decltype(pointer() - pointer());
23802411
using container_type = detail::buffer<T>;
23812412

23822413
FMT_CONSTEXPR basic_appender(detail::buffer<T>& buf) : container(&buf) {}
@@ -2596,7 +2627,7 @@ class context : private detail::locale_ref {
25962627
void operator=(const context&) = delete;
25972628

25982629
FMT_CONSTEXPR auto arg(int id) const -> format_arg { return args_.get(id); }
2599-
auto arg(string_view name) -> format_arg { return args_.get(name); }
2630+
inline auto arg(string_view name) -> format_arg { return args_.get(name); }
26002631
FMT_CONSTEXPR auto arg_id(string_view name) -> int {
26012632
return args_.get_id(name);
26022633
}
@@ -2605,7 +2636,7 @@ class context : private detail::locale_ref {
26052636
FMT_CONSTEXPR auto out() -> iterator { return out_; }
26062637

26072638
// Advances the begin iterator to `it`.
2608-
void advance_to(iterator) {}
2639+
FMT_CONSTEXPR void advance_to(iterator) {}
26092640

26102641
FMT_CONSTEXPR auto locale() -> detail::locale_ref { return *this; }
26112642
};
@@ -2627,12 +2658,14 @@ inline auto runtime(string_view s) -> runtime_format_string<> { return {{s}}; }
26272658
/// A compile-time format string.
26282659
template <typename... T> struct fstring {
26292660
private:
2630-
static constexpr int num_static_named_args =
2661+
static constexpr size_t num_static_named_args =
26312662
detail::count_static_named_args<T...>();
26322663

2633-
using checker = detail::format_string_checker<
2634-
char, static_cast<int>(sizeof...(T)), num_static_named_args,
2635-
num_static_named_args != detail::count_named_args<T...>()>;
2664+
using checker =
2665+
detail::format_string_checker<char, static_cast<int>(sizeof...(T)),
2666+
static_cast<int>(num_static_named_args),
2667+
num_static_named_args !=
2668+
detail::count_named_args<T...>()>;
26362669

26372670
using arg_pack = detail::arg_pack<T...>;
26382671

@@ -2657,8 +2690,9 @@ template <typename... T> struct fstring {
26572690
template <typename S,
26582691
FMT_ENABLE_IF(std::is_convertible<const S&, string_view>::value)>
26592692
FMT_CONSTEVAL FMT_ALWAYS_INLINE fstring(const S& s) : str(s) {
2693+
auto sv = string_view(str);
26602694
if (FMT_USE_CONSTEVAL)
2661-
detail::parse_format_string<char>(s, checker(s, arg_pack()));
2695+
detail::parse_format_string<char>(sv, checker(sv, arg_pack()));
26622696
#ifdef FMT_ENFORCE_COMPILE_STRING
26632697
static_assert(
26642698
FMT_USE_CONSTEVAL && sizeof(s) != 0,

3rd/fmt/fmt/chrono.h

+21-19
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ struct is_same_arithmetic_type
444444
std::is_floating_point<Rep2>::value)> {
445445
};
446446

447-
inline void throw_duration_error() {
447+
FMT_NORETURN inline void throw_duration_error() {
448448
FMT_THROW(format_error("cannot format duration"));
449449
}
450450

@@ -540,24 +540,24 @@ inline auto localtime(std::time_t time) -> std::tm {
540540
std::time_t time_;
541541
std::tm tm_;
542542

543-
dispatcher(std::time_t t) : time_(t) {}
543+
inline dispatcher(std::time_t t) : time_(t) {}
544544

545-
auto run() -> bool {
545+
inline auto run() -> bool {
546546
using namespace fmt::detail;
547547
return handle(localtime_r(&time_, &tm_));
548548
}
549549

550-
auto handle(std::tm* tm) -> bool { return tm != nullptr; }
550+
inline auto handle(std::tm* tm) -> bool { return tm != nullptr; }
551551

552-
auto handle(detail::null<>) -> bool {
552+
inline auto handle(detail::null<>) -> bool {
553553
using namespace fmt::detail;
554554
return fallback(localtime_s(&tm_, &time_));
555555
}
556556

557-
auto fallback(int res) -> bool { return res == 0; }
557+
inline auto fallback(int res) -> bool { return res == 0; }
558558

559559
#if !FMT_MSC_VERSION
560-
auto fallback(detail::null<>) -> bool {
560+
inline auto fallback(detail::null<>) -> bool {
561561
using namespace fmt::detail;
562562
std::tm* tm = std::localtime(&time_);
563563
if (tm) tm_ = *tm;
@@ -591,24 +591,24 @@ inline auto gmtime(std::time_t time) -> std::tm {
591591
std::time_t time_;
592592
std::tm tm_;
593593

594-
dispatcher(std::time_t t) : time_(t) {}
594+
inline dispatcher(std::time_t t) : time_(t) {}
595595

596-
auto run() -> bool {
596+
inline auto run() -> bool {
597597
using namespace fmt::detail;
598598
return handle(gmtime_r(&time_, &tm_));
599599
}
600600

601-
auto handle(std::tm* tm) -> bool { return tm != nullptr; }
601+
inline auto handle(std::tm* tm) -> bool { return tm != nullptr; }
602602

603-
auto handle(detail::null<>) -> bool {
603+
inline auto handle(detail::null<>) -> bool {
604604
using namespace fmt::detail;
605605
return fallback(gmtime_s(&tm_, &time_));
606606
}
607607

608-
auto fallback(int res) -> bool { return res == 0; }
608+
inline auto fallback(int res) -> bool { return res == 0; }
609609

610610
#if !FMT_MSC_VERSION
611-
auto fallback(detail::null<>) -> bool {
611+
inline auto fallback(detail::null<>) -> bool {
612612
std::tm* tm = std::gmtime(&time_);
613613
if (tm) tm_ = *tm;
614614
return tm != nullptr;
@@ -912,7 +912,9 @@ template <typename Derived> struct null_chrono_spec_handler {
912912
};
913913

914914
struct tm_format_checker : null_chrono_spec_handler<tm_format_checker> {
915-
FMT_NORETURN void unsupported() { FMT_THROW(format_error("no format")); }
915+
FMT_NORETURN inline void unsupported() {
916+
FMT_THROW(format_error("no format"));
917+
}
916918

917919
template <typename Char>
918920
FMT_CONSTEXPR void on_text(const Char*, const Char*) {}
@@ -1069,7 +1071,7 @@ void write_fractional_seconds(OutputIt& out, Duration d, int precision = -1) {
10691071
}
10701072
} else if (precision > 0) {
10711073
*out++ = '.';
1072-
leading_zeroes = (std::min)(leading_zeroes, precision);
1074+
leading_zeroes = min_of(leading_zeroes, precision);
10731075
int remaining = precision - leading_zeroes;
10741076
out = detail::fill_n(out, leading_zeroes, '0');
10751077
if (remaining < num_digits) {
@@ -1572,7 +1574,7 @@ class tm_writer {
15721574
struct chrono_format_checker : null_chrono_spec_handler<chrono_format_checker> {
15731575
bool has_precision_integral = false;
15741576

1575-
FMT_NORETURN void unsupported() { FMT_THROW(format_error("no date")); }
1577+
FMT_NORETURN inline void unsupported() { FMT_THROW(format_error("no date")); }
15761578

15771579
template <typename Char>
15781580
FMT_CONSTEXPR void on_text(const Char*, const Char*) {}
@@ -1693,14 +1695,14 @@ class get_locale {
16931695
bool has_locale_ = false;
16941696

16951697
public:
1696-
get_locale(bool localized, locale_ref loc) : has_locale_(localized) {
1698+
inline get_locale(bool localized, locale_ref loc) : has_locale_(localized) {
16971699
if (localized)
16981700
::new (&locale_) std::locale(loc.template get<std::locale>());
16991701
}
1700-
~get_locale() {
1702+
inline ~get_locale() {
17011703
if (has_locale_) locale_.~locale();
17021704
}
1703-
operator const std::locale&() const {
1705+
inline operator const std::locale&() const {
17041706
return has_locale_ ? locale_ : get_classic_locale();
17051707
}
17061708
};

0 commit comments

Comments
 (0)