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