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