fmt/format.cc

240 lines
6.5 KiB
C++
Raw Normal View History

2012-12-07 16:31:09 +00:00
/*
Small, safe and fast printf-like formatting library for C++
Author: Victor Zverovich
*/
#include "format.h"
#include <cassert>
#include <climits>
2012-12-07 16:31:09 +00:00
#include <cstring>
#include <algorithm>
using std::size_t;
2012-12-09 02:45:35 +00:00
namespace {
// Throws Exception(message) if format contains '}', otherwise throws
// FormatError reporting unmatched '{'. The idea is that unmatched '{'
// should override other errors.
template <typename Exception>
2012-12-09 02:45:35 +00:00
void Throw(const char *s, const char *message) {
while (*s && *s != '}')
++s;
if (!*s)
throw fmt::FormatError("unmatched '{' in format");
throw Exception(message);
}
2012-12-09 02:45:35 +00:00
// Parses an unsigned integer advancing s to the end of the parsed input.
// This function assumes that the first character of s is a digit.
unsigned ParseUInt(const char *&s) {
assert('0' <= *s && *s <= '9');
unsigned value = 0;
do {
unsigned new_value = value * 10 + (*s++ - '0');
if (new_value < value) // Check if value wrapped around.
Throw<fmt::FormatError>(s, "number is too big in format");
value = new_value;
} while ('0' <= *s && *s <= '9');
return value;
}
// Flags.
enum {
PLUS_FLAG = 1,
ZERO_FLAG = 2
};
void CheckFlags(unsigned flags) {
if (flags == 0) return;
if ((flags & PLUS_FLAG) != 0)
throw fmt::FormatError("format specifier '+' used with non-numeric type");
if ((flags & ZERO_FLAG) != 0)
throw fmt::FormatError("format specifier '0' used with non-numeric type");
}
}
2012-12-07 16:31:09 +00:00
template <typename T>
2012-12-09 17:03:47 +00:00
void fmt::Formatter::FormatBuiltinArg(
2012-12-07 16:31:09 +00:00
const char *format, const T &arg, int width, int precision) {
size_t offset = buffer_.size();
buffer_.resize(buffer_.capacity());
for (;;) {
size_t size = buffer_.size() - offset;
int n = 0;
if (width < 0) {
n = precision < 0 ?
snprintf(&buffer_[offset], size, format, arg) :
snprintf(&buffer_[offset], size, format, precision, arg);
} else {
n = precision < 0 ?
snprintf(&buffer_[offset], size, format, width, arg) :
snprintf(&buffer_[offset], size, format, width, precision, arg);
}
if (n >= 0 && offset + n < buffer_.size()) {
buffer_.resize(offset + n);
return;
}
buffer_.resize(n >= 0 ? offset + n + 1 : 2 * buffer_.size());
}
}
void fmt::Formatter::Format() {
buffer_.reserve(500);
const char *start = format_;
const char *s = start;
while (*s) {
if (*s++ != '{') continue;
buffer_.insert(buffer_.end(), start, s - 1);
2012-12-07 16:31:09 +00:00
// Parse argument index.
2012-12-09 02:45:35 +00:00
if (*s < '0' || *s > '9')
Throw<fmt::FormatError>(s, "missing argument index in format string");
unsigned arg_index = ParseUInt(s);
if (arg_index >= args_.size())
Throw<std::out_of_range>(s, "argument index is out of range in format");
2012-12-07 16:31:09 +00:00
2012-12-09 17:03:47 +00:00
enum { MAX_FORMAT_SIZE = 9}; // longest format: %+0*.*ld
char arg_format[MAX_FORMAT_SIZE];
2012-12-07 16:31:09 +00:00
char *arg_format_ptr = arg_format;
*arg_format_ptr++ = '%';
2012-12-09 02:45:35 +00:00
unsigned flags = 0;
2012-12-07 16:31:09 +00:00
int width = -1;
int precision = -1;
2012-12-09 02:45:35 +00:00
char type = 0;
2012-12-07 16:31:09 +00:00
if (*s == ':') {
++s;
2012-12-09 02:45:35 +00:00
if (*s == '+') {
flags |= PLUS_FLAG;
2012-12-07 16:31:09 +00:00
*arg_format_ptr++ = *s++;
2012-12-09 02:45:35 +00:00
}
if (*s == '0') {
flags |= ZERO_FLAG;
2012-12-07 16:31:09 +00:00
*arg_format_ptr++ = *s++;
2012-12-09 02:45:35 +00:00
}
2012-12-07 16:31:09 +00:00
// Parse width.
if ('0' <= *s && *s <= '9') {
*arg_format_ptr++ = '*';
2012-12-09 02:45:35 +00:00
unsigned number = ParseUInt(s);
if (number > INT_MAX) ; // TODO: error
width = number;
2012-12-07 16:31:09 +00:00
}
// Parse precision.
if (*s == '.') {
*arg_format_ptr++ = '.';
*arg_format_ptr++ = '*';
++s;
precision = 0;
if ('0' <= *s && *s <= '9') {
do {
// TODO: check overflow
2012-12-07 16:31:09 +00:00
precision = precision * 10 + (*s++ - '0');
} while ('0' <= *s && *s <= '9');
} else {
// TODO: error
}
}
// Parse type.
if (*s == 'f' || *s == 'g')
type = *s++; // TODO: check if the type matches
}
if (*s++ != '}')
2012-12-09 02:45:35 +00:00
throw FormatError("unmatched '{' in format");
2012-12-07 16:31:09 +00:00
start = s;
// Format argument.
Arg &arg = args_[arg_index];
switch (arg.type) {
case CHAR:
2012-12-09 02:45:35 +00:00
CheckFlags(flags);
2012-12-07 16:31:09 +00:00
if (width == -1 && precision == -1) {
buffer_.push_back(arg.int_value);
break;
}
*arg_format_ptr++ = 'c';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.int_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case INT:
*arg_format_ptr++ = 'd';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.int_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case UINT:
*arg_format_ptr++ = 'd';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.uint_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case LONG:
*arg_format_ptr++ = 'l';
*arg_format_ptr++ = 'd';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.long_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case ULONG:
*arg_format_ptr++ = 'l';
*arg_format_ptr++ = 'd';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.ulong_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case DOUBLE:
*arg_format_ptr++ = type ? type : 'g';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.double_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case LONG_DOUBLE:
2012-12-09 02:45:35 +00:00
*arg_format_ptr++ = 'L';
2012-12-07 16:31:09 +00:00
*arg_format_ptr++ = 'g';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.long_double_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case STRING:
2012-12-09 02:45:35 +00:00
CheckFlags(flags);
2012-12-07 16:31:09 +00:00
if (width == -1 && precision == -1) {
const char *str = arg.string_value;
2012-12-09 17:03:47 +00:00
std::size_t size = arg.size;
if (size == 0 && *str)
size = std::strlen(str);
buffer_.insert(buffer_.end(), str, str + size);
2012-12-07 16:31:09 +00:00
break;
}
*arg_format_ptr++ = 's';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.string_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case WSTRING:
2012-12-09 02:45:35 +00:00
CheckFlags(flags);
2012-12-07 16:31:09 +00:00
*arg_format_ptr++ = 'l';
*arg_format_ptr++ = 's';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.wstring_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
case POINTER:
2012-12-09 02:45:35 +00:00
CheckFlags(flags);
2012-12-07 16:31:09 +00:00
*arg_format_ptr++ = 'p';
*arg_format_ptr = '\0';
2012-12-09 17:03:47 +00:00
FormatBuiltinArg(arg_format, arg.pointer_value, width, precision);
2012-12-07 16:31:09 +00:00
break;
2012-12-09 17:03:47 +00:00
case CUSTOM:
2012-12-09 02:45:35 +00:00
CheckFlags(flags);
2012-12-09 17:03:47 +00:00
(this->*arg.format)(arg.custom_value);
break;
2012-12-07 16:31:09 +00:00
default:
assert(false);
break;
}
}
buffer_.insert(buffer_.end(), start, s + 1);
}
fmt::ArgFormatter::~ArgFormatter() {
if (!formatter_) return;
FinishFormatting();
}