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