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