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