Add support for user-defined types.
This commit is contained in:
34
format.cc
34
format.cc
@@ -56,7 +56,7 @@ void CheckFlags(unsigned flags) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fmt::Formatter::FormatArg(
|
||||
void fmt::Formatter::FormatBuiltinArg(
|
||||
const char *format, const T &arg, int width, int precision) {
|
||||
size_t offset = buffer_.size();
|
||||
buffer_.resize(buffer_.capacity());
|
||||
@@ -95,7 +95,8 @@ void fmt::Formatter::Format() {
|
||||
if (arg_index >= args_.size())
|
||||
Throw<std::out_of_range>(s, "argument index is out of range in format");
|
||||
|
||||
char arg_format[8]; // longest format: %+0*.*ld
|
||||
enum { MAX_FORMAT_SIZE = 9}; // longest format: %+0*.*ld
|
||||
char arg_format[MAX_FORMAT_SIZE];
|
||||
char *arg_format_ptr = arg_format;
|
||||
*arg_format_ptr++ = '%';
|
||||
|
||||
@@ -158,68 +159,71 @@ void fmt::Formatter::Format() {
|
||||
}
|
||||
*arg_format_ptr++ = 'c';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.int_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.int_value, width, precision);
|
||||
break;
|
||||
case INT:
|
||||
*arg_format_ptr++ = 'd';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.int_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.int_value, width, precision);
|
||||
break;
|
||||
case UINT:
|
||||
*arg_format_ptr++ = 'd';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.uint_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.uint_value, width, precision);
|
||||
break;
|
||||
case LONG:
|
||||
*arg_format_ptr++ = 'l';
|
||||
*arg_format_ptr++ = 'd';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.long_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.long_value, width, precision);
|
||||
break;
|
||||
case ULONG:
|
||||
*arg_format_ptr++ = 'l';
|
||||
*arg_format_ptr++ = 'd';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.ulong_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.ulong_value, width, precision);
|
||||
break;
|
||||
case DOUBLE:
|
||||
*arg_format_ptr++ = type ? type : 'g';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.double_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.double_value, width, precision);
|
||||
break;
|
||||
case LONG_DOUBLE:
|
||||
*arg_format_ptr++ = 'L';
|
||||
*arg_format_ptr++ = 'g';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.long_double_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.long_double_value, width, precision);
|
||||
break;
|
||||
case STRING:
|
||||
CheckFlags(flags);
|
||||
if (width == -1 && precision == -1) {
|
||||
const char *str = arg.string_value;
|
||||
buffer_.insert(buffer_.end(), str, str + std::strlen(str));
|
||||
std::size_t size = arg.size;
|
||||
if (size == 0 && *str)
|
||||
size = std::strlen(str);
|
||||
buffer_.insert(buffer_.end(), str, str + size);
|
||||
break;
|
||||
}
|
||||
*arg_format_ptr++ = 's';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.string_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.string_value, width, precision);
|
||||
break;
|
||||
case WSTRING:
|
||||
CheckFlags(flags);
|
||||
*arg_format_ptr++ = 'l';
|
||||
*arg_format_ptr++ = 's';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.wstring_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.wstring_value, width, precision);
|
||||
break;
|
||||
case POINTER:
|
||||
CheckFlags(flags);
|
||||
*arg_format_ptr++ = 'p';
|
||||
*arg_format_ptr = '\0';
|
||||
FormatArg(arg_format, arg.pointer_value, width, precision);
|
||||
FormatBuiltinArg(arg_format, arg.pointer_value, width, precision);
|
||||
break;
|
||||
case OTHER:
|
||||
case CUSTOM:
|
||||
CheckFlags(flags);
|
||||
(this->*arg.format)(arg.other_value);
|
||||
(this->*arg.format)(arg.custom_value);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
|
||||
Reference in New Issue
Block a user