diff options
Diffstat (limited to 'source/extern/fmt/printf.h')
-rw-r--r-- | source/extern/fmt/printf.h | 266 |
1 files changed, 131 insertions, 135 deletions
diff --git a/source/extern/fmt/printf.h b/source/extern/fmt/printf.h index 554715e..07e8157 100644 --- a/source/extern/fmt/printf.h +++ b/source/extern/fmt/printf.h @@ -16,22 +16,22 @@ FMT_BEGIN_NAMESPACE FMT_BEGIN_EXPORT -template <typename T> struct printf_formatter { printf_formatter() = delete; }; - -template <typename Char> -class basic_printf_parse_context : public basic_format_parse_context<Char> { - using basic_format_parse_context<Char>::basic_format_parse_context; +template <typename T> struct printf_formatter { + printf_formatter() = delete; }; -template <typename OutputIt, typename Char> class basic_printf_context { +template <typename Char> class basic_printf_context { private: - OutputIt out_; + detail::buffer_appender<Char> out_; basic_format_args<basic_printf_context> args_; + static_assert(std::is_same<Char, char>::value || + std::is_same<Char, wchar_t>::value, + "Unsupported code unit type."); + public: using char_type = Char; - using format_arg = basic_format_arg<basic_printf_context>; - using parse_context_type = basic_printf_parse_context<Char>; + using parse_context_type = basic_format_parse_context<Char>; template <typename T> using formatter_type = printf_formatter<T>; /** @@ -40,75 +40,77 @@ template <typename OutputIt, typename Char> class basic_printf_context { stored in the context object so make sure they have appropriate lifetimes. \endrst */ - basic_printf_context(OutputIt out, + basic_printf_context(detail::buffer_appender<Char> out, basic_format_args<basic_printf_context> args) : out_(out), args_(args) {} - OutputIt out() { return out_; } - void advance_to(OutputIt it) { out_ = it; } + auto out() -> detail::buffer_appender<Char> { return out_; } + void advance_to(detail::buffer_appender<Char>) {} - detail::locale_ref locale() { return {}; } + auto locale() -> detail::locale_ref { return {}; } - format_arg arg(int id) const { return args_.get(id); } + auto arg(int id) const -> basic_format_arg<basic_printf_context> { + return args_.get(id); + } FMT_CONSTEXPR void on_error(const char* message) { detail::error_handler().on_error(message); } }; -FMT_BEGIN_DETAIL_NAMESPACE +namespace detail { // Checks if a value fits in int - used to avoid warnings about comparing // signed and unsigned integers. template <bool IsSigned> struct int_checker { - template <typename T> static bool fits_in_int(T value) { + template <typename T> static auto fits_in_int(T value) -> bool { unsigned max = max_value<int>(); return value <= max; } - static bool fits_in_int(bool) { return true; } + static auto fits_in_int(bool) -> bool { return true; } }; template <> struct int_checker<true> { - template <typename T> static bool fits_in_int(T value) { + template <typename T> static auto fits_in_int(T value) -> bool { return value >= (std::numeric_limits<int>::min)() && value <= max_value<int>(); } - static bool fits_in_int(int) { return true; } + static auto fits_in_int(int) -> bool { return true; } }; -class printf_precision_handler { - public: +struct printf_precision_handler { template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> - int operator()(T value) { + auto operator()(T value) -> int { if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value)) throw_format_error("number is too big"); return (std::max)(static_cast<int>(value), 0); } template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> - int operator()(T) { + auto operator()(T) -> int { throw_format_error("precision is not integer"); return 0; } }; // An argument visitor that returns true iff arg is a zero integer. -class is_zero_int { - public: +struct is_zero_int { template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> - bool operator()(T value) { + auto operator()(T value) -> bool { return value == 0; } template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> - bool operator()(T) { + auto operator()(T) -> bool { return false; } }; template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {}; -template <> struct make_unsigned_or_bool<bool> { using type = bool; }; +template <> struct make_unsigned_or_bool<bool> { + using type = bool; +}; template <typename T, typename Context> class arg_converter { private: @@ -132,22 +134,23 @@ template <typename T, typename Context> class arg_converter { if (const_check(sizeof(target_type) <= sizeof(int))) { // Extra casts are used to silence warnings. if (is_signed) { - arg_ = detail::make_arg<Context>( - static_cast<int>(static_cast<target_type>(value))); + auto n = static_cast<int>(static_cast<target_type>(value)); + arg_ = detail::make_arg<Context>(n); } else { using unsigned_type = typename make_unsigned_or_bool<target_type>::type; - arg_ = detail::make_arg<Context>( - static_cast<unsigned>(static_cast<unsigned_type>(value))); + auto n = static_cast<unsigned>(static_cast<unsigned_type>(value)); + arg_ = detail::make_arg<Context>(n); } } else { if (is_signed) { // glibc's printf doesn't sign extend arguments of smaller types: // std::printf("%lld", -42); // prints "4294967254" // but we don't have to do the same because it's a UB. - arg_ = detail::make_arg<Context>(static_cast<long long>(value)); + auto n = static_cast<long long>(value); + arg_ = detail::make_arg<Context>(n); } else { - arg_ = detail::make_arg<Context>( - static_cast<typename make_unsigned_or_bool<U>::type>(value)); + auto n = static_cast<typename make_unsigned_or_bool<U>::type>(value); + arg_ = detail::make_arg<Context>(n); } } } @@ -175,8 +178,8 @@ template <typename Context> class char_converter { template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> void operator()(T value) { - arg_ = detail::make_arg<Context>( - static_cast<typename Context::char_type>(value)); + auto c = static_cast<typename Context::char_type>(value); + arg_ = detail::make_arg<Context>(c); } template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> @@ -186,8 +189,8 @@ template <typename Context> class char_converter { // An argument visitor that return a pointer to a C string if argument is a // string or null otherwise. template <typename Char> struct get_cstring { - template <typename T> const Char* operator()(T) { return nullptr; } - const Char* operator()(const Char* s) { return s; } + template <typename T> auto operator()(T) -> const Char* { return nullptr; } + auto operator()(const Char* s) -> const Char* { return s; } }; // Checks if an argument is a valid printf width specifier and sets @@ -200,7 +203,7 @@ template <typename Char> class printf_width_handler { explicit printf_width_handler(format_specs<Char>& specs) : specs_(specs) {} template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> - unsigned operator()(T value) { + auto operator()(T value) -> unsigned { auto width = static_cast<uint32_or_64_or_128_t<T>>(value); if (detail::is_negative(value)) { specs_.align = align::left; @@ -212,7 +215,7 @@ template <typename Char> class printf_width_handler { } template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> - unsigned operator()(T) { + auto operator()(T) -> unsigned { throw_format_error("width is not integer"); return 0; } @@ -227,80 +230,85 @@ auto make_arg_formatter(buffer_appender<Char> iter, format_specs<Char>& s) } // The ``printf`` argument formatter. -template <typename OutputIt, typename Char> +template <typename Char> class printf_arg_formatter : public arg_formatter<Char> { private: using base = arg_formatter<Char>; - using context_type = basic_printf_context<OutputIt, Char>; + using context_type = basic_printf_context<Char>; context_type& context_; - OutputIt write_null_pointer(bool is_string = false) { + void write_null_pointer(bool is_string = false) { auto s = this->specs; s.type = presentation_type::none; - return write_bytes(this->out, is_string ? "(null)" : "(nil)", s); + write_bytes(this->out, is_string ? "(null)" : "(nil)", s); } public: - printf_arg_formatter(OutputIt iter, format_specs<Char>& s, context_type& ctx) + printf_arg_formatter(buffer_appender<Char> iter, format_specs<Char>& s, + context_type& ctx) : base(make_arg_formatter(iter, s)), context_(ctx) {} - OutputIt operator()(monostate value) { return base::operator()(value); } + void operator()(monostate value) { base::operator()(value); } template <typename T, FMT_ENABLE_IF(detail::is_integral<T>::value)> - OutputIt operator()(T value) { + void operator()(T value) { // MSVC2013 fails to compile separate overloads for bool and Char so use // std::is_same instead. - if (std::is_same<T, Char>::value) { - format_specs<Char> fmt_specs = this->specs; - if (fmt_specs.type != presentation_type::none && - fmt_specs.type != presentation_type::chr) { - return (*this)(static_cast<int>(value)); - } - fmt_specs.sign = sign::none; - fmt_specs.alt = false; - fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types. - // align::numeric needs to be overwritten here since the '0' flag is - // ignored for non-numeric types - if (fmt_specs.align == align::none || fmt_specs.align == align::numeric) - fmt_specs.align = align::right; - return write<Char>(this->out, static_cast<Char>(value), fmt_specs); + if (!std::is_same<T, Char>::value) { + base::operator()(value); + return; + } + format_specs<Char> fmt_specs = this->specs; + if (fmt_specs.type != presentation_type::none && + fmt_specs.type != presentation_type::chr) { + return (*this)(static_cast<int>(value)); } - return base::operator()(value); + fmt_specs.sign = sign::none; + fmt_specs.alt = false; + fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types. + // align::numeric needs to be overwritten here since the '0' flag is + // ignored for non-numeric types + if (fmt_specs.align == align::none || fmt_specs.align == align::numeric) + fmt_specs.align = align::right; + write<Char>(this->out, static_cast<Char>(value), fmt_specs); } template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)> - OutputIt operator()(T value) { - return base::operator()(value); + void operator()(T value) { + base::operator()(value); } /** Formats a null-terminated C string. */ - OutputIt operator()(const char* value) { - if (value) return base::operator()(value); - return write_null_pointer(this->specs.type != presentation_type::pointer); + void operator()(const char* value) { + if (value) + base::operator()(value); + else + write_null_pointer(this->specs.type != presentation_type::pointer); } /** Formats a null-terminated wide C string. */ - OutputIt operator()(const wchar_t* value) { - if (value) return base::operator()(value); - return write_null_pointer(this->specs.type != presentation_type::pointer); + void operator()(const wchar_t* value) { + if (value) + base::operator()(value); + else + write_null_pointer(this->specs.type != presentation_type::pointer); } - OutputIt operator()(basic_string_view<Char> value) { - return base::operator()(value); - } + void operator()(basic_string_view<Char> value) { base::operator()(value); } /** Formats a pointer. */ - OutputIt operator()(const void* value) { - return value ? base::operator()(value) : write_null_pointer(); + void operator()(const void* value) { + if (value) + base::operator()(value); + else + write_null_pointer(); } /** Formats an argument of a custom (user-defined) type. */ - OutputIt operator()(typename basic_format_arg<context_type>::handle handle) { - auto parse_ctx = - basic_printf_parse_context<Char>(basic_string_view<Char>()); + void operator()(typename basic_format_arg<context_type>::handle handle) { + auto parse_ctx = basic_format_parse_context<Char>({}); handle.format(parse_ctx, context_); - return this->out; } }; @@ -318,9 +326,7 @@ void parse_flags(format_specs<Char>& specs, const Char*& it, const Char* end) { specs.fill[0] = '0'; break; case ' ': - if (specs.sign != sign::plus) { - specs.sign = sign::space; - } + if (specs.sign != sign::plus) specs.sign = sign::space; break; case '#': specs.alt = true; @@ -332,8 +338,8 @@ void parse_flags(format_specs<Char>& specs, const Char*& it, const Char* end) { } template <typename Char, typename GetArg> -int parse_header(const Char*& it, const Char* end, format_specs<Char>& specs, - GetArg get_arg) { +auto parse_header(const Char*& it, const Char* end, format_specs<Char>& specs, + GetArg get_arg) -> int { int arg_index = -1; Char c = *it; if (c >= '0' && c <= '9') { @@ -414,8 +420,8 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format, basic_format_args<Context> args) { using iterator = buffer_appender<Char>; auto out = iterator(buf); - auto context = basic_printf_context<iterator, Char>(out, args); - auto parse_ctx = basic_printf_parse_context<Char>(format); + auto context = basic_printf_context<Char>(out, args); + auto parse_ctx = basic_format_parse_context<Char>(format); // Returns the argument with specified index or, if arg_index is -1, the next // argument. @@ -437,12 +443,11 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format, } Char c = *it++; if (it != end && *it == c) { - out = write(out, basic_string_view<Char>(start, to_unsigned(it - start))); + write(out, basic_string_view<Char>(start, to_unsigned(it - start))); start = ++it; continue; } - out = - write(out, basic_string_view<Char>(start, to_unsigned(it - 1 - start))); + write(out, basic_string_view<Char>(start, to_unsigned(it - 1 - start))); auto specs = format_specs<Char>(); specs.align = align::right; @@ -469,16 +474,17 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format, auto arg = get_arg(arg_index); // For d, i, o, u, x, and X conversion specifiers, if a precision is // specified, the '0' flag is ignored - if (specs.precision >= 0 && arg.is_integral()) - specs.fill[0] = - ' '; // Ignore '0' flag for non-numeric types or if '-' present. + if (specs.precision >= 0 && arg.is_integral()) { + // Ignore '0' for non-numeric types or if '-' present. + specs.fill[0] = ' '; + } if (specs.precision >= 0 && arg.type() == type::cstring_type) { auto str = visit_format_arg(get_cstring<Char>(), arg); auto str_end = str + specs.precision; auto nul = std::find(str, str_end, Char()); - arg = make_arg<basic_printf_context<iterator, Char>>( - basic_string_view<Char>( - str, to_unsigned(nul != str_end ? nul - str : specs.precision))); + auto sv = basic_string_view<Char>( + str, to_unsigned(nul != str_end ? nul - str : specs.precision)); + arg = make_arg<basic_printf_context<Char>>(sv); } if (specs.alt && visit_format_arg(is_zero_int(), arg)) specs.alt = false; if (specs.fill[0] == '0') { @@ -540,8 +546,7 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format, type = 'd'; break; case 'c': - visit_format_arg( - char_converter<basic_printf_context<iterator, Char>>(arg), arg); + visit_format_arg(char_converter<basic_printf_context<Char>>(arg), arg); break; } } @@ -552,19 +557,14 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format, start = it; // Format argument. - out = visit_format_arg( - printf_arg_formatter<iterator, Char>(out, specs, context), arg); + visit_format_arg(printf_arg_formatter<Char>(out, specs, context), arg); } write(out, basic_string_view<Char>(start, to_unsigned(it - start))); } -FMT_END_DETAIL_NAMESPACE - -template <typename Char> -using basic_printf_context_t = - basic_printf_context<detail::buffer_appender<Char>, Char>; +} // namespace detail -using printf_context = basic_printf_context_t<char>; -using wprintf_context = basic_printf_context_t<wchar_t>; +using printf_context = basic_printf_context<char>; +using wprintf_context = basic_printf_context<wchar_t>; using printf_args = basic_format_args<printf_context>; using wprintf_args = basic_format_args<wprintf_context>; @@ -581,25 +581,20 @@ inline auto make_printf_args(const T&... args) return {args...}; } -/** - \rst - Constructs an `~fmt::format_arg_store` object that contains references to - arguments and can be implicitly converted to `~fmt::wprintf_args`. - \endrst - */ +// DEPRECATED! template <typename... T> inline auto make_wprintf_args(const T&... args) -> format_arg_store<wprintf_context, T...> { return {args...}; } -template <typename S, typename Char = char_t<S>> +template <typename Char> inline auto vsprintf( - const S& fmt, - basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args) + basic_string_view<Char> fmt, + basic_format_args<basic_printf_context<type_identity_t<Char>>> args) -> std::basic_string<Char> { auto buf = basic_memory_buffer<Char>(); - detail::vprintf(buf, detail::to_string_view(fmt), args); + detail::vprintf(buf, fmt, args); return to_string(buf); } @@ -615,18 +610,17 @@ inline auto vsprintf( template <typename S, typename... T, typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>> inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string<Char> { - using context = basic_printf_context_t<Char>; return vsprintf(detail::to_string_view(fmt), - fmt::make_format_args<context>(args...)); + fmt::make_format_args<basic_printf_context<Char>>(args...)); } -template <typename S, typename Char = char_t<S>> +template <typename Char> inline auto vfprintf( - std::FILE* f, const S& fmt, - basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args) + std::FILE* f, basic_string_view<Char> fmt, + basic_format_args<basic_printf_context<type_identity_t<Char>>> args) -> int { auto buf = basic_memory_buffer<Char>(); - detail::vprintf(buf, detail::to_string_view(fmt), args); + detail::vprintf(buf, fmt, args); size_t size = buf.size(); return std::fwrite(buf.data(), sizeof(Char), size, f) < size ? -1 @@ -644,17 +638,16 @@ inline auto vfprintf( */ template <typename S, typename... T, typename Char = char_t<S>> inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int { - using context = basic_printf_context_t<Char>; return vfprintf(f, detail::to_string_view(fmt), - fmt::make_format_args<context>(args...)); + fmt::make_format_args<basic_printf_context<Char>>(args...)); } -template <typename S, typename Char = char_t<S>> -inline auto vprintf( - const S& fmt, - basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args) +template <typename Char> +FMT_DEPRECATED inline auto vprintf( + basic_string_view<Char> fmt, + basic_format_args<basic_printf_context<type_identity_t<Char>>> args) -> int { - return vfprintf(stdout, detail::to_string_view(fmt), args); + return vfprintf(stdout, fmt, args); } /** @@ -666,11 +659,14 @@ inline auto vprintf( fmt::printf("Elapsed time: %.2f seconds", 1.23); \endrst */ -template <typename S, typename... T, FMT_ENABLE_IF(detail::is_string<S>::value)> -inline auto printf(const S& fmt, const T&... args) -> int { - return vprintf( - detail::to_string_view(fmt), - fmt::make_format_args<basic_printf_context_t<char_t<S>>>(args...)); +template <typename... T> +inline auto printf(string_view fmt, const T&... args) -> int { + return vfprintf(stdout, fmt, make_printf_args(args...)); +} +template <typename... T> +FMT_DEPRECATED inline auto printf(basic_string_view<wchar_t> fmt, + const T&... args) -> int { + return vfprintf(stdout, fmt, make_wprintf_args(args...)); } FMT_END_EXPORT |