Skip to content

Commit

Permalink
update fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 19, 2024
1 parent 832205f commit 0f82827
Show file tree
Hide file tree
Showing 12 changed files with 514 additions and 209 deletions.
8 changes: 5 additions & 3 deletions 3rd/fmt/fmt/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#ifndef FMT_ARGS_H_
#define FMT_ARGS_H_

#include <functional> // std::reference_wrapper
#include <memory> // std::unique_ptr
#include <vector>
#ifndef FMT_IMPORT_STD
# include <functional> // std::reference_wrapper
# include <memory> // std::unique_ptr
# include <vector>
#endif

#include "format.h" // std_string_view

Expand Down
101 changes: 65 additions & 36 deletions 3rd/fmt/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
#include <stdio.h> // FILE
#include <string.h> // strlen

#ifndef FMT_IMPORT_STD
// <cstddef> is also included transitively from <type_traits>.
#include <cstddef> // std::byte
#include <type_traits> // std::enable_if
# include <cstddef> // std::byte
# include <type_traits> // std::enable_if
#else
import std;
#endif

// The fmt library version in the form major * 10000 + minor * 100 + patch.
#define FMT_VERSION 100202
Expand Down Expand Up @@ -137,7 +141,7 @@
#elif defined(__NVCOMPILER)
# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0
#elif FMT_GCC_VERSION >= 903 && FMT_CPLUSPLUS >= 201709L
# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0
# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
#elif defined(__cpp_nontype_template_args) && \
__cpp_nontype_template_args >= 201911L
# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
Expand Down Expand Up @@ -175,8 +179,7 @@
#endif

// Disable [[noreturn]] on MSVC/NVCC because of bogus unreachable code warnings.
#if FMT_HAS_CPP_ATTRIBUTE(noreturn) && FMT_EXCEPTIONS && !FMT_MSC_VERSION && \
!defined(__NVCC__)
#if FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VERSION && !defined(__NVCC__)
# define FMT_NORETURN [[noreturn]]
#else
# define FMT_NORETURN
Expand Down Expand Up @@ -1162,6 +1165,20 @@ using appender = basic_appender<char>;

namespace detail {

template <typename T, typename Enable = void>
struct locking : std::true_type {};
template <typename T>
struct locking<T, void_t<typename formatter<remove_cvref_t<T>>::nonlocking>>
: std::false_type {};

template <typename T = int> FMT_CONSTEXPR inline auto is_locking() -> bool {
return locking<T>::value;
}
template <typename T1, typename T2, typename... Tail>
FMT_CONSTEXPR inline auto is_locking() -> bool {
return locking<T1>::value || is_locking<T2, Tail...>();
}

// An optimized version of std::copy with the output value type (T).
template <typename T, typename InputIt>
auto copy(InputIt begin, InputIt end, appender out) -> appender {
Expand Down Expand Up @@ -1491,12 +1508,14 @@ template <typename Context> struct arg_mapper {
return {};
}

// is_fundamental is used to allow formatters for extended FP types.
template <typename T, typename U = remove_const_t<T>,
FMT_ENABLE_IF((std::is_class<U>::value || std::is_enum<U>::value ||
std::is_union<U>::value) &&
!has_to_string_view<U>::value && !is_char<U>::value &&
!is_named_arg<U>::value &&
!std::is_arithmetic<format_as_t<U>>::value)>
FMT_ENABLE_IF(
(std::is_class<U>::value || std::is_enum<U>::value ||
std::is_union<U>::value || std::is_fundamental<U>::value) &&
!has_to_string_view<U>::value && !is_char<U>::value &&
!is_named_arg<U>::value && !std::is_integral<U>::value &&
!std::is_arithmetic<format_as_t<U>>::value)>
FMT_MAP_API auto map(T& val) -> decltype(FMT_DECLTYPE_THIS do_map(val)) {
return do_map(val);
}
Expand Down Expand Up @@ -2738,7 +2757,9 @@ template <typename Char, typename... Args> class format_string_checker {
return id >= 0 && id < num_args ? parse_funcs_[id](context_) : begin;
}

FMT_CONSTEXPR void on_error(const char* message) { report_error(message); }
FMT_NORETURN FMT_CONSTEXPR void on_error(const char* message) {
report_error(message);
}
};

// A base class for compile-time strings.
Expand Down Expand Up @@ -2783,33 +2804,25 @@ FMT_API void vprint_mojibake(FILE*, string_view, format_args, bool = false);
#ifndef _WIN32
inline void vprint_mojibake(FILE*, string_view, format_args, bool) {}
#endif
} // namespace detail

FMT_BEGIN_EXPORT

// A formatter specialization for natively supported types.
template <typename T, typename Char>
struct formatter<T, Char,
enable_if_t<detail::type_constant<T, Char>::value !=
detail::type::custom_type>> {
template <typename T, typename Char, type TYPE> struct native_formatter {
private:
detail::dynamic_format_specs<Char> specs_;
dynamic_format_specs<Char> specs_;

public:
using nonlocking = void;

template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* {
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin();
auto type = detail::type_constant<T, Char>::value;
auto end =
detail::parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, type);
if (type == detail::type::char_type) detail::check_char_specs(specs_);
auto end = parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, TYPE);
if (const_check(TYPE == type::char_type)) check_char_specs(specs_);
return end;
}

template <detail::type U = detail::type_constant<T, Char>::value,
FMT_ENABLE_IF(U == detail::type::string_type ||
U == detail::type::cstring_type ||
U == detail::type::char_type)>
template <type U = TYPE,
FMT_ENABLE_IF(U == type::string_type || U == type::cstring_type ||
U == type::char_type)>
FMT_CONSTEXPR void set_debug_format(bool set = true) {
specs_.type = set ? presentation_type::debug : presentation_type::none;
}
Expand All @@ -2818,6 +2831,17 @@ struct formatter<T, Char,
FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const
-> decltype(ctx.out());
};
} // namespace detail

FMT_BEGIN_EXPORT

// A formatter specialization for natively supported types.
template <typename T, typename Char>
struct formatter<T, Char,
enable_if_t<detail::type_constant<T, Char>::value !=
detail::type::custom_type>>
: detail::native_formatter<T, Char, detail::type_constant<T, Char>::value> {
};

template <typename Char = char> struct runtime_format_string {
basic_string_view<Char> str;
Expand Down Expand Up @@ -2945,8 +2969,8 @@ template <typename OutputIt, typename Sentinel = OutputIt>
struct format_to_result {
/** Iterator pointing to just after the last successful write in the range. */
OutputIt out;
/** Sentinel indicating the end of the output range. */
Sentinel out_last;
/** Specifies if the output was truncated. */
bool truncated;

FMT_CONSTEXPR operator OutputIt&() & noexcept { return out; }
FMT_CONSTEXPR operator const OutputIt&() const& noexcept { return out; }
Expand All @@ -2958,13 +2982,15 @@ struct format_to_result {
template <size_t N>
auto vformat_to(char (&out)[N], string_view fmt, format_args args)
-> format_to_result<char*> {
return {vformat_to_n(out, N, fmt, args).out, out + N};
auto result = vformat_to_n(out, N, fmt, args);
return {result.out, result.size > N};
}

template <size_t N, typename... T>
FMT_INLINE auto format_to(char (&out)[N], format_string<T...> fmt, T&&... args)
-> format_to_result<char*> {
return vformat_to(out, fmt, fmt::make_format_args(args...));
auto result = fmt::format_to_n(out, N, fmt, static_cast<T&&>(args)...);
return {result.out, result.size > N};
}

/** Returns the number of chars in the output of ``format(fmt, args...)``. */
Expand All @@ -2978,6 +3004,7 @@ FMT_NODISCARD FMT_INLINE auto formatted_size(format_string<T...> fmt,

FMT_API void vprint(string_view fmt, format_args args);
FMT_API void vprint(FILE* f, string_view fmt, format_args args);
FMT_API void vprint_locked(FILE* f, string_view fmt, format_args args);
FMT_API void vprintln(FILE* f, string_view fmt, format_args args);

/**
Expand All @@ -2993,8 +3020,9 @@ FMT_API void vprintln(FILE* f, string_view fmt, format_args args);
template <typename... T>
FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
const auto& vargs = fmt::make_format_args(args...);
return detail::is_utf8() ? vprint(fmt, vargs)
: detail::vprint_mojibake(stdout, fmt, vargs);
if (!detail::is_utf8()) return detail::vprint_mojibake(stdout, fmt, vargs);
return detail::is_locking<T...>() ? vprint(fmt, vargs)
: vprint_locked(stdout, fmt, vargs);
}

/**
Expand All @@ -3010,8 +3038,9 @@ FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
template <typename... T>
FMT_INLINE void print(FILE* f, format_string<T...> fmt, T&&... args) {
const auto& vargs = fmt::make_format_args(args...);
return detail::is_utf8() ? vprint(f, fmt, vargs)
: detail::vprint_mojibake(f, fmt, vargs);
if (!detail::is_utf8()) return detail::vprint_mojibake(f, fmt, vargs);
return detail::is_locking<T...>() ? vprint(f, fmt, vargs)
: vprint_locked(f, fmt, vargs);
}

/**
Expand Down
Loading

0 comments on commit 0f82827

Please sign in to comment.