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