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