| Victor Zverovich | c095445 | 2018-01-06 09:09:50 -0800 | [diff] [blame] | 1 | // Formatting library for C++ |
| 2 | // |
| 3 | // Copyright (c) 2012 - 2016, Victor Zverovich |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // For the license information refer to format.h. |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 7 | |
| 8 | #ifndef FMT_PRINTF_H_ |
| 9 | #define FMT_PRINTF_H_ |
| 10 | |
| 11 | #include <algorithm> // std::fill_n |
| 12 | #include <limits> // std::numeric_limits |
| 13 | |
| Victor Zverovich | f853d94 | 2018-01-20 10:28:10 -0800 | [diff] [blame] | 14 | #include "ostream.h" |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 15 | |
| Victor Zverovich | 838400d | 2018-05-12 08:33:51 -0700 | [diff] [blame] | 16 | FMT_BEGIN_NAMESPACE |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 17 | namespace internal { |
| 18 | |
| 19 | // Checks if a value fits in int - used to avoid warnings about comparing |
| 20 | // signed and unsigned integers. |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 21 | template <bool IsSigned> struct int_checker { |
| 22 | template <typename T> static bool fits_in_int(T value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 23 | unsigned max = std::numeric_limits<int>::max(); |
| 24 | return value <= max; |
| 25 | } |
| 26 | static bool fits_in_int(bool) { return true; } |
| 27 | }; |
| 28 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 29 | template <> struct int_checker<true> { |
| 30 | template <typename T> static bool fits_in_int(T value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 31 | return value >= std::numeric_limits<int>::min() && |
| 32 | value <= std::numeric_limits<int>::max(); |
| 33 | } |
| 34 | static bool fits_in_int(int) { return true; } |
| 35 | }; |
| 36 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 37 | class printf_precision_handler : public function<int> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 38 | public: |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 39 | template <typename T> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 40 | typename std::enable_if<std::is_integral<T>::value, int>::type operator()( |
| 41 | T value) { |
| Victor Zverovich | 418659a | 2018-03-03 14:04:59 -0800 | [diff] [blame] | 42 | if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value)) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 43 | FMT_THROW(format_error("number is too big")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 44 | return static_cast<int>(value); |
| 45 | } |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 46 | |
| 47 | template <typename T> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 48 | typename std::enable_if<!std::is_integral<T>::value, int>::type operator()( |
| 49 | T) { |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 50 | FMT_THROW(format_error("precision is not integer")); |
| Victor Zverovich | 6322b47 | 2018-06-06 16:51:35 +0200 | [diff] [blame] | 51 | return 0; |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 52 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 55 | // An argument visitor that returns true iff arg is a zero integer. |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 56 | class is_zero_int : public function<bool> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 57 | public: |
| 58 | template <typename T> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 59 | typename std::enable_if<std::is_integral<T>::value, bool>::type operator()( |
| 60 | T value) { |
| 61 | return value == 0; |
| 62 | } |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 63 | |
| 64 | template <typename T> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 65 | typename std::enable_if<!std::is_integral<T>::value, bool>::type operator()( |
| 66 | T) { |
| 67 | return false; |
| 68 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 71 | template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {}; |
| Victor Zverovich | 0fbd846 | 2017-09-04 11:41:15 -0700 | [diff] [blame] | 72 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 73 | template <> struct make_unsigned_or_bool<bool> { typedef bool type; }; |
| Victor Zverovich | 0fbd846 | 2017-09-04 11:41:15 -0700 | [diff] [blame] | 74 | |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 75 | template <typename T, typename Context> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 76 | class arg_converter : public function<void> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 77 | private: |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 78 | typedef typename Context::char_type Char; |
| 79 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 80 | basic_format_arg<Context>& arg_; |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 81 | typename Context::char_type type_; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 82 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 83 | public: |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 84 | arg_converter(basic_format_arg<Context>& arg, Char type) |
| 85 | : arg_(arg), type_(type) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 86 | |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 87 | void operator()(bool value) { |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 88 | if (type_ != 's') operator()<bool>(value); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | template <typename U> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 92 | typename std::enable_if<std::is_integral<U>::value>::type operator()( |
| 93 | U value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 94 | bool is_signed = type_ == 'd' || type_ == 'i'; |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 95 | typedef typename std::conditional<std::is_same<T, void>::value, U, T>::type |
| 96 | TargetType; |
| Victor Zverovich | 5013c15 | 2018-02-10 06:52:46 -0800 | [diff] [blame] | 97 | if (const_check(sizeof(TargetType) <= sizeof(int))) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 98 | // Extra casts are used to silence warnings. |
| 99 | if (is_signed) { |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 100 | arg_ = internal::make_arg<Context>( |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 101 | static_cast<int>(static_cast<TargetType>(value))); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 102 | } else { |
| Victor Zverovich | 0fbd846 | 2017-09-04 11:41:15 -0700 | [diff] [blame] | 103 | typedef typename make_unsigned_or_bool<TargetType>::type Unsigned; |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 104 | arg_ = internal::make_arg<Context>( |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 105 | static_cast<unsigned>(static_cast<Unsigned>(value))); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 106 | } |
| 107 | } else { |
| 108 | if (is_signed) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 109 | // glibc's printf doesn't sign extend arguments of smaller types: |
| 110 | // std::printf("%lld", -42); // prints "4294967254" |
| 111 | // but we don't have to do the same because it's a UB. |
| Victor Zverovich | 016aceb | 2017-08-26 09:09:43 -0700 | [diff] [blame] | 112 | arg_ = internal::make_arg<Context>(static_cast<long long>(value)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 113 | } else { |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 114 | arg_ = internal::make_arg<Context>( |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 115 | static_cast<typename make_unsigned_or_bool<U>::type>(value)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 119 | |
| 120 | template <typename U> |
| Victor Zverovich | 77c892c | 2017-08-27 08:16:46 -0700 | [diff] [blame] | 121 | typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) { |
| Bruce Mitchener | 4523053 | 2019-03-10 08:06:30 +0700 | [diff] [blame] | 122 | // No conversion needed for non-integral types. |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 123 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 126 | // Converts an integer argument to T for printf, if T is an integral type. |
| 127 | // If T is void, the argument is converted to corresponding signed or unsigned |
| 128 | // type depending on the type specifier: 'd' and 'i' - signed, other - |
| 129 | // unsigned). |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 130 | template <typename T, typename Context, typename Char> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 131 | void convert_arg(basic_format_arg<Context>& arg, Char type) { |
| Victor Zverovich | f548063 | 2018-10-05 07:15:41 -0700 | [diff] [blame] | 132 | visit_format_arg(arg_converter<T, Context>(arg, type), arg); |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 135 | // Converts an integer argument to char for printf. |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 136 | template <typename Context> class char_converter : public function<void> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 137 | private: |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 138 | basic_format_arg<Context>& arg_; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 139 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 140 | public: |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 141 | explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 142 | |
| 143 | template <typename T> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 144 | typename std::enable_if<std::is_integral<T>::value>::type operator()( |
| 145 | T value) { |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 146 | typedef typename Context::char_type Char; |
| 147 | arg_ = internal::make_arg<Context>(static_cast<Char>(value)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 148 | } |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 149 | |
| 150 | template <typename T> |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 151 | typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) { |
| Bruce Mitchener | 4523053 | 2019-03-10 08:06:30 +0700 | [diff] [blame] | 152 | // No conversion needed for non-integral types. |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 153 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | // Checks if an argument is a valid printf width specifier and sets |
| 157 | // left alignment if it is negative. |
| Victor Zverovich | bf0f107 | 2017-01-28 13:17:47 +0000 | [diff] [blame] | 158 | template <typename Char> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 159 | class printf_width_handler : public function<unsigned> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 160 | private: |
| Victor Zverovich | bf0f107 | 2017-01-28 13:17:47 +0000 | [diff] [blame] | 161 | typedef basic_format_specs<Char> format_specs; |
| 162 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 163 | format_specs& spec_; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 164 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 165 | public: |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 166 | explicit printf_width_handler(format_specs& spec) : spec_(spec) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 167 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 168 | template <typename T> |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 169 | typename std::enable_if<std::is_integral<T>::value, unsigned>::type |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 170 | operator()(T value) { |
| Victor Zverovich | 6a2ff28 | 2017-02-19 06:46:51 -0800 | [diff] [blame] | 171 | typedef typename internal::int_traits<T>::main_type UnsignedType; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 172 | UnsignedType width = static_cast<UnsignedType>(value); |
| 173 | if (internal::is_negative(value)) { |
| 174 | spec_.align_ = ALIGN_LEFT; |
| 175 | width = 0 - width; |
| 176 | } |
| Victor Zverovich | e0d6f63 | 2016-06-15 06:29:47 -0700 | [diff] [blame] | 177 | unsigned int_max = std::numeric_limits<int>::max(); |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 178 | if (width > int_max) FMT_THROW(format_error("number is too big")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 179 | return static_cast<unsigned>(width); |
| 180 | } |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 181 | |
| 182 | template <typename T> |
| 183 | typename std::enable_if<!std::is_integral<T>::value, unsigned>::type |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 184 | operator()(T) { |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 185 | FMT_THROW(format_error("width is not integer")); |
| Victor Zverovich | 6322b47 | 2018-06-06 16:51:35 +0200 | [diff] [blame] | 186 | return 0; |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 187 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 188 | }; |
| Victor Zverovich | 8c2e15a | 2018-12-12 16:07:54 -0800 | [diff] [blame] | 189 | |
| 190 | template <typename Char, typename Context> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 191 | void printf(basic_buffer<Char>& buf, basic_string_view<Char> format, |
| Victor Zverovich | 8c2e15a | 2018-12-12 16:07:54 -0800 | [diff] [blame] | 192 | basic_format_args<Context> args) { |
| 193 | Context(std::back_inserter(buf), format, args).format(); |
| 194 | } |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 195 | |
| 196 | template <typename OutputIt, typename Char, typename Context> |
| 197 | internal::truncating_iterator<OutputIt> printf( |
| 198 | internal::truncating_iterator<OutputIt> it, basic_string_view<Char> format, |
| 199 | basic_format_args<Context> args) { |
| 200 | return Context(it, format, args).format(); |
| 201 | } |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 202 | } // namespace internal |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 203 | |
| Victor Zverovich | 8c2e15a | 2018-12-12 16:07:54 -0800 | [diff] [blame] | 204 | using internal::printf; // For printing into memory_buffer. |
| 205 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 206 | template <typename Range> class printf_arg_formatter; |
| Victor Zverovich | 10e70a0 | 2017-12-02 09:44:48 -0800 | [diff] [blame] | 207 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 208 | template <typename OutputIt, typename Char, |
| 209 | typename ArgFormatter = printf_arg_formatter< |
| 210 | back_insert_range<internal::basic_buffer<Char>>>> |
| Victor Zverovich | 217e7c7 | 2018-01-14 07:19:23 -0800 | [diff] [blame] | 211 | class basic_printf_context; |
| Victor Zverovich | 10e70a0 | 2017-12-02 09:44:48 -0800 | [diff] [blame] | 212 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 213 | /** |
| 214 | \rst |
| Victor Zverovich | d58cc8a | 2016-11-20 07:42:38 -0800 | [diff] [blame] | 215 | The ``printf`` argument formatter. |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 216 | \endrst |
| 217 | */ |
| Victor Zverovich | c095445 | 2018-01-06 09:09:50 -0800 | [diff] [blame] | 218 | template <typename Range> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 219 | class printf_arg_formatter |
| 220 | : public internal::function< |
| 221 | typename internal::arg_formatter_base<Range>::iterator>, |
| 222 | public internal::arg_formatter_base<Range> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 223 | private: |
| Victor Zverovich | affb35c | 2018-02-11 09:23:47 -0800 | [diff] [blame] | 224 | typedef typename Range::value_type char_type; |
| Victor Zverovich | 418659a | 2018-03-03 14:04:59 -0800 | [diff] [blame] | 225 | typedef decltype(internal::declval<Range>().begin()) iterator; |
| Victor Zverovich | affb35c | 2018-02-11 09:23:47 -0800 | [diff] [blame] | 226 | typedef internal::arg_formatter_base<Range> base; |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 227 | typedef basic_printf_context<iterator, char_type, printf_arg_formatter> |
| 228 | context_type; |
| Victor Zverovich | bd8a7e7 | 2018-01-21 14:30:38 -0800 | [diff] [blame] | 229 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 230 | context_type& context_; |
| Victor Zverovich | 10e70a0 | 2017-12-02 09:44:48 -0800 | [diff] [blame] | 231 | |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 232 | void write_null_pointer(char) { |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 233 | this->spec()->type = 0; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 234 | this->write("(nil)"); |
| 235 | } |
| 236 | |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 237 | void write_null_pointer(wchar_t) { |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 238 | this->spec()->type = 0; |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 239 | this->write(L"(nil)"); |
| 240 | } |
| 241 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 242 | public: |
| Victor Zverovich | affb35c | 2018-02-11 09:23:47 -0800 | [diff] [blame] | 243 | typedef typename base::format_specs format_specs; |
| Victor Zverovich | bf0f107 | 2017-01-28 13:17:47 +0000 | [diff] [blame] | 244 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 245 | /** |
| 246 | \rst |
| 247 | Constructs an argument formatter object. |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 248 | *buffer* is a reference to the output buffer and *spec* contains format |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 249 | specifier information for standard argument types. |
| 250 | \endrst |
| 251 | */ |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 252 | printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx) |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 253 | : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 254 | |
| Victor Zverovich | e928b67 | 2018-07-04 11:36:38 -0700 | [diff] [blame] | 255 | template <typename T> |
| 256 | typename std::enable_if<std::is_integral<T>::value, iterator>::type |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 257 | operator()(T value) { |
| Victor Zverovich | e928b67 | 2018-07-04 11:36:38 -0700 | [diff] [blame] | 258 | // MSVC2013 fails to compile separate overloads for bool and char_type so |
| 259 | // use std::is_same instead. |
| 260 | if (std::is_same<T, bool>::value) { |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 261 | format_specs& fmt_spec = *this->spec(); |
| 262 | if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 263 | fmt_spec.type = 0; |
| Victor Zverovich | 479ee2a | 2018-07-04 13:17:03 -0700 | [diff] [blame] | 264 | this->write(value != 0); |
| Victor Zverovich | e928b67 | 2018-07-04 11:36:38 -0700 | [diff] [blame] | 265 | } else if (std::is_same<T, char_type>::value) { |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 266 | format_specs& fmt_spec = *this->spec(); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 267 | if (fmt_spec.type && fmt_spec.type != 'c') |
| Victor Zverovich | e928b67 | 2018-07-04 11:36:38 -0700 | [diff] [blame] | 268 | return (*this)(static_cast<int>(value)); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 269 | fmt_spec.flags = 0; |
| Victor Zverovich | e928b67 | 2018-07-04 11:36:38 -0700 | [diff] [blame] | 270 | fmt_spec.align_ = ALIGN_RIGHT; |
| 271 | return base::operator()(value); |
| 272 | } else { |
| 273 | return base::operator()(value); |
| 274 | } |
| Victor Zverovich | 3cf0526 | 2018-03-30 08:20:12 -1000 | [diff] [blame] | 275 | return this->out(); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| Victor Zverovich | e928b67 | 2018-07-04 11:36:38 -0700 | [diff] [blame] | 278 | template <typename T> |
| 279 | typename std::enable_if<std::is_floating_point<T>::value, iterator>::type |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 280 | operator()(T value) { |
| Victor Zverovich | 3cf0526 | 2018-03-30 08:20:12 -1000 | [diff] [blame] | 281 | return base::operator()(value); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 284 | /** Formats a null-terminated C string. */ |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 285 | iterator operator()(const char* value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 286 | if (value) |
| Victor Zverovich | c095445 | 2018-01-06 09:09:50 -0800 | [diff] [blame] | 287 | base::operator()(value); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 288 | else if (this->spec()->type == 'p') |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 289 | write_null_pointer(char_type()); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 290 | else |
| 291 | this->write("(null)"); |
| Victor Zverovich | 3cf0526 | 2018-03-30 08:20:12 -1000 | [diff] [blame] | 292 | return this->out(); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 295 | /** Formats a null-terminated wide C string. */ |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 296 | iterator operator()(const wchar_t* value) { |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 297 | if (value) |
| 298 | base::operator()(value); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 299 | else if (this->spec()->type == 'p') |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 300 | write_null_pointer(char_type()); |
| 301 | else |
| 302 | this->write(L"(null)"); |
| 303 | return this->out(); |
| 304 | } |
| 305 | |
| Victor Zverovich | 479ee2a | 2018-07-04 13:17:03 -0700 | [diff] [blame] | 306 | iterator operator()(basic_string_view<char_type> value) { |
| 307 | return base::operator()(value); |
| 308 | } |
| 309 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 310 | iterator operator()(monostate value) { return base::operator()(value); } |
| Victor Zverovich | 479ee2a | 2018-07-04 13:17:03 -0700 | [diff] [blame] | 311 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 312 | /** Formats a pointer. */ |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 313 | iterator operator()(const void* value) { |
| 314 | if (value) return base::operator()(value); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 315 | this->spec()->type = 0; |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 316 | write_null_pointer(char_type()); |
| Victor Zverovich | 3cf0526 | 2018-03-30 08:20:12 -1000 | [diff] [blame] | 317 | return this->out(); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 320 | /** Formats an argument of a custom (user-defined) type. */ |
| Victor Zverovich | 23759b2 | 2018-04-04 07:38:21 -0700 | [diff] [blame] | 321 | iterator operator()(typename basic_format_arg<context_type>::handle handle) { |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 322 | handle.format(context_.parse_context(), context_); |
| Victor Zverovich | 3cf0526 | 2018-03-30 08:20:12 -1000 | [diff] [blame] | 323 | return this->out(); |
| Victor Zverovich | 5e0562a | 2017-08-13 13:09:02 -0700 | [diff] [blame] | 324 | } |
| 325 | }; |
| 326 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 327 | template <typename T> struct printf_formatter { |
| Victor Zverovich | 8cbf544 | 2017-09-17 08:32:57 -0700 | [diff] [blame] | 328 | template <typename ParseContext> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 329 | auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { |
| 330 | return ctx.begin(); |
| 331 | } |
| Victor Zverovich | 5e0562a | 2017-08-13 13:09:02 -0700 | [diff] [blame] | 332 | |
| Victor Zverovich | 91ee9c9 | 2018-01-14 11:00:27 -0800 | [diff] [blame] | 333 | template <typename FormatContext> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 334 | auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) { |
| Victor Zverovich | fa9066f | 2018-04-22 09:16:32 -0700 | [diff] [blame] | 335 | internal::format_value(internal::get_container(ctx.out()), value); |
| 336 | return ctx.out(); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 337 | } |
| 338 | }; |
| 339 | |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 340 | /** This template formats data and writes the output to a writer. */ |
| Victor Zverovich | bd8a7e7 | 2018-01-21 14:30:38 -0800 | [diff] [blame] | 341 | template <typename OutputIt, typename Char, typename ArgFormatter> |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 342 | class basic_printf_context { |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame] | 343 | public: |
| 344 | /** The character type for the output. */ |
| Victor Zverovich | affb35c | 2018-02-11 09:23:47 -0800 | [diff] [blame] | 345 | typedef Char char_type; |
| Victor Zverovich | e4572e5 | 2019-02-10 06:34:47 -0800 | [diff] [blame] | 346 | typedef basic_format_arg<basic_printf_context> format_arg; |
| Victor Zverovich | 5e0562a | 2017-08-13 13:09:02 -0700 | [diff] [blame] | 347 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 348 | template <typename T> struct formatter_type { |
| 349 | typedef printf_formatter<T> type; |
| 350 | }; |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame] | 351 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 352 | private: |
| Victor Zverovich | affb35c | 2018-02-11 09:23:47 -0800 | [diff] [blame] | 353 | typedef basic_format_specs<char_type> format_specs; |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 354 | |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 355 | OutputIt out_; |
| 356 | basic_format_args<basic_printf_context> args_; |
| 357 | basic_parse_context<Char> parse_ctx_; |
| 358 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 359 | static void parse_flags(format_specs& spec, const Char*& it, const Char* end); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 360 | |
| 361 | // Returns the argument with specified index or, if arg_index is equal |
| 362 | // to the maximum unsigned value, the next argument. |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 363 | format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max()); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 364 | |
| 365 | // Parses argument index, flags and width and returns the argument index. |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 366 | unsigned parse_header(const Char*& it, const Char* end, format_specs& spec); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 367 | |
| 368 | public: |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 369 | /** |
| 370 | \rst |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 371 | Constructs a ``printf_context`` object. References to the arguments and |
| 372 | the writer are stored in the context object so make sure they have |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 373 | appropriate lifetimes. |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 374 | \endrst |
| 375 | */ |
| Victor Zverovich | bd8a7e7 | 2018-01-21 14:30:38 -0800 | [diff] [blame] | 376 | basic_printf_context(OutputIt out, basic_string_view<char_type> format_str, |
| Victor Zverovich | 217e7c7 | 2018-01-14 07:19:23 -0800 | [diff] [blame] | 377 | basic_format_args<basic_printf_context> args) |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 378 | : out_(out), args_(args), parse_ctx_(format_str) {} |
| Victor Zverovich | 8cbf544 | 2017-09-17 08:32:57 -0700 | [diff] [blame] | 379 | |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 380 | OutputIt out() { return out_; } |
| 381 | void advance_to(OutputIt it) { out_ = it; } |
| 382 | |
| 383 | format_arg arg(unsigned id) const { return args_.get(id); } |
| 384 | |
| 385 | basic_parse_context<Char>& parse_context() { return parse_ctx_; } |
| 386 | |
| 387 | FMT_CONSTEXPR void on_error(const char* message) { |
| 388 | parse_ctx_.on_error(message); |
| 389 | } |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 390 | |
| Victor Zverovich | c095445 | 2018-01-06 09:09:50 -0800 | [diff] [blame] | 391 | /** Formats stored arguments and writes the output to the range. */ |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 392 | OutputIt format(); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 393 | }; |
| 394 | |
| Victor Zverovich | bd8a7e7 | 2018-01-21 14:30:38 -0800 | [diff] [blame] | 395 | template <typename OutputIt, typename Char, typename AF> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 396 | void basic_printf_context<OutputIt, Char, AF>::parse_flags(format_specs& spec, |
| 397 | const Char*& it, |
| 398 | const Char* end) { |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 399 | for (; it != end; ++it) { |
| 400 | switch (*it) { |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 401 | case '-': |
| 402 | spec.align_ = ALIGN_LEFT; |
| 403 | break; |
| 404 | case '+': |
| 405 | spec.flags |= SIGN_FLAG | PLUS_FLAG; |
| 406 | break; |
| 407 | case '0': |
| 408 | spec.fill_ = '0'; |
| 409 | break; |
| 410 | case ' ': |
| 411 | spec.flags |= SIGN_FLAG; |
| 412 | break; |
| 413 | case '#': |
| 414 | spec.flags |= HASH_FLAG; |
| 415 | break; |
| 416 | default: |
| 417 | return; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| Victor Zverovich | bd8a7e7 | 2018-01-21 14:30:38 -0800 | [diff] [blame] | 422 | template <typename OutputIt, typename Char, typename AF> |
| 423 | typename basic_printf_context<OutputIt, Char, AF>::format_arg |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 424 | basic_printf_context<OutputIt, Char, AF>::get_arg(unsigned arg_index) { |
| Victor Zverovich | c523dd5 | 2017-11-19 07:36:01 -0800 | [diff] [blame] | 425 | if (arg_index == std::numeric_limits<unsigned>::max()) |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 426 | arg_index = parse_ctx_.next_arg_id(); |
| 427 | else |
| 428 | parse_ctx_.check_arg_id(--arg_index); |
| 429 | return internal::get_arg(*this, arg_index); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| Victor Zverovich | bd8a7e7 | 2018-01-21 14:30:38 -0800 | [diff] [blame] | 432 | template <typename OutputIt, typename Char, typename AF> |
| 433 | unsigned basic_printf_context<OutputIt, Char, AF>::parse_header( |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 434 | const Char*& it, const Char* end, format_specs& spec) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 435 | unsigned arg_index = std::numeric_limits<unsigned>::max(); |
| Victor Zverovich | c095445 | 2018-01-06 09:09:50 -0800 | [diff] [blame] | 436 | char_type c = *it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 437 | if (c >= '0' && c <= '9') { |
| 438 | // Parse an argument index (if followed by '$') or a width possibly |
| 439 | // preceded with '0' flag(s). |
| Victor Zverovich | 932ab2b | 2017-10-21 08:37:52 -0700 | [diff] [blame] | 440 | internal::error_handler eh; |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 441 | unsigned value = parse_nonnegative_int(it, end, eh); |
| 442 | if (it != end && *it == '$') { // value is an argument index |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 443 | ++it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 444 | arg_index = value; |
| 445 | } else { |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 446 | if (c == '0') spec.fill_ = '0'; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 447 | if (value != 0) { |
| 448 | // Nonzero value means that we parsed width and don't need to |
| 449 | // parse it or flags again, so return now. |
| 450 | spec.width_ = value; |
| 451 | return arg_index; |
| 452 | } |
| 453 | } |
| 454 | } |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 455 | parse_flags(spec, it, end); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 456 | // Parse width. |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 457 | if (it != end) { |
| 458 | if (*it >= '0' && *it <= '9') { |
| 459 | internal::error_handler eh; |
| 460 | spec.width_ = parse_nonnegative_int(it, end, eh); |
| 461 | } else if (*it == '*') { |
| 462 | ++it; |
| 463 | spec.width_ = visit_format_arg( |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 464 | internal::printf_width_handler<char_type>(spec), get_arg()); |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 465 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 466 | } |
| 467 | return arg_index; |
| 468 | } |
| 469 | |
| Victor Zverovich | bd8a7e7 | 2018-01-21 14:30:38 -0800 | [diff] [blame] | 470 | template <typename OutputIt, typename Char, typename AF> |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 471 | OutputIt basic_printf_context<OutputIt, Char, AF>::format() { |
| 472 | auto out = this->out(); |
| Victor Zverovich | 442fa1b | 2019-02-09 19:34:42 -0800 | [diff] [blame] | 473 | const Char* start = parse_ctx_.begin(); |
| 474 | const Char* end = parse_ctx_.end(); |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 475 | auto it = start; |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 476 | while (it != end) { |
| Victor Zverovich | c095445 | 2018-01-06 09:09:50 -0800 | [diff] [blame] | 477 | char_type c = *it++; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 478 | if (c != '%') continue; |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 479 | if (it != end && *it == c) { |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 480 | out = std::copy(start, it, out); |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 481 | start = ++it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 482 | continue; |
| 483 | } |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 484 | out = std::copy(start, it - 1, out); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 485 | |
| Victor Zverovich | 296e9ca | 2017-01-28 12:51:35 +0000 | [diff] [blame] | 486 | format_specs spec; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 487 | spec.align_ = ALIGN_RIGHT; |
| 488 | |
| 489 | // Parse argument index, flags and width. |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 490 | unsigned arg_index = parse_header(it, end, spec); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 491 | |
| 492 | // Parse precision. |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 493 | if (it != end && *it == '.') { |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 494 | ++it; |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 495 | c = it != end ? *it : 0; |
| 496 | if ('0' <= c && c <= '9') { |
| Victor Zverovich | 932ab2b | 2017-10-21 08:37:52 -0700 | [diff] [blame] | 497 | internal::error_handler eh; |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 498 | spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh)); |
| 499 | } else if (c == '*') { |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 500 | ++it; |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 501 | spec.precision = |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 502 | visit_format_arg(internal::printf_precision_handler(), get_arg()); |
| Victor Zverovich | 5060568 | 2018-01-27 17:56:19 -0800 | [diff] [blame] | 503 | } else { |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 504 | spec.precision = 0; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 508 | format_arg arg = get_arg(arg_index); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 509 | if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg)) |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 510 | spec.flags = static_cast<uint_least8_t>( |
| 511 | spec.flags & (~internal::to_unsigned<int>(HASH_FLAG))); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 512 | if (spec.fill_ == '0') { |
| Victor Zverovich | 7cea163 | 2017-12-09 06:19:15 -0800 | [diff] [blame] | 513 | if (arg.is_arithmetic()) |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 514 | spec.align_ = ALIGN_NUMERIC; |
| 515 | else |
| 516 | spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. |
| 517 | } |
| 518 | |
| 519 | // Parse length and convert the argument to the required type. |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 520 | c = it != end ? *it++ : 0; |
| 521 | char_type t = it != end ? *it : 0; |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 522 | using internal::convert_arg; |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 523 | switch (c) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 524 | case 'h': |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 525 | if (t == 'h') { |
| 526 | ++it; |
| 527 | t = it != end ? *it : 0; |
| 528 | convert_arg<signed char>(arg, t); |
| 529 | } else { |
| 530 | convert_arg<short>(arg, t); |
| 531 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 532 | break; |
| 533 | case 'l': |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 534 | if (t == 'l') { |
| 535 | ++it; |
| 536 | t = it != end ? *it : 0; |
| 537 | convert_arg<long long>(arg, t); |
| 538 | } else { |
| 539 | convert_arg<long>(arg, t); |
| 540 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 541 | break; |
| 542 | case 'j': |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 543 | convert_arg<intmax_t>(arg, t); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 544 | break; |
| 545 | case 'z': |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 546 | convert_arg<std::size_t>(arg, t); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 547 | break; |
| 548 | case 't': |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 549 | convert_arg<std::ptrdiff_t>(arg, t); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 550 | break; |
| 551 | case 'L': |
| 552 | // printf produces garbage when 'L' is omitted for long double, no |
| 553 | // need to do the same. |
| 554 | break; |
| 555 | default: |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 556 | --it; |
| Daniela Engert | f5cc77c | 2018-12-31 16:21:45 +0100 | [diff] [blame] | 557 | convert_arg<void>(arg, c); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | // Parse type. |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 561 | if (it == end) FMT_THROW(format_error("invalid format string")); |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 562 | spec.type = static_cast<char>(*it++); |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 563 | if (arg.is_integral()) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 564 | // Normalize type. |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 565 | switch (spec.type) { |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 566 | case 'i': |
| 567 | case 'u': |
| Victor Zverovich | 2924622 | 2018-10-17 09:15:29 -0700 | [diff] [blame] | 568 | spec.type = 'd'; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 569 | break; |
| 570 | case 'c': |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 571 | visit_format_arg(internal::char_converter<basic_printf_context>(arg), |
| 572 | arg); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 573 | break; |
| 574 | } |
| 575 | } |
| 576 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 577 | start = it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 578 | |
| 579 | // Format argument. |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 580 | visit_format_arg(AF(out, spec, *this), arg); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 581 | } |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 582 | return std::copy(start, it, out); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 583 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 584 | |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 585 | template <typename Buffer> struct basic_printf_context_t { |
| 586 | typedef basic_printf_context<std::back_insert_iterator<Buffer>, |
| 587 | typename Buffer::value_type> |
| 588 | type; |
| Victor Zverovich | affb35c | 2018-02-11 09:23:47 -0800 | [diff] [blame] | 589 | }; |
| Victor Zverovich | 217e7c7 | 2018-01-14 07:19:23 -0800 | [diff] [blame] | 590 | |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 591 | typedef basic_printf_context_t<internal::buffer>::type printf_context; |
| 592 | typedef basic_printf_context_t<internal::wbuffer>::type wprintf_context; |
| 593 | |
| 594 | typedef basic_format_args<printf_context> printf_args; |
| 595 | typedef basic_format_args<wprintf_context> wprintf_args; |
| 596 | |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 597 | template <typename OutputIt, typename Char = typename OutputIt::value_type> |
| 598 | struct basic_printf_n_context_t { |
| 599 | typedef fmt::internal::truncating_iterator<OutputIt> OutputIter; |
| 600 | typedef output_range<OutputIter, Char> Range; |
| 601 | typedef basic_printf_context<OutputIter, Char, printf_arg_formatter<Range>> |
| 602 | type; |
| 603 | }; |
| 604 | |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 605 | /** |
| 606 | \rst |
| 607 | Constructs an `~fmt::format_arg_store` object that contains references to |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 608 | arguments and can be implicitly converted to `~fmt::printf_args`. |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 609 | \endrst |
| 610 | */ |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 611 | template <typename... Args> |
| 612 | inline format_arg_store<printf_context, Args...> make_printf_args( |
| 613 | const Args&... args) { |
| 614 | return {args...}; |
| 615 | } |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 616 | |
| 617 | /** |
| 618 | \rst |
| 619 | Constructs an `~fmt::format_arg_store` object that contains references to |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 620 | arguments and can be implicitly converted to `~fmt::wprintf_args`. |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 621 | \endrst |
| 622 | */ |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 623 | template <typename... Args> |
| 624 | inline format_arg_store<wprintf_context, Args...> make_wprintf_args( |
| 625 | const Args&... args) { |
| 626 | return {args...}; |
| 627 | } |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 628 | |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 629 | template <typename S, typename Char = FMT_CHAR(S)> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 630 | inline std::basic_string<Char> vsprintf( |
| 631 | const S& format, |
| 632 | basic_format_args< |
| 633 | typename basic_printf_context_t<internal::basic_buffer<Char>>::type> |
| 634 | args) { |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 635 | basic_memory_buffer<Char> buffer; |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 636 | printf(buffer, to_string_view(format), args); |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 637 | return to_string(buffer); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 640 | template <typename OutputIt, typename S, typename Char = FMT_CHAR(S)> |
| 641 | inline typename std::enable_if<internal::is_output_iterator<OutputIt>::value, |
| 642 | format_to_n_result<OutputIt>>::type |
| 643 | vsnprintf( |
| 644 | OutputIt out, std::size_t n, const S& format, |
| 645 | basic_format_args<typename basic_printf_n_context_t<OutputIt, Char>::type> |
| 646 | args) { |
| 647 | typedef internal::truncating_iterator<OutputIt> It; |
| 648 | auto it = printf(It(out, n), to_string_view(format), args); |
| 649 | return {it.base(), it.count()}; |
| 650 | } |
| 651 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 652 | /** |
| 653 | \rst |
| 654 | Formats arguments and returns the result as a string. |
| 655 | |
| 656 | **Example**:: |
| 657 | |
| 658 | std::string message = fmt::sprintf("The answer is %d", 42); |
| 659 | \endrst |
| 660 | */ |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 661 | template <typename S, typename... Args> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 662 | inline FMT_ENABLE_IF_T(internal::is_string<S>::value, |
| 663 | std::basic_string<FMT_CHAR(S)>) |
| 664 | sprintf(const S& format, const Args&... args) { |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 665 | internal::check_format_string<Args...>(format); |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 666 | typedef internal::basic_buffer<FMT_CHAR(S)> buffer; |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 667 | typedef typename basic_printf_context_t<buffer>::type context; |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 668 | format_arg_store<context, Args...> as{args...}; |
| 669 | return vsprintf(to_string_view(format), basic_format_args<context>(as)); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| Daniela Engert | b0cde86 | 2019-01-01 11:45:56 +0100 | [diff] [blame] | 672 | /** |
| 673 | \rst |
| 674 | Formats arguments for up to ``n`` characters stored through output iterator |
| 675 | ``out``. The function returns the updated iterator and the untruncated amount |
| 676 | of characters. |
| 677 | |
| 678 | **Example**:: |
| 679 | std::vector<char> out; |
| 680 | |
| 681 | typedef fmt::format_to_n_result< |
| 682 | std::back_insert_iterator<std::vector<char>>> res; |
| 683 | res Res = fmt::snprintf(std::back_inserter(out), 5, "The answer is %d", 42); |
| 684 | \endrst |
| 685 | */ |
| 686 | template <typename OutputIt, typename S, typename... Args> |
| 687 | inline FMT_ENABLE_IF_T(internal::is_string<S>::value&& |
| 688 | internal::is_output_iterator<OutputIt>::value, |
| 689 | format_to_n_result<OutputIt>) |
| 690 | snprintf(OutputIt out, std::size_t n, const S& format, |
| 691 | const Args&... args) { |
| 692 | internal::check_format_string<Args...>(format); |
| 693 | typedef FMT_CHAR(S) Char; |
| 694 | typedef typename basic_printf_n_context_t<OutputIt, Char>::type context; |
| 695 | format_arg_store<context, Args...> as{args...}; |
| 696 | return vsnprintf(out, n, to_string_view(format), |
| 697 | basic_format_args<context>(as)); |
| 698 | } |
| 699 | |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 700 | template <typename S, typename Char = FMT_CHAR(S)> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 701 | inline int vfprintf( |
| 702 | std::FILE* f, const S& format, |
| 703 | basic_format_args< |
| 704 | typename basic_printf_context_t<internal::basic_buffer<Char>>::type> |
| 705 | args) { |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 706 | basic_memory_buffer<Char> buffer; |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 707 | printf(buffer, to_string_view(format), args); |
| Victor Zverovich | 955062d | 2017-12-17 08:36:19 -0800 | [diff] [blame] | 708 | std::size_t size = buffer.size(); |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 709 | return std::fwrite(buffer.data(), sizeof(Char), size, f) < size |
| 710 | ? -1 |
| 711 | : static_cast<int>(size); |
| Victor Zverovich | 955062d | 2017-12-17 08:36:19 -0800 | [diff] [blame] | 712 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 713 | |
| 714 | /** |
| 715 | \rst |
| 716 | Prints formatted data to the file *f*. |
| 717 | |
| 718 | **Example**:: |
| 719 | |
| 720 | fmt::fprintf(stderr, "Don't %s!", "panic"); |
| 721 | \endrst |
| 722 | */ |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 723 | template <typename S, typename... Args> |
| Victor Zverovich | 7f7504b | 2018-12-12 18:21:13 -0800 | [diff] [blame] | 724 | inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int) |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 725 | fprintf(std::FILE* f, const S& format, const Args&... args) { |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 726 | internal::check_format_string<Args...>(format); |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 727 | typedef internal::basic_buffer<FMT_CHAR(S)> buffer; |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 728 | typedef typename basic_printf_context_t<buffer>::type context; |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 729 | format_arg_store<context, Args...> as{args...}; |
| 730 | return vfprintf(f, to_string_view(format), basic_format_args<context>(as)); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 733 | template <typename S, typename Char = FMT_CHAR(S)> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 734 | inline int vprintf( |
| 735 | const S& format, |
| 736 | basic_format_args< |
| 737 | typename basic_printf_context_t<internal::basic_buffer<Char>>::type> |
| 738 | args) { |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 739 | return vfprintf(stdout, to_string_view(format), args); |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 740 | } |
| 741 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 742 | /** |
| 743 | \rst |
| 744 | Prints formatted data to ``stdout``. |
| 745 | |
| 746 | **Example**:: |
| 747 | |
| 748 | fmt::printf("Elapsed time: %.2f seconds", 1.23); |
| 749 | \endrst |
| 750 | */ |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 751 | template <typename S, typename... Args> |
| Victor Zverovich | 7f7504b | 2018-12-12 18:21:13 -0800 | [diff] [blame] | 752 | inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int) |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 753 | printf(const S& format_str, const Args&... args) { |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 754 | internal::check_format_string<Args...>(format_str); |
| 755 | typedef internal::basic_buffer<FMT_CHAR(S)> buffer; |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 756 | typedef typename basic_printf_context_t<buffer>::type context; |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 757 | format_arg_store<context, Args...> as{args...}; |
| 758 | return vprintf(to_string_view(format_str), basic_format_args<context>(as)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 759 | } |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 760 | |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 761 | template <typename S, typename Char = FMT_CHAR(S)> |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 762 | inline int vfprintf( |
| 763 | std::basic_ostream<Char>& os, const S& format, |
| 764 | basic_format_args< |
| 765 | typename basic_printf_context_t<internal::basic_buffer<Char>>::type> |
| 766 | args) { |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 767 | basic_memory_buffer<Char> buffer; |
| Victor Zverovich | 0a96c03 | 2018-10-25 07:20:02 -0700 | [diff] [blame] | 768 | printf(buffer, to_string_view(format), args); |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 769 | internal::write(os, buffer); |
| 770 | return static_cast<int>(buffer.size()); |
| 771 | } |
| 772 | |
| Victor Zverovich | 9dbb60c | 2016-08-03 08:52:05 -0700 | [diff] [blame] | 773 | /** |
| 774 | \rst |
| 775 | Prints formatted data to the stream *os*. |
| 776 | |
| 777 | **Example**:: |
| 778 | |
| Victor Zverovich | 4023291 | 2018-03-04 09:55:17 -0800 | [diff] [blame] | 779 | fmt::fprintf(cerr, "Don't %s!", "panic"); |
| Victor Zverovich | 9dbb60c | 2016-08-03 08:52:05 -0700 | [diff] [blame] | 780 | \endrst |
| 781 | */ |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 782 | template <typename S, typename... Args> |
| Victor Zverovich | 7f7504b | 2018-12-12 18:21:13 -0800 | [diff] [blame] | 783 | inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int) |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 784 | fprintf(std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str, |
| 785 | const Args&... args) { |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 786 | internal::check_format_string<Args...>(format_str); |
| 787 | typedef internal::basic_buffer<FMT_CHAR(S)> buffer; |
| tnovotny | e37d6a9 | 2018-11-22 22:57:07 +0100 | [diff] [blame] | 788 | typedef typename basic_printf_context_t<buffer>::type context; |
| Victor Zverovich | 58b6f8d | 2019-01-12 18:27:38 -0800 | [diff] [blame] | 789 | format_arg_store<context, Args...> as{args...}; |
| Daniela Engert | 2c81c85 | 2018-10-08 20:14:39 +0200 | [diff] [blame] | 790 | return vfprintf(os, to_string_view(format_str), |
| Daniela Engert | f27defc | 2018-10-07 14:38:29 +0200 | [diff] [blame] | 791 | basic_format_args<context>(as)); |
| Daniela Engert | 2570f1a | 2018-04-26 20:32:14 +0200 | [diff] [blame] | 792 | } |
| Victor Zverovich | 838400d | 2018-05-12 08:33:51 -0700 | [diff] [blame] | 793 | FMT_END_NAMESPACE |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 794 | |
| 795 | #endif // FMT_PRINTF_H_ |