| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 1 | /* |
| Victor Zverovich | 0996e98 | 2012-12-10 20:37:35 -0800 | [diff] [blame] | 2 | Small, safe and fast string formatting library for C++ |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 3 | Author: Victor Zverovich |
| 4 | */ |
| 5 | |
| 6 | #include "format.h" |
| 7 | |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 10 | #include <cassert> |
| Victor Zverovich | 63539c0 | 2012-12-08 08:17:12 -0800 | [diff] [blame] | 11 | #include <climits> |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 12 | #include <cstring> |
| 13 | #include <algorithm> |
| 14 | |
| 15 | using std::size_t; |
| 16 | |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 17 | namespace { |
| 18 | |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 19 | // Flags. |
| 20 | enum { PLUS_FLAG = 1, ZERO_FLAG = 2, HEX_PREFIX_FLAG = 4 }; |
| 21 | |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 22 | // Throws Exception(message) if format contains '}', otherwise throws |
| 23 | // FormatError reporting unmatched '{'. The idea is that unmatched '{' |
| 24 | // should override other errors. |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 25 | void ReportError(const char *s, const std::string &message) { |
| Victor Zverovich | 31a5070 | 2012-12-10 15:04:55 -0800 | [diff] [blame] | 26 | for (int num_open_braces = 1; *s; ++s) { |
| 27 | if (*s == '{') { |
| 28 | ++num_open_braces; |
| 29 | } else if (*s == '}') { |
| 30 | if (--num_open_braces == 0) |
| 31 | throw fmt::FormatError(message); |
| 32 | } |
| 33 | } |
| 34 | throw fmt::FormatError("unmatched '{' in format"); |
| Victor Zverovich | 5f3ed20 | 2012-12-07 17:48:10 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 37 | void ReportUnknownType(char code, const char *type) { |
| 38 | if (std::isprint(code)) { |
| 39 | throw fmt::FormatError( |
| 40 | str(fmt::Format("unknown format code '{0}' for {1}") << code << type)); |
| 41 | } |
| 42 | throw fmt::FormatError( |
| 43 | str(fmt::Format("unknown format code '\\x{0:02x}' for {1}") |
| 44 | << static_cast<unsigned>(code) << type)); |
| 45 | } |
| 46 | |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 47 | // Parses an unsigned integer advancing s to the end of the parsed input. |
| 48 | // This function assumes that the first character of s is a digit. |
| 49 | unsigned ParseUInt(const char *&s) { |
| 50 | assert('0' <= *s && *s <= '9'); |
| 51 | unsigned value = 0; |
| 52 | do { |
| 53 | unsigned new_value = value * 10 + (*s++ - '0'); |
| 54 | if (new_value < value) // Check if value wrapped around. |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 55 | ReportError(s, "number is too big in format"); |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 56 | value = new_value; |
| 57 | } while ('0' <= *s && *s <= '9'); |
| 58 | return value; |
| 59 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 60 | |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 61 | // Information about an integer type. |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 62 | template <typename T> |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 63 | struct IntTraits { |
| 64 | typedef T UnsignedType; |
| 65 | static bool IsNegative(T) { return false; } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | template <> |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 69 | struct IntTraits<int> { |
| 70 | typedef unsigned UnsignedType; |
| 71 | static bool IsNegative(int value) { return value < 0; } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | template <> |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 75 | struct IntTraits<long> { |
| 76 | typedef unsigned long UnsignedType; |
| 77 | static bool IsNegative(long value) { return value < 0; } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | template <typename T> |
| 81 | struct IsLongDouble { enum {VALUE = 0}; }; |
| 82 | |
| 83 | template <> |
| 84 | struct IsLongDouble<long double> { enum {VALUE = 1}; }; |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 87 | template <typename T> |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 88 | void fmt::Formatter::FormatInt(T value, unsigned flags, int width, char type) { |
| 89 | int size = 0; |
| 90 | char sign = 0; |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 91 | typedef typename IntTraits<T>::UnsignedType UnsignedType; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 92 | UnsignedType abs_value = value; |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 93 | if (IntTraits<T>::IsNegative(value)) { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 94 | sign = '-'; |
| 95 | ++size; |
| 96 | abs_value = -value; |
| 97 | } else if ((flags & PLUS_FLAG) != 0) { |
| 98 | sign = '+'; |
| 99 | ++size; |
| 100 | } |
| 101 | char fill = (flags & ZERO_FLAG) != 0 ? '0' : ' '; |
| 102 | size_t start = buffer_.size(); |
| 103 | char *p = 0; |
| 104 | switch (type) { |
| 105 | case 0: case 'd': { |
| 106 | UnsignedType n = abs_value; |
| 107 | do { |
| 108 | ++size; |
| 109 | } while ((n /= 10) != 0); |
| 110 | width = std::max(width, size); |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 111 | p = GrowBuffer(width) + width - 1; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 112 | n = abs_value; |
| 113 | do { |
| 114 | *p-- = '0' + (n % 10); |
| 115 | } while ((n /= 10) != 0); |
| 116 | break; |
| 117 | } |
| 118 | case 'x': case 'X': { |
| 119 | UnsignedType n = abs_value; |
| 120 | bool print_prefix = (flags & HEX_PREFIX_FLAG) != 0; |
| 121 | if (print_prefix) size += 2; |
| 122 | do { |
| 123 | ++size; |
| 124 | } while ((n >>= 4) != 0); |
| 125 | width = std::max(width, size); |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 126 | p = GrowBuffer(width) + width - 1; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 127 | n = abs_value; |
| 128 | const char *digits = type == 'x' ? "0123456789abcdef" : "0123456789ABCDEF"; |
| 129 | do { |
| 130 | *p-- = digits[n & 0xf]; |
| 131 | } while ((n >>= 4) != 0); |
| 132 | if (print_prefix) { |
| 133 | *p-- = type; |
| 134 | *p-- = '0'; |
| 135 | } |
| 136 | break; |
| 137 | } |
| 138 | case 'o': { |
| 139 | UnsignedType n = abs_value; |
| 140 | do { |
| 141 | ++size; |
| 142 | } while ((n >>= 3) != 0); |
| 143 | width = std::max(width, size); |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 144 | p = GrowBuffer(width) + width - 1; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 145 | n = abs_value; |
| 146 | do { |
| 147 | *p-- = '0' + (n & 7); |
| 148 | } while ((n >>= 3) != 0); |
| 149 | break; |
| 150 | } |
| 151 | default: |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 152 | ReportUnknownType(type, "integer"); |
| 153 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 154 | } |
| 155 | if (sign) { |
| 156 | if ((flags & ZERO_FLAG) != 0) |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 157 | buffer_[start++] = sign; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 158 | else |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 159 | *p-- = sign; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 160 | } |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 161 | std::fill(&buffer_[start], p + 1, fill); |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | template <typename T> |
| 165 | void fmt::Formatter::FormatDouble( |
| 166 | T value, unsigned flags, int width, int precision, char type) { |
| 167 | // Check type. |
| 168 | switch (type) { |
| Victor Zverovich | d73306b | 2012-12-10 12:16:02 -0800 | [diff] [blame] | 169 | case 0: |
| 170 | type = 'g'; |
| 171 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 172 | case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': |
| 173 | break; |
| 174 | default: |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 175 | ReportUnknownType(type, "double"); |
| 176 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Build format string. |
| 180 | enum { MAX_FORMAT_SIZE = 9}; // longest format: %+0*.*Lg |
| 181 | char format[MAX_FORMAT_SIZE]; |
| 182 | char *format_ptr = format; |
| 183 | *format_ptr++ = '%'; |
| 184 | if ((flags & PLUS_FLAG) != 0) |
| 185 | *format_ptr++ = '+'; |
| 186 | if ((flags & ZERO_FLAG) != 0) |
| 187 | *format_ptr++ = '0'; |
| 188 | if (width > 0) |
| 189 | *format_ptr++ = '*'; |
| 190 | if (precision >= 0) { |
| 191 | *format_ptr++ = '.'; |
| 192 | *format_ptr++ = '*'; |
| 193 | } |
| 194 | if (IsLongDouble<T>::VALUE) |
| 195 | *format_ptr++ = 'L'; |
| Victor Zverovich | d73306b | 2012-12-10 12:16:02 -0800 | [diff] [blame] | 196 | *format_ptr++ = type; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 197 | *format_ptr = '\0'; |
| 198 | |
| 199 | // Format using snprintf. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 200 | size_t offset = buffer_.size(); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 201 | for (;;) { |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 202 | size_t size = buffer_.capacity() - offset; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 203 | int n = 0; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 204 | if (width <= 0) { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 205 | n = precision < 0 ? |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 206 | snprintf(&buffer_[offset], size, format, value) : |
| 207 | snprintf(&buffer_[offset], size, format, precision, value); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 208 | } else { |
| 209 | n = precision < 0 ? |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 210 | snprintf(&buffer_[offset], size, format, width, value) : |
| 211 | snprintf(&buffer_[offset], size, format, width, precision, value); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 212 | } |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 213 | if (n >= 0 && offset + n < buffer_.capacity()) { |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 214 | GrowBuffer(n); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 215 | return; |
| 216 | } |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 217 | buffer_.reserve(n >= 0 ? offset + n + 1 : 2 * buffer_.capacity()); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| Victor Zverovich | 57dbd2c | 2012-12-11 12:23:52 -0800 | [diff] [blame] | 221 | void fmt::Formatter::DoFormat() { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 222 | const char *start = format_; |
| Victor Zverovich | 57dbd2c | 2012-12-11 12:23:52 -0800 | [diff] [blame] | 223 | format_ = 0; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 224 | const char *s = start; |
| Victor Zverovich | 5f3ed20 | 2012-12-07 17:48:10 -0800 | [diff] [blame] | 225 | while (*s) { |
| Victor Zverovich | 31a5070 | 2012-12-10 15:04:55 -0800 | [diff] [blame] | 226 | char c = *s++; |
| 227 | if (c != '{' && c != '}') continue; |
| 228 | if (*s == c) { |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 229 | buffer_.append(start, s); |
| Victor Zverovich | 31a5070 | 2012-12-10 15:04:55 -0800 | [diff] [blame] | 230 | start = ++s; |
| 231 | continue; |
| 232 | } |
| 233 | if (c == '}') |
| 234 | throw FormatError("unmatched '}' in format"); |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 235 | buffer_.append(start, s - 1); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 236 | |
| 237 | // Parse argument index. |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 238 | if (*s < '0' || *s > '9') |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 239 | ReportError(s, "missing argument index in format string"); |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 240 | unsigned arg_index = ParseUInt(s); |
| Victor Zverovich | 63539c0 | 2012-12-08 08:17:12 -0800 | [diff] [blame] | 241 | if (arg_index >= args_.size()) |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 242 | ReportError(s, "argument index is out of range in format"); |
| Victor Zverovich | 4db5a66 | 2012-12-11 10:27:13 -0800 | [diff] [blame] | 243 | const Arg &arg = *args_[arg_index]; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 244 | |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 245 | unsigned flags = 0; |
| 246 | int width = 0; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 247 | int precision = -1; |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 248 | char type = 0; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 249 | if (*s == ':') { |
| 250 | ++s; |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 251 | if (*s == '+') { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 252 | ++s; |
| 253 | if (arg.type > LAST_NUMERIC_TYPE) |
| 254 | ReportError(s, "format specifier '+' requires numeric argument"); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 255 | if (arg.type == UINT || arg.type == ULONG) { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 256 | ReportError(s, |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 257 | "format specifier '+' requires signed argument"); |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 258 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 259 | flags |= PLUS_FLAG; |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 260 | } |
| 261 | if (*s == '0') { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 262 | ++s; |
| 263 | if (arg.type > LAST_NUMERIC_TYPE) |
| 264 | ReportError(s, "format specifier '0' requires numeric argument"); |
| 265 | flags |= ZERO_FLAG; |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 266 | } |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 267 | |
| 268 | // Parse width. |
| 269 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 270 | unsigned value = ParseUInt(s); |
| 271 | if (value > INT_MAX) |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 272 | ReportError(s, "number is too big in format"); |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 273 | width = value; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Parse precision. |
| 277 | if (*s == '.') { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 278 | ++s; |
| 279 | precision = 0; |
| 280 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 281 | unsigned value = ParseUInt(s); |
| 282 | if (value > INT_MAX) |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 283 | ReportError(s, "number is too big in format"); |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 284 | precision = value; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 285 | } else { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 286 | ReportError(s, "missing precision in format"); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 287 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 288 | if (arg.type != DOUBLE && arg.type != LONG_DOUBLE) { |
| 289 | ReportError(s, |
| 290 | "precision specifier requires floating-point argument"); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | // Parse type. |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 295 | if (*s != '}' && *s) |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 296 | type = *s++; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (*s++ != '}') |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 300 | throw FormatError("unmatched '{' in format"); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 301 | start = s; |
| 302 | |
| 303 | // Format argument. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 304 | switch (arg.type) { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 305 | case INT: |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 306 | FormatInt(arg.int_value, flags, width, type); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 307 | break; |
| 308 | case UINT: |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 309 | FormatInt(arg.uint_value, flags, width, type); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 310 | break; |
| 311 | case LONG: |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 312 | FormatInt(arg.long_value, flags, width, type); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 313 | break; |
| 314 | case ULONG: |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 315 | FormatInt(arg.ulong_value, flags, width, type); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 316 | break; |
| 317 | case DOUBLE: |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 318 | FormatDouble(arg.double_value, flags, width, precision, type); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 319 | break; |
| 320 | case LONG_DOUBLE: |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 321 | FormatDouble(arg.long_double_value, flags, width, precision, type); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 322 | break; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 323 | case CHAR: { |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 324 | if (type && type != 'c') |
| 325 | ReportUnknownType(type, "char"); |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 326 | char *out = GrowBuffer(std::max(width, 1)); |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 327 | *out++ = arg.int_value; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 328 | if (width > 1) |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 329 | std::fill_n(out, width - 1, ' '); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 330 | break; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 331 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 332 | case STRING: { |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 333 | if (type && type != 's') |
| 334 | ReportUnknownType(type, "string"); |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 335 | const char *str = arg.string.value; |
| 336 | size_t size = arg.string.size; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 337 | if (size == 0 && *str) |
| 338 | size = std::strlen(str); |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 339 | char *out = GrowBuffer(std::max<size_t>(width, size)); |
| 340 | out = std::copy(str, str + size, out); |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 341 | if (static_cast<unsigned>(width) > size) |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 342 | std::fill_n(out, width - size, ' '); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 343 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 344 | } |
| 345 | case POINTER: |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 346 | if (type && type != 'p') |
| 347 | ReportUnknownType(type, "pointer"); |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 348 | FormatInt(reinterpret_cast<uintptr_t>( |
| 349 | arg.pointer_value), HEX_PREFIX_FLAG, width, 'x'); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 350 | break; |
| Victor Zverovich | 280ea8b | 2012-12-09 09:03:47 -0800 | [diff] [blame] | 351 | case CUSTOM: |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 352 | if (type) |
| 353 | ReportUnknownType(type, "object"); |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame^] | 354 | (this->*arg.custom.format)(arg.custom.value, width); |
| Victor Zverovich | 63539c0 | 2012-12-08 08:17:12 -0800 | [diff] [blame] | 355 | break; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 356 | default: |
| 357 | assert(false); |
| 358 | break; |
| 359 | } |
| 360 | } |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 361 | buffer_.append(start, s + 1); |
| Victor Zverovich | 0996e98 | 2012-12-10 20:37:35 -0800 | [diff] [blame] | 362 | buffer_.resize(buffer_.size() - 1); // Don't count the terminating zero. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 363 | } |