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