annotate dep/fmt/include/fmt/printf.h @ 363:f10507d8f686

CI/linux: deploy qt plugin as well
author Paper <paper@paper.us.eu.org>
date Mon, 15 Jul 2024 01:33:51 -0400
parents 4aeffed717ef
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
329
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 // Formatting library for C++ - legacy printf implementation
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 //
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 // Copyright (c) 2012 - 2016, Victor Zverovich
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 // All rights reserved.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 //
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 // For the license information refer to format.h.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #ifndef FMT_PRINTF_H_
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 #define FMT_PRINTF_H_
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 #include <algorithm> // std::max
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 #include <limits> // std::numeric_limits
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 #include "format.h"
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 FMT_BEGIN_NAMESPACE
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 FMT_BEGIN_EXPORT
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 template <typename T> struct printf_formatter {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 printf_formatter() = delete;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 template <typename Char> class basic_printf_context {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 private:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 detail::buffer_appender<Char> out_;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 basic_format_args<basic_printf_context> args_;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 static_assert(std::is_same<Char, char>::value ||
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 std::is_same<Char, wchar_t>::value,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 "Unsupported code unit type.");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 public:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 using char_type = Char;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 using parse_context_type = basic_format_parse_context<Char>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 template <typename T> using formatter_type = printf_formatter<T>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 /**
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 \rst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 Constructs a ``printf_context`` object. References to the arguments are
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 stored in the context object so make sure they have appropriate lifetimes.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 \endrst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 basic_printf_context(detail::buffer_appender<Char> out,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 basic_format_args<basic_printf_context> args)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 : out_(out), args_(args) {}
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 auto out() -> detail::buffer_appender<Char> { return out_; }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 void advance_to(detail::buffer_appender<Char>) {}
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 auto locale() -> detail::locale_ref { return {}; }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 auto arg(int id) const -> basic_format_arg<basic_printf_context> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 return args_.get(id);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 FMT_CONSTEXPR void on_error(const char* message) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 detail::error_handler().on_error(message);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 namespace detail {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 // Checks if a value fits in int - used to avoid warnings about comparing
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 // signed and unsigned integers.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 template <bool IsSigned> struct int_checker {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 template <typename T> static auto fits_in_int(T value) -> bool {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 unsigned max = max_value<int>();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 return value <= max;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 static auto fits_in_int(bool) -> bool { return true; }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 template <> struct int_checker<true> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 template <typename T> static auto fits_in_int(T value) -> bool {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 return value >= (std::numeric_limits<int>::min)() &&
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 value <= max_value<int>();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 static auto fits_in_int(int) -> bool { return true; }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 struct printf_precision_handler {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 auto operator()(T value) -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 throw_format_error("number is too big");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 return (std::max)(static_cast<int>(value), 0);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 auto operator()(T) -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 throw_format_error("precision is not integer");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 return 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96 // An argument visitor that returns true iff arg is a zero integer.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97 struct is_zero_int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99 auto operator()(T value) -> bool {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 return value == 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 auto operator()(T) -> bool {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 return false;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109 template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {};
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111 template <> struct make_unsigned_or_bool<bool> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 using type = bool;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 template <typename T, typename Context> class arg_converter {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116 private:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117 using char_type = typename Context::char_type;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 basic_format_arg<Context>& arg_;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120 char_type type_;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 public:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123 arg_converter(basic_format_arg<Context>& arg, char_type type)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124 : arg_(arg), type_(type) {}
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126 void operator()(bool value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 if (type_ != 's') operator()<bool>(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
131 void operator()(U value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
132 bool is_signed = type_ == 'd' || type_ == 'i';
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
133 using target_type = conditional_t<std::is_same<T, void>::value, U, T>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134 if (const_check(sizeof(target_type) <= sizeof(int))) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
135 // Extra casts are used to silence warnings.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
136 if (is_signed) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
137 auto n = static_cast<int>(static_cast<target_type>(value));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
138 arg_ = detail::make_arg<Context>(n);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
139 } else {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
140 using unsigned_type = typename make_unsigned_or_bool<target_type>::type;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
141 auto n = static_cast<unsigned>(static_cast<unsigned_type>(value));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
142 arg_ = detail::make_arg<Context>(n);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
143 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
144 } else {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
145 if (is_signed) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
146 // glibc's printf doesn't sign extend arguments of smaller types:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
147 // std::printf("%lld", -42); // prints "4294967254"
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
148 // but we don't have to do the same because it's a UB.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
149 auto n = static_cast<long long>(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
150 arg_ = detail::make_arg<Context>(n);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
151 } else {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
152 auto n = static_cast<typename make_unsigned_or_bool<U>::type>(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
153 arg_ = detail::make_arg<Context>(n);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
154 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
155 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
156 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
157
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
158 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
159 void operator()(U) {} // No conversion needed for non-integral types.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
160 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
161
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
162 // Converts an integer argument to T for printf, if T is an integral type.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
163 // If T is void, the argument is converted to corresponding signed or unsigned
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
164 // type depending on the type specifier: 'd' and 'i' - signed, other -
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
165 // unsigned).
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
166 template <typename T, typename Context, typename Char>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
167 void convert_arg(basic_format_arg<Context>& arg, Char type) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
168 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
169 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
170
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
171 // Converts an integer argument to char for printf.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
172 template <typename Context> class char_converter {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
173 private:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
174 basic_format_arg<Context>& arg_;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
175
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
176 public:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
177 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {}
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
178
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
179 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
180 void operator()(T value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
181 auto c = static_cast<typename Context::char_type>(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
182 arg_ = detail::make_arg<Context>(c);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
183 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
184
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
185 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
186 void operator()(T) {} // No conversion needed for non-integral types.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
187 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
188
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
189 // An argument visitor that return a pointer to a C string if argument is a
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
190 // string or null otherwise.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
191 template <typename Char> struct get_cstring {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
192 template <typename T> auto operator()(T) -> const Char* { return nullptr; }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
193 auto operator()(const Char* s) -> const Char* { return s; }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
194 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
195
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
196 // Checks if an argument is a valid printf width specifier and sets
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
197 // left alignment if it is negative.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
198 template <typename Char> class printf_width_handler {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
199 private:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
200 format_specs<Char>& specs_;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
201
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
202 public:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
203 explicit printf_width_handler(format_specs<Char>& specs) : specs_(specs) {}
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
204
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
205 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
206 auto operator()(T value) -> unsigned {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
207 auto width = static_cast<uint32_or_64_or_128_t<T>>(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
208 if (detail::is_negative(value)) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
209 specs_.align = align::left;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
210 width = 0 - width;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
211 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
212 unsigned int_max = max_value<int>();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
213 if (width > int_max) throw_format_error("number is too big");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
214 return static_cast<unsigned>(width);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
215 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
216
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
217 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
218 auto operator()(T) -> unsigned {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
219 throw_format_error("width is not integer");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
220 return 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
221 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
222 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
223
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
224 // Workaround for a bug with the XL compiler when initializing
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
225 // printf_arg_formatter's base class.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
226 template <typename Char>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
227 auto make_arg_formatter(buffer_appender<Char> iter, format_specs<Char>& s)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
228 -> arg_formatter<Char> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
229 return {iter, s, locale_ref()};
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
230 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
231
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
232 // The ``printf`` argument formatter.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
233 template <typename Char>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
234 class printf_arg_formatter : public arg_formatter<Char> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
235 private:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
236 using base = arg_formatter<Char>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
237 using context_type = basic_printf_context<Char>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
238
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
239 context_type& context_;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
240
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
241 void write_null_pointer(bool is_string = false) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
242 auto s = this->specs;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
243 s.type = presentation_type::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
244 write_bytes(this->out, is_string ? "(null)" : "(nil)", s);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
245 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
246
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
247 public:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
248 printf_arg_formatter(buffer_appender<Char> iter, format_specs<Char>& s,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
249 context_type& ctx)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
250 : base(make_arg_formatter(iter, s)), context_(ctx) {}
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
251
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
252 void operator()(monostate value) { base::operator()(value); }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
253
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
254 template <typename T, FMT_ENABLE_IF(detail::is_integral<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
255 void operator()(T value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
256 // MSVC2013 fails to compile separate overloads for bool and Char so use
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
257 // std::is_same instead.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
258 if (!std::is_same<T, Char>::value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
259 base::operator()(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
260 return;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
261 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
262 format_specs<Char> fmt_specs = this->specs;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
263 if (fmt_specs.type != presentation_type::none &&
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
264 fmt_specs.type != presentation_type::chr) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
265 return (*this)(static_cast<int>(value));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
266 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
267 fmt_specs.sign = sign::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
268 fmt_specs.alt = false;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
269 fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
270 // align::numeric needs to be overwritten here since the '0' flag is
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
271 // ignored for non-numeric types
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
272 if (fmt_specs.align == align::none || fmt_specs.align == align::numeric)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
273 fmt_specs.align = align::right;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
274 write<Char>(this->out, static_cast<Char>(value), fmt_specs);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
275 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
276
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
277 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
278 void operator()(T value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
279 base::operator()(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
280 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
281
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
282 /** Formats a null-terminated C string. */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
283 void operator()(const char* value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
284 if (value)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
285 base::operator()(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
286 else
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
287 write_null_pointer(this->specs.type != presentation_type::pointer);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
288 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
289
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
290 /** Formats a null-terminated wide C string. */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
291 void operator()(const wchar_t* value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
292 if (value)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
293 base::operator()(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
294 else
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
295 write_null_pointer(this->specs.type != presentation_type::pointer);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
296 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
297
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
298 void operator()(basic_string_view<Char> value) { base::operator()(value); }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
299
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
300 /** Formats a pointer. */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
301 void operator()(const void* value) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
302 if (value)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
303 base::operator()(value);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
304 else
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
305 write_null_pointer();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
306 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
307
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
308 /** Formats an argument of a custom (user-defined) type. */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
309 void operator()(typename basic_format_arg<context_type>::handle handle) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
310 auto parse_ctx = basic_format_parse_context<Char>({});
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
311 handle.format(parse_ctx, context_);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
312 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
313 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
314
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
315 template <typename Char>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
316 void parse_flags(format_specs<Char>& specs, const Char*& it, const Char* end) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
317 for (; it != end; ++it) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
318 switch (*it) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
319 case '-':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
320 specs.align = align::left;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
321 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
322 case '+':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
323 specs.sign = sign::plus;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
324 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
325 case '0':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
326 specs.fill[0] = '0';
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
327 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
328 case ' ':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
329 if (specs.sign != sign::plus) specs.sign = sign::space;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
330 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
331 case '#':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
332 specs.alt = true;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
333 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
334 default:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
335 return;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
336 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
337 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
338 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
339
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
340 template <typename Char, typename GetArg>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
341 auto parse_header(const Char*& it, const Char* end, format_specs<Char>& specs,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
342 GetArg get_arg) -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
343 int arg_index = -1;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
344 Char c = *it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
345 if (c >= '0' && c <= '9') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
346 // Parse an argument index (if followed by '$') or a width possibly
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
347 // preceded with '0' flag(s).
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
348 int value = parse_nonnegative_int(it, end, -1);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
349 if (it != end && *it == '$') { // value is an argument index
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
350 ++it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
351 arg_index = value != -1 ? value : max_value<int>();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
352 } else {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
353 if (c == '0') specs.fill[0] = '0';
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
354 if (value != 0) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
355 // Nonzero value means that we parsed width and don't need to
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
356 // parse it or flags again, so return now.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
357 if (value == -1) throw_format_error("number is too big");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
358 specs.width = value;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
359 return arg_index;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
360 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
361 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
362 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
363 parse_flags(specs, it, end);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
364 // Parse width.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
365 if (it != end) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
366 if (*it >= '0' && *it <= '9') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
367 specs.width = parse_nonnegative_int(it, end, -1);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
368 if (specs.width == -1) throw_format_error("number is too big");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
369 } else if (*it == '*') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
370 ++it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
371 specs.width = static_cast<int>(visit_format_arg(
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
372 detail::printf_width_handler<Char>(specs), get_arg(-1)));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
373 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
374 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
375 return arg_index;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
376 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
377
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
378 inline auto parse_printf_presentation_type(char c, type t)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
379 -> presentation_type {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
380 using pt = presentation_type;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
381 constexpr auto integral_set = sint_set | uint_set | bool_set | char_set;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
382 switch (c) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
383 case 'd':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
384 return in(t, integral_set) ? pt::dec : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
385 case 'o':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
386 return in(t, integral_set) ? pt::oct : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
387 case 'x':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
388 return in(t, integral_set) ? pt::hex_lower : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
389 case 'X':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
390 return in(t, integral_set) ? pt::hex_upper : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
391 case 'a':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
392 return in(t, float_set) ? pt::hexfloat_lower : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
393 case 'A':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
394 return in(t, float_set) ? pt::hexfloat_upper : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
395 case 'e':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
396 return in(t, float_set) ? pt::exp_lower : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
397 case 'E':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
398 return in(t, float_set) ? pt::exp_upper : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
399 case 'f':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
400 return in(t, float_set) ? pt::fixed_lower : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
401 case 'F':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
402 return in(t, float_set) ? pt::fixed_upper : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
403 case 'g':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
404 return in(t, float_set) ? pt::general_lower : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
405 case 'G':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
406 return in(t, float_set) ? pt::general_upper : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
407 case 'c':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
408 return in(t, integral_set) ? pt::chr : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
409 case 's':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
410 return in(t, string_set | cstring_set) ? pt::string : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
411 case 'p':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
412 return in(t, pointer_set | cstring_set) ? pt::pointer : pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
413 default:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
414 return pt::none;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
415 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
416 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
417
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
418 template <typename Char, typename Context>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
419 void vprintf(buffer<Char>& buf, basic_string_view<Char> format,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
420 basic_format_args<Context> args) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
421 using iterator = buffer_appender<Char>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
422 auto out = iterator(buf);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
423 auto context = basic_printf_context<Char>(out, args);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
424 auto parse_ctx = basic_format_parse_context<Char>(format);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
425
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
426 // Returns the argument with specified index or, if arg_index is -1, the next
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
427 // argument.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
428 auto get_arg = [&](int arg_index) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
429 if (arg_index < 0)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
430 arg_index = parse_ctx.next_arg_id();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
431 else
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
432 parse_ctx.check_arg_id(--arg_index);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
433 return detail::get_arg(context, arg_index);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
434 };
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
435
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
436 const Char* start = parse_ctx.begin();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
437 const Char* end = parse_ctx.end();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
438 auto it = start;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
439 while (it != end) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
440 if (!find<false, Char>(it, end, '%', it)) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
441 it = end; // find leaves it == nullptr if it doesn't find '%'.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
442 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
443 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
444 Char c = *it++;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
445 if (it != end && *it == c) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
446 write(out, basic_string_view<Char>(start, to_unsigned(it - start)));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
447 start = ++it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
448 continue;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
449 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
450 write(out, basic_string_view<Char>(start, to_unsigned(it - 1 - start)));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
451
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
452 auto specs = format_specs<Char>();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
453 specs.align = align::right;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
454
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
455 // Parse argument index, flags and width.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
456 int arg_index = parse_header(it, end, specs, get_arg);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
457 if (arg_index == 0) throw_format_error("argument not found");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
458
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
459 // Parse precision.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
460 if (it != end && *it == '.') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
461 ++it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
462 c = it != end ? *it : 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
463 if ('0' <= c && c <= '9') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
464 specs.precision = parse_nonnegative_int(it, end, 0);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
465 } else if (c == '*') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
466 ++it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
467 specs.precision = static_cast<int>(
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
468 visit_format_arg(printf_precision_handler(), get_arg(-1)));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
469 } else {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
470 specs.precision = 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
471 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
472 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
473
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
474 auto arg = get_arg(arg_index);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
475 // For d, i, o, u, x, and X conversion specifiers, if a precision is
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
476 // specified, the '0' flag is ignored
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
477 if (specs.precision >= 0 && arg.is_integral()) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
478 // Ignore '0' for non-numeric types or if '-' present.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
479 specs.fill[0] = ' ';
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
480 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
481 if (specs.precision >= 0 && arg.type() == type::cstring_type) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
482 auto str = visit_format_arg(get_cstring<Char>(), arg);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
483 auto str_end = str + specs.precision;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
484 auto nul = std::find(str, str_end, Char());
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
485 auto sv = basic_string_view<Char>(
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
486 str, to_unsigned(nul != str_end ? nul - str : specs.precision));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
487 arg = make_arg<basic_printf_context<Char>>(sv);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
488 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
489 if (specs.alt && visit_format_arg(is_zero_int(), arg)) specs.alt = false;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
490 if (specs.fill[0] == '0') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
491 if (arg.is_arithmetic() && specs.align != align::left)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
492 specs.align = align::numeric;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
493 else
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
494 specs.fill[0] = ' '; // Ignore '0' flag for non-numeric types or if '-'
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
495 // flag is also present.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
496 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
497
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
498 // Parse length and convert the argument to the required type.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
499 c = it != end ? *it++ : 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
500 Char t = it != end ? *it : 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
501 switch (c) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
502 case 'h':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
503 if (t == 'h') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
504 ++it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
505 t = it != end ? *it : 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
506 convert_arg<signed char>(arg, t);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
507 } else {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
508 convert_arg<short>(arg, t);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
509 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
510 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
511 case 'l':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
512 if (t == 'l') {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
513 ++it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
514 t = it != end ? *it : 0;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
515 convert_arg<long long>(arg, t);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
516 } else {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
517 convert_arg<long>(arg, t);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
518 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
519 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
520 case 'j':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
521 convert_arg<intmax_t>(arg, t);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
522 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
523 case 'z':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
524 convert_arg<size_t>(arg, t);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
525 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
526 case 't':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
527 convert_arg<std::ptrdiff_t>(arg, t);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
528 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
529 case 'L':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
530 // printf produces garbage when 'L' is omitted for long double, no
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
531 // need to do the same.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
532 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
533 default:
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
534 --it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
535 convert_arg<void>(arg, c);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
536 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
537
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
538 // Parse type.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
539 if (it == end) throw_format_error("invalid format string");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
540 char type = static_cast<char>(*it++);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
541 if (arg.is_integral()) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
542 // Normalize type.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
543 switch (type) {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
544 case 'i':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
545 case 'u':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
546 type = 'd';
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
547 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
548 case 'c':
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
549 visit_format_arg(char_converter<basic_printf_context<Char>>(arg), arg);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
550 break;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
551 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
552 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
553 specs.type = parse_printf_presentation_type(type, arg.type());
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
554 if (specs.type == presentation_type::none)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
555 throw_format_error("invalid format specifier");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
556
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
557 start = it;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
558
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
559 // Format argument.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
560 visit_format_arg(printf_arg_formatter<Char>(out, specs, context), arg);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
561 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
562 write(out, basic_string_view<Char>(start, to_unsigned(it - start)));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
563 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
564 } // namespace detail
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
565
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
566 using printf_context = basic_printf_context<char>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
567 using wprintf_context = basic_printf_context<wchar_t>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
568
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
569 using printf_args = basic_format_args<printf_context>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
570 using wprintf_args = basic_format_args<wprintf_context>;
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
571
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
572 /**
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
573 \rst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
574 Constructs an `~fmt::format_arg_store` object that contains references to
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
575 arguments and can be implicitly converted to `~fmt::printf_args`.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
576 \endrst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
577 */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
578 template <typename... T>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
579 inline auto make_printf_args(const T&... args)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
580 -> format_arg_store<printf_context, T...> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
581 return {args...};
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
582 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
583
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
584 // DEPRECATED!
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
585 template <typename... T>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
586 inline auto make_wprintf_args(const T&... args)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
587 -> format_arg_store<wprintf_context, T...> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
588 return {args...};
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
589 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
590
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
591 template <typename Char>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
592 inline auto vsprintf(
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
593 basic_string_view<Char> fmt,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
594 basic_format_args<basic_printf_context<type_identity_t<Char>>> args)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
595 -> std::basic_string<Char> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
596 auto buf = basic_memory_buffer<Char>();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
597 detail::vprintf(buf, fmt, args);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
598 return to_string(buf);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
599 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
600
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
601 /**
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
602 \rst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
603 Formats arguments and returns the result as a string.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
604
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
605 **Example**::
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
606
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
607 std::string message = fmt::sprintf("The answer is %d", 42);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
608 \endrst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
609 */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
610 template <typename S, typename... T,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
611 typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
612 inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string<Char> {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
613 return vsprintf(detail::to_string_view(fmt),
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
614 fmt::make_format_args<basic_printf_context<Char>>(args...));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
615 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
616
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
617 template <typename Char>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
618 inline auto vfprintf(
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
619 std::FILE* f, basic_string_view<Char> fmt,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
620 basic_format_args<basic_printf_context<type_identity_t<Char>>> args)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
621 -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
622 auto buf = basic_memory_buffer<Char>();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
623 detail::vprintf(buf, fmt, args);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
624 size_t size = buf.size();
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
625 return std::fwrite(buf.data(), sizeof(Char), size, f) < size
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
626 ? -1
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
627 : static_cast<int>(size);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
628 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
629
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
630 /**
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
631 \rst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
632 Prints formatted data to the file *f*.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
633
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
634 **Example**::
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
635
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
636 fmt::fprintf(stderr, "Don't %s!", "panic");
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
637 \endrst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
638 */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
639 template <typename S, typename... T, typename Char = char_t<S>>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
640 inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
641 return vfprintf(f, detail::to_string_view(fmt),
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
642 fmt::make_format_args<basic_printf_context<Char>>(args...));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
643 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
644
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
645 template <typename Char>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
646 FMT_DEPRECATED inline auto vprintf(
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
647 basic_string_view<Char> fmt,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
648 basic_format_args<basic_printf_context<type_identity_t<Char>>> args)
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
649 -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
650 return vfprintf(stdout, fmt, args);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
651 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
652
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
653 /**
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
654 \rst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
655 Prints formatted data to ``stdout``.
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
656
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
657 **Example**::
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
658
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
659 fmt::printf("Elapsed time: %.2f seconds", 1.23);
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
660 \endrst
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
661 */
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
662 template <typename... T>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
663 inline auto printf(string_view fmt, const T&... args) -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
664 return vfprintf(stdout, fmt, make_printf_args(args...));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
665 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
666 template <typename... T>
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
667 FMT_DEPRECATED inline auto printf(basic_string_view<wchar_t> fmt,
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
668 const T&... args) -> int {
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
669 return vfprintf(stdout, fmt, make_wprintf_args(args...));
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
670 }
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
671
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
672 FMT_END_EXPORT
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
673 FMT_END_NAMESPACE
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
674
4aeffed717ef dep/fmt: add dependency
Paper <paper@paper.us.eu.org>
parents:
diff changeset
675 #endif // FMT_PRINTF_H_