fmt/format.cc

231 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;
}
}
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");
Arg &arg = args_[arg_index];
2012-12-07 16:31:09 +00:00
enum { MAX_FORMAT_SIZE = 10}; // longest format: %+0-*.*ld
2012-12-09 17:03:47 +00:00
char arg_format[MAX_FORMAT_SIZE];
2012-12-07 16:31:09 +00:00
char *arg_format_ptr = arg_format;
*arg_format_ptr++ = '%';
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 == '+') {
if (arg.type > LAST_NUMERIC_TYPE) {
Throw<FormatError>(s,
"format specifier '+' used with non-numeric type");
}
2012-12-07 16:31:09 +00:00
*arg_format_ptr++ = *s++;
2012-12-09 02:45:35 +00:00
}
if (*s == '0') {
if (arg.type > LAST_NUMERIC_TYPE) {
Throw<FormatError>(s,
"format specifier '0' used with non-numeric type");
}
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') {
if (arg.type > LAST_NUMERIC_TYPE)
*arg_format_ptr++ = '-';
2012-12-07 16:31:09 +00:00
*arg_format_ptr++ = '*';
unsigned value = ParseUInt(s);
if (value > INT_MAX)
Throw<FormatError>(s, "number is too big in format");
width = value;
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') {
unsigned value = ParseUInt(s);
if (value > INT_MAX)
Throw<FormatError>(s, "number is too big in format"); // TODO: test
precision = value;
2012-12-07 16:31:09 +00:00
} 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.
switch (arg.type) {
case CHAR:
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:
// TODO: align string left by default
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_.reserve(buffer_.size() + size + 1);
2012-12-09 17:03:47 +00:00
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:
*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:
*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:
(this->*arg.format)(arg.custom_value, width);
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();
}