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