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