Move StringValue to Arg & simplify StringTraits.

This commit is contained in:
Victor Zverovich 2014-07-14 08:04:17 -07:00
parent 8a66bb89b2
commit e3a2ac8725
3 changed files with 23 additions and 23 deletions

View File

@ -169,15 +169,19 @@ const Char *find_closing_brace(const Char *s, int num_open_braces = 1) {
throw fmt::FormatError("unmatched '{' in format");
}
// Handles width specifier.
// Checks if an argument is a valid printf width specifier and sets
// left alignment if it is negative.
struct WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, ULongLong> {
private:
fmt::FormatSpec &spec_;
public:
explicit WidthHandler(fmt::FormatSpec &spec) : spec_(spec) {}
ULongLong visit_unhandled_arg() {
throw fmt::FormatError("width is not integer");
}
ULongLong visit_any_int(fmt::LongLong value) {
ULongLong width = value;
if (value < 0) {
@ -186,6 +190,7 @@ struct WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, ULongLong>
}
return width;
}
ULongLong visit_any_uint(ULongLong value) { return value; }
};
} // namespace
@ -565,7 +570,9 @@ void fmt::BasicWriter<Char>::FormatDouble(T value, const FormatSpec &spec) {
template <typename Char>
template <typename StringChar>
void fmt::BasicWriter<Char>::write_str(
const internal::StringValue<StringChar> &str, const FormatSpec &spec) {
const internal::Arg::StringValue<StringChar> &str, const FormatSpec &spec) {
// Check if StringChar is convertible to Char.
internal::CharTraits<Char>::convert(StringChar());
if (spec.type_ && spec.type_ != 's')
internal::ReportUnknownType(spec.type_, "string");
const StringChar *s = str.value;
@ -840,7 +847,7 @@ void fmt::internal::PrintfParser<Char>::Format(
writer.write_str(arg.string, spec);
break;
case Arg::WSTRING:
writer.write_str(internal::CharTraits<Char>::convert(arg.wstring), spec);
writer.write_str(arg.wstring, spec);
break;
case Arg::POINTER:
if (spec.type_ && spec.type_ != 'p')
@ -1048,7 +1055,7 @@ const Char *fmt::BasicFormatter<Char>::format(
writer_.write_str(arg.string, spec);
break;
case Arg::WSTRING:
writer_.write_str(internal::CharTraits<Char>::convert(arg.wstring), spec);
writer_.write_str(arg.wstring, spec);
break;
case Arg::POINTER:
if (spec.type_ && spec.type_ != 'p')

View File

@ -340,12 +340,6 @@ void Array<T, SIZE>::append(const T *begin, const T *end) {
size_ += num_elements;
}
template <typename Char>
struct StringValue {
const Char *value;
std::size_t size;
};
template <typename Char>
class BasicCharTraits {
public:
@ -373,11 +367,6 @@ public:
static char convert(char value) { return value; }
static StringValue<char> convert(StringValue<wchar_t>) {
StringValue<char> s = {"", 0};
return s;
}
template <typename T>
static int FormatFloat(char *buffer, std::size_t size,
const char *format, unsigned width, int precision, T value);
@ -391,8 +380,6 @@ class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> {
static wchar_t convert(char value) { return value; }
static wchar_t convert(wchar_t value) { return value; }
static StringValue<wchar_t> convert(StringValue<wchar_t> s) { return s; }
static const wchar_t *check(const wchar_t *s) { return s; }
template <typename T>
@ -600,6 +587,12 @@ struct Arg {
};
Type type;
template <typename Char>
struct StringValue {
const Char *value;
std::size_t size;
};
typedef void (*FormatFunc)(
void *formatter, const void *arg, const void *format_str);
@ -773,10 +766,10 @@ class ArgVisitor {
Result visit_char(int) {
return FMT_DISPATCH(visit_unhandled_arg());
}
Result visit_string(StringValue<char>) {
Result visit_string(Arg::StringValue<char>) {
return FMT_DISPATCH(visit_unhandled_arg());
}
Result visit_wstring(StringValue<wchar_t>) {
Result visit_wstring(Arg::StringValue<wchar_t>) {
return FMT_DISPATCH(visit_unhandled_arg());
}
Result visit_pointer(const void *) {
@ -1347,7 +1340,7 @@ class BasicWriter {
template <typename StringChar>
void write_str(
const internal::StringValue<StringChar> &str, const FormatSpec &spec);
const internal::Arg::StringValue<StringChar> &str, const FormatSpec &spec);
// This method is private to disallow writing a wide string to a
// char stream and vice versa. If you want to print a wide string

View File

@ -254,10 +254,10 @@ struct TestVisitor : fmt::internal::ArgVisitor<TestVisitor, Result> {
Result visit_double(double value) { return value; }
Result visit_long_double(long double value) { return value; }
Result visit_char(int value) { return static_cast<char>(value); }
Result visit_string(fmt::internal::StringValue<char> s) { return s.value; }
Result visit_wstring(fmt::internal::StringValue<wchar_t> s) { return s.value; }
Result visit_string(Arg::StringValue<char> s) { return s.value; }
Result visit_wstring(Arg::StringValue<wchar_t> s) { return s.value; }
Result visit_pointer(const void *p) { return p; }
Result visit_custom(fmt::internal::Arg::CustomValue c) {
Result visit_custom(Arg::CustomValue c) {
return *static_cast<const ::Test*>(c.value);
}
};