separated unsigned long long support

put the better error message on a separate branch
This commit is contained in:
Gregory Czajkowski 2013-11-20 13:44:10 -08:00
parent 62466cc3d9
commit 5dd604b481
2 changed files with 230 additions and 249 deletions

465
format.cc
View File

@ -160,6 +160,12 @@ void fmt::BasicWriter<Char>::FormatDecimal(
buffer[0] = internal::DIGITS[index];
}
template <typename Char>
void fmt::BasicWriter<Char>::FormatDecimal(
CharPtr buffer, unsigned long long value, unsigned num_digits) {
return fmt::BasicWriter<Char>::FormatDecimal(buffer, static_cast<uint64_t>(value), num_digits);
}
template <typename Char>
typename fmt::BasicWriter<Char>::CharPtr
fmt::BasicWriter<Char>::PrepareFilledBuffer(
@ -409,248 +415,241 @@ void fmt::BasicFormatter<Char>::CheckSign(const Char *&s, const Arg &arg) {
template <typename Char>
void fmt::BasicFormatter<Char>::DoFormat() {
const Char *start = format_; // capture the format string before it is reset
const Char *original = format_;
const Char *start = format_;
format_ = 0;
next_arg_index_ = 0;
const Char *s = start;
typedef internal::Array<Char, BasicWriter<Char>::INLINE_BUFFER_SIZE> Buffer;
BasicWriter<Char> &writer = *writer_;
try {
while (*s) {
Char c = *s++;
if (c != '{' && c != '}') continue;
if (*s == c) {
writer.buffer_.append(start, s);
start = ++s;
continue;
}
if (c == '}')
throw FormatError("unmatched '}' in format");
num_open_braces_= 1;
writer.buffer_.append(start, s - 1);
while (*s) {
Char c = *s++;
if (c != '{' && c != '}') continue;
if (*s == c) {
writer.buffer_.append(start, s);
start = ++s;
continue;
}
if (c == '}')
throw FormatError("unmatched '}' in format");
num_open_braces_= 1;
writer.buffer_.append(start, s - 1);
const Arg &arg = ParseArgIndex(s);
const Arg &arg = ParseArgIndex(s);
FormatSpec spec;
int precision = -1;
if (*s == ':') {
++s;
FormatSpec spec;
int precision = -1;
if (*s == ':') {
++s;
// Parse fill and alignment.
if (Char c = *s) {
const Char *p = s + 1;
spec.align_ = ALIGN_DEFAULT;
do {
switch (*p) {
case '<':
spec.align_ = ALIGN_LEFT;
break;
case '>':
spec.align_ = ALIGN_RIGHT;
break;
case '=':
spec.align_ = ALIGN_NUMERIC;
break;
case '^':
spec.align_ = ALIGN_CENTER;
break;
}
if (spec.align_ != ALIGN_DEFAULT) {
if (p != s) {
if (c == '}') break;
if (c == '{')
ReportError(s, "invalid fill character '{'");
s += 2;
spec.fill_ = c;
} else ++s;
if (spec.align_ == ALIGN_NUMERIC && arg.type > LAST_NUMERIC_TYPE)
ReportError(s, "format specifier '=' requires numeric argument");
break;
}
} while (--p >= s);
}
// Parse sign.
switch (*s) {
case '+':
CheckSign(s, arg);
spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
break;
case '-':
CheckSign(s, arg);
break;
case ' ':
CheckSign(s, arg);
spec.flags_ |= SIGN_FLAG;
break;
}
if (*s == '#') {
if (arg.type > LAST_NUMERIC_TYPE)
ReportError(s, "format specifier '#' requires numeric argument");
spec.flags_ |= HASH_FLAG;
++s;
}
// Parse width and zero flag.
if ('0' <= *s && *s <= '9') {
if (*s == '0') {
if (arg.type > LAST_NUMERIC_TYPE)
ReportError(s, "format specifier '0' requires numeric argument");
// Parse fill and alignment.
if (Char c = *s) {
const Char *p = s + 1;
spec.align_ = ALIGN_DEFAULT;
do {
switch (*p) {
case '<':
spec.align_ = ALIGN_LEFT;
break;
case '>':
spec.align_ = ALIGN_RIGHT;
break;
case '=':
spec.align_ = ALIGN_NUMERIC;
spec.fill_ = '0';
break;
case '^':
spec.align_ = ALIGN_CENTER;
break;
}
// Zero may be parsed again as a part of the width, but it is simpler
// and more efficient than checking if the next char is a digit.
if (spec.align_ != ALIGN_DEFAULT) {
if (p != s) {
if (c == '}') break;
if (c == '{')
ReportError(s, "invalid fill character '{'");
s += 2;
spec.fill_ = c;
} else ++s;
if (spec.align_ == ALIGN_NUMERIC && arg.type > LAST_NUMERIC_TYPE)
ReportError(s, "format specifier '=' requires numeric argument");
break;
}
} while (--p >= s);
}
// Parse sign.
switch (*s) {
case '+':
CheckSign(s, arg);
spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
break;
case '-':
CheckSign(s, arg);
break;
case ' ':
CheckSign(s, arg);
spec.flags_ |= SIGN_FLAG;
break;
}
if (*s == '#') {
if (arg.type > LAST_NUMERIC_TYPE)
ReportError(s, "format specifier '#' requires numeric argument");
spec.flags_ |= HASH_FLAG;
++s;
}
// Parse width and zero flag.
if ('0' <= *s && *s <= '9') {
if (*s == '0') {
if (arg.type > LAST_NUMERIC_TYPE)
ReportError(s, "format specifier '0' requires numeric argument");
spec.align_ = ALIGN_NUMERIC;
spec.fill_ = '0';
}
// Zero may be parsed again as a part of the width, but it is simpler
// and more efficient than checking if the next char is a digit.
unsigned value = ParseUInt(s);
if (value > INT_MAX)
ReportError(s, "number is too big in format");
spec.width_ = value;
}
// Parse precision.
if (*s == '.') {
++s;
precision = 0;
if ('0' <= *s && *s <= '9') {
unsigned value = ParseUInt(s);
if (value > INT_MAX)
ReportError(s, "number is too big in format");
spec.width_ = value;
}
// Parse precision.
if (*s == '.') {
precision = value;
} else if (*s == '{') {
++s;
precision = 0;
if ('0' <= *s && *s <= '9') {
unsigned value = ParseUInt(s);
if (value > INT_MAX)
ReportError(s, "number is too big in format");
precision = value;
} else if (*s == '{') {
++s;
++num_open_braces_;
const Arg &precision_arg = ParseArgIndex(s);
unsigned long long value = 0;
switch (precision_arg.type) {
case INT:
if (precision_arg.int_value < 0)
ReportError(s, "negative precision in format");
value = precision_arg.int_value;
break;
case UINT:
value = precision_arg.uint_value;
break;
case LONG:
if (precision_arg.long_value < 0)
ReportError(s, "negative precision in format");
value = precision_arg.long_value;
break;
case ULONG:
value = precision_arg.ulong_value;
break;
case ULLONG:
value = precision_arg.ulong_long_value;
break;
default:
ReportError(s, "precision is not integer");
}
if (value > INT_MAX)
ReportError(s, "number is too big in format");
precision = static_cast<int>(value);
if (*s++ != '}')
throw FormatError("unmatched '{' in format");
--num_open_braces_;
} else {
ReportError(s, "missing precision in format");
}
if (arg.type != DOUBLE && arg.type != LONG_DOUBLE) {
ReportError(s,
"precision specifier requires floating-point argument");
}
}
// Parse type.
if (*s != '}' && *s)
spec.type_ = static_cast<char>(*s++);
}
if (*s++ != '}')
throw FormatError("unmatched '{' in format");
start = s;
// Format argument.
switch (arg.type) {
case INT:
writer.FormatInt(arg.int_value, spec);
break;
case UINT:
writer.FormatInt(arg.uint_value, spec);
break;
case LONG:
writer.FormatInt(arg.long_value, spec);
break;
case ULONG:
writer.FormatInt(arg.ulong_value, spec);
break;
case ULLONG:
writer.FormatInt(arg.ulong_long_value, spec);
break;
case DOUBLE:
writer.FormatDouble(arg.double_value, spec, precision);
break;
case LONG_DOUBLE:
writer.FormatDouble(arg.long_double_value, spec, precision);
break;
case CHAR: {
if (spec.type_ && spec.type_ != 'c')
internal::ReportUnknownType(spec.type_, "char");
typedef typename BasicWriter<Char>::CharPtr CharPtr;
CharPtr out = CharPtr();
if (spec.width_ > 1) {
Char fill = static_cast<Char>(spec.fill());
out = writer.GrowBuffer(spec.width_);
if (spec.align_ == ALIGN_RIGHT) {
std::fill_n(out, spec.width_ - 1, fill);
out += spec.width_ - 1;
} else if (spec.align_ == ALIGN_CENTER) {
out = writer.FillPadding(out, spec.width_, 1, fill);
} else {
std::fill_n(out + 1, spec.width_ - 1, fill);
++num_open_braces_;
const Arg &precision_arg = ParseArgIndex(s);
unsigned long long value = 0;
switch (precision_arg.type) {
case INT:
if (precision_arg.int_value < 0)
ReportError(s, "negative precision in format");
value = precision_arg.int_value;
break;
case UINT:
value = precision_arg.uint_value;
break;
case LONG:
if (precision_arg.long_value < 0)
ReportError(s, "negative precision in format");
value = precision_arg.long_value;
break;
case ULONG:
value = precision_arg.ulong_value;
break;
case ULLONG:
value = precision_arg.ulong_long_value;
break;
default:
ReportError(s, "precision is not integer");
}
if (value > INT_MAX)
ReportError(s, "number is too big in format");
precision = value;
if (*s++ != '}')
throw FormatError("unmatched '{' in format");
--num_open_braces_;
} else {
out = writer.GrowBuffer(1);
ReportError(s, "missing precision in format");
}
*out = arg.int_value;
break;
}
case STRING: {
if (spec.type_ && spec.type_ != 's')
internal::ReportUnknownType(spec.type_, "string");
const Char *str = arg.string.value;
std::size_t size = arg.string.size;
if (size == 0) {
if (!str)
throw FormatError("string pointer is null");
if (*str)
size = std::char_traits<Char>::length(str);
if (arg.type != DOUBLE && arg.type != LONG_DOUBLE) {
ReportError(s,
"precision specifier requires floating-point argument");
}
writer.FormatString(str, size, spec);
break;
}
case POINTER:
if (spec.type_ && spec.type_ != 'p')
internal::ReportUnknownType(spec.type_, "pointer");
spec.flags_= HASH_FLAG;
spec.type_ = 'x';
writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
break;
case CUSTOM:
if (spec.type_)
internal::ReportUnknownType(spec.type_, "object");
arg.custom.format(writer, arg.custom.value, spec);
break;
default:
assert(false);
break;
// Parse type.
if (*s != '}' && *s)
spec.type_ = static_cast<char>(*s++);
}
if (*s++ != '}')
throw FormatError("unmatched '{' in format");
start = s;
// Format argument.
switch (arg.type) {
case INT:
writer.FormatInt(arg.int_value, spec);
break;
case UINT:
writer.FormatInt(arg.uint_value, spec);
break;
case LONG:
writer.FormatInt(arg.long_value, spec);
break;
case ULONG:
writer.FormatInt(arg.ulong_value, spec);
break;
case ULLONG:
writer.FormatInt(arg.ulong_long_value, spec);
break;
case DOUBLE:
writer.FormatDouble(arg.double_value, spec, precision);
break;
case LONG_DOUBLE:
writer.FormatDouble(arg.long_double_value, spec, precision);
break;
case CHAR: {
if (spec.type_ && spec.type_ != 'c')
internal::ReportUnknownType(spec.type_, "char");
typedef typename BasicWriter<Char>::CharPtr CharPtr;
CharPtr out = CharPtr();
if (spec.width_ > 1) {
Char fill = static_cast<Char>(spec.fill());
out = writer.GrowBuffer(spec.width_);
if (spec.align_ == ALIGN_RIGHT) {
std::fill_n(out, spec.width_ - 1, fill);
out += spec.width_ - 1;
} else if (spec.align_ == ALIGN_CENTER) {
out = writer.FillPadding(out, spec.width_, 1, fill);
} else {
std::fill_n(out + 1, spec.width_ - 1, fill);
}
} else {
out = writer.GrowBuffer(1);
}
}
} catch (const FormatError &e) {
// rethrow FormatError with the format string pointed to by start
throw BasicFormatError<Char>(e.what(), original);
*out = arg.int_value;
break;
}
case STRING: {
if (spec.type_ && spec.type_ != 's')
internal::ReportUnknownType(spec.type_, "string");
const Char *str = arg.string.value;
std::size_t size = arg.string.size;
if (size == 0) {
if (!str)
throw FormatError("string pointer is null");
if (*str)
size = std::char_traits<Char>::length(str);
}
writer.FormatString(str, size, spec);
break;
}
case POINTER:
if (spec.type_ && spec.type_ != 'p')
internal::ReportUnknownType(spec.type_, "pointer");
spec.flags_= HASH_FLAG;
spec.type_ = 'x';
writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
break;
case CUSTOM:
if (spec.type_)
internal::ReportUnknownType(spec.type_, "object");
arg.custom.format(writer, arg.custom.value, spec);
break;
default:
assert(false);
break;
}
}
writer.buffer_.append(start, s);
}
@ -669,6 +668,9 @@ template fmt::BasicWriter<char>::CharPtr
template void fmt::BasicWriter<char>::FormatDecimal(
CharPtr buffer, uint64_t value, unsigned num_digits);
template void fmt::BasicWriter<char>::FormatDecimal(
CharPtr buffer, unsigned long long value, unsigned num_digits);
template fmt::BasicWriter<char>::CharPtr
fmt::BasicWriter<char>::PrepareFilledBuffer(
unsigned size, const AlignSpec &spec, char sign);
@ -686,17 +688,6 @@ template void fmt::BasicFormatter<char>::CheckSign(
template void fmt::BasicFormatter<char>::DoFormat();
const char* FormatErrorMessage(const std::string &message, const char* format) {
return fmt::c_str(fmt::Format("error: {} while parsing {}") << message << format);
}
template<> fmt::BasicFormatError<char>::BasicFormatError(const std::string &message, const char *format)
: std::runtime_error(FormatErrorMessage(message, format)), format_(format) {}
template<> fmt::BasicFormatError<char>::~BasicFormatError() {
std::runtime_error::~runtime_error();
}
// Explicit instantiations for wchar_t.
template void fmt::BasicWriter<wchar_t>::FormatDouble<double>(
@ -712,6 +703,9 @@ template fmt::BasicWriter<wchar_t>::CharPtr
template void fmt::BasicWriter<wchar_t>::FormatDecimal(
CharPtr buffer, uint64_t value, unsigned num_digits);
template void fmt::BasicWriter<wchar_t>::FormatDecimal(
CharPtr buffer, unsigned long long value, unsigned num_digits);
template fmt::BasicWriter<wchar_t>::CharPtr
fmt::BasicWriter<wchar_t>::PrepareFilledBuffer(
unsigned size, const AlignSpec &spec, char sign);
@ -729,10 +723,3 @@ template void fmt::BasicFormatter<wchar_t>::CheckSign(
const wchar_t *&s, const Arg &arg);
template void fmt::BasicFormatter<wchar_t>::DoFormat();
template<> fmt::BasicFormatError<wchar_t>::BasicFormatError(const std::string &message, const wchar_t *format)
: std::runtime_error(message), format_(format){}
template<> fmt::BasicFormatError<wchar_t>::~BasicFormatError() {
std::runtime_error::~runtime_error();
}

View File

@ -292,16 +292,6 @@ class FormatError : public std::runtime_error {
: std::runtime_error(message) {}
};
template <typename Char>
class BasicFormatError : public std::runtime_error {
private:
std::basic_string<Char> format_;
public:
explicit BasicFormatError(const std::string &message, const Char *format);
virtual ~BasicFormatError() throw();
const Char *format() const { return format_.c_str(); }
};
enum Alignment {
ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
};
@ -521,6 +511,9 @@ class BasicWriter {
static void FormatDecimal(
CharPtr buffer, uint64_t value, unsigned num_digits);
static void FormatDecimal(
CharPtr buffer, unsigned long long value, unsigned num_digits);
static CharPtr FillPadding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill);
@ -1203,6 +1196,7 @@ class FormatInt {
}
explicit FormatInt(unsigned value) : str_(FormatDecimal(value)) {}
explicit FormatInt(uint64_t value) : str_(FormatDecimal(value)) {}
explicit FormatInt(unsigned long long value) : str_(FormatDecimal(value)) {}
inline const char *c_str() const { return str_; }
inline std::string str() const { return str_; }