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