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