Move FormatParser to the internal namespace.

This commit is contained in:
Victor Zverovich 2014-07-08 16:20:33 -07:00
parent f711266244
commit e825156add
4 changed files with 194 additions and 171 deletions

278
format.cc
View File

@ -154,6 +154,19 @@ int ParseNonnegativeInt(const Char *&s, const char *&error) FMT_NOEXCEPT(true) {
} }
return value; return value;
} }
template <typename Char>
const Char *find_closing_brace(const Char *s, int num_open_braces = 1) {
for (int n = num_open_braces; *s; ++s) {
if (*s == '{') {
++n;
} else if (*s == '}') {
if (--n == 0)
return s;
}
}
throw fmt::FormatError("unmatched '{' in format");
}
} // namespace } // namespace
int fmt::internal::SignBitNoInline(double value) { return SignBit(value); } int fmt::internal::SignBitNoInline(double value) { return SignBit(value); }
@ -357,15 +370,8 @@ void fmt::internal::FormatWinErrorMessage(
template <typename Char> template <typename Char>
void fmt::internal::FormatErrorReporter<Char>::operator()( void fmt::internal::FormatErrorReporter<Char>::operator()(
const Char *s, fmt::StringRef message) const { const Char *s, fmt::StringRef message) const {
for (int n = num_open_braces; *s; ++s) { if (find_closing_brace(s, num_open_braces))
if (*s == '{') { throw fmt::FormatError(message);
++n;
} else if (*s == '}') {
if (--n == 0)
throw fmt::FormatError(message);
}
}
throw fmt::FormatError("unmatched '{' in format");
} }
// Fills the padding around the content and returns the pointer to the // Fills the padding around the content and returns the pointer to the
@ -553,7 +559,7 @@ void fmt::BasicWriter<Char>::write_str(
template <typename Char> template <typename Char>
inline const Arg inline const Arg
&fmt::BasicWriter<Char>::FormatParser::ParseArgIndex(const Char *&s) { &fmt::internal::FormatParser<Char>::ParseArgIndex(const Char *&s) {
unsigned arg_index = 0; unsigned arg_index = 0;
if (*s < '0' || *s > '9') { if (*s < '0' || *s > '9') {
if (*s != '}' && *s != ':') if (*s != '}' && *s != ':')
@ -580,7 +586,7 @@ inline const Arg
} }
template <typename Char> template <typename Char>
void fmt::BasicWriter<Char>::FormatParser::CheckSign( void fmt::internal::FormatParser<Char>::CheckSign(
const Char *&s, const Arg &arg) { const Char *&s, const Arg &arg) {
char sign = static_cast<char>(*s); char sign = static_cast<char>(*s);
if (arg.type > Arg::LAST_NUMERIC_TYPE) { if (arg.type > Arg::LAST_NUMERIC_TYPE) {
@ -851,7 +857,7 @@ void fmt::internal::PrintfParser<Char>::Format(
case Arg::CUSTOM: case Arg::CUSTOM:
if (spec.type_) if (spec.type_)
internal::ReportUnknownType(spec.type_, "object"); internal::ReportUnknownType(spec.type_, "object");
arg.custom.format(&writer, arg.custom.value, spec); arg.custom.format(&writer, arg.custom.value, "s");
break; break;
default: default:
assert(false); assert(false);
@ -862,39 +868,23 @@ void fmt::internal::PrintfParser<Char>::Format(
} }
template <typename Char> template <typename Char>
void fmt::BasicWriter<Char>::FormatParser::Format( const Char *fmt::internal::FormatParser<Char>::format(
BasicWriter<Char> &writer, BasicStringRef<Char> format, const Char *format_str, const internal::Arg &arg) {
const ArgList &args) { const Char *s = format_str;
const char *error = 0; const char *error = 0;
const Char *start = format.c_str(); FormatSpec spec;
args_ = args; if (*s == ':') {
next_arg_index_ = 0; if (arg.type == Arg::CUSTOM) {
const Char *s = start; arg.custom.format(this, arg.custom.value, s);
while (*s) { return find_closing_brace(s) + 1;
Char c = *s++;
if (c != '{' && c != '}') continue;
if (*s == c) {
writer.buffer_.append(start, s);
start = ++s;
continue;
} }
if (c == '}') ++s;
throw FormatError("unmatched '}' in format"); // Parse fill and alignment.
report_error_.num_open_braces = 1; if (Char c = *s) {
writer.buffer_.append(start, s - 1); const Char *p = s + 1;
spec.align_ = ALIGN_DEFAULT;
const Arg &arg = ParseArgIndex(s); do {
switch (*p) {
FormatSpec spec;
if (*s == ':') {
++s;
// Parse fill and alignment.
if (Char c = *s) {
const Char *p = s + 1;
spec.align_ = ALIGN_DEFAULT;
do {
switch (*p) {
case '<': case '<':
spec.align_ = ALIGN_LEFT; spec.align_ = ALIGN_LEFT;
break; break;
@ -907,24 +897,24 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
case '^': case '^':
spec.align_ = ALIGN_CENTER; spec.align_ = ALIGN_CENTER;
break; break;
} }
if (spec.align_ != ALIGN_DEFAULT) { if (spec.align_ != ALIGN_DEFAULT) {
if (p != s) { if (p != s) {
if (c == '}') break; if (c == '}') break;
if (c == '{') if (c == '{')
report_error_(s, "invalid fill character '{'"); report_error_(s, "invalid fill character '{'");
s += 2; s += 2;
spec.fill_ = c; spec.fill_ = c;
} else ++s; } else ++s;
if (spec.align_ == ALIGN_NUMERIC && arg.type > Arg::LAST_NUMERIC_TYPE) if (spec.align_ == ALIGN_NUMERIC && arg.type > Arg::LAST_NUMERIC_TYPE)
report_error_(s, "format specifier '=' requires numeric argument"); report_error_(s, "format specifier '=' requires numeric argument");
break; break;
} }
} while (--p >= s); } while (--p >= s);
} }
// Parse sign. // Parse sign.
switch (*s) { switch (*s) {
case '+': case '+':
CheckSign(s, arg); CheckSign(s, arg);
spec.flags_ |= SIGN_FLAG | PLUS_FLAG; spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
@ -936,44 +926,44 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
CheckSign(s, arg); CheckSign(s, arg);
spec.flags_ |= SIGN_FLAG; spec.flags_ |= SIGN_FLAG;
break; break;
} }
if (*s == '#') { if (*s == '#') {
if (arg.type > Arg::LAST_NUMERIC_TYPE)
report_error_(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 > Arg::LAST_NUMERIC_TYPE) if (arg.type > Arg::LAST_NUMERIC_TYPE)
report_error_(s, "format specifier '#' requires numeric argument"); report_error_(s, "format specifier '0' requires numeric argument");
spec.flags_ |= HASH_FLAG; spec.align_ = ALIGN_NUMERIC;
++s; 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.
spec.width_ = ParseNonnegativeInt(s, error);
if (error)
report_error_(s, error);
}
// Parse width and zero flag. // Parse precision.
if (*s == '.') {
++s;
spec.precision_ = 0;
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9') {
if (*s == '0') { spec.precision_ = ParseNonnegativeInt(s, error);
if (arg.type > Arg::LAST_NUMERIC_TYPE)
report_error_(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.
spec.width_ = ParseNonnegativeInt(s, error);
if (error) if (error)
report_error_(s, error); report_error_(s, error);
} } else if (*s == '{') {
// Parse precision.
if (*s == '.') {
++s; ++s;
spec.precision_ = 0; ++report_error_.num_open_braces;
if ('0' <= *s && *s <= '9') { const Arg &precision_arg = ParseArgIndex(s);
spec.precision_ = ParseNonnegativeInt(s, error); ULongLong value = 0;
if (error) switch (precision_arg.type) {
report_error_(s, error);
} else if (*s == '{') {
++s;
++report_error_.num_open_braces;
const Arg &precision_arg = ParseArgIndex(s);
ULongLong value = 0;
switch (precision_arg.type) {
case Arg::INT: case Arg::INT:
if (precision_arg.int_value < 0) if (precision_arg.int_value < 0)
report_error_(s, "negative precision in format"); report_error_(s, "negative precision in format");
@ -992,50 +982,50 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
break; break;
default: default:
report_error_(s, "precision is not integer"); report_error_(s, "precision is not integer");
}
if (value > INT_MAX)
report_error_(s, "number is too big in format");
spec.precision_ = static_cast<int>(value);
if (*s++ != '}')
throw FormatError("unmatched '{' in format");
--report_error_.num_open_braces;
} else {
report_error_(s, "missing precision in format");
}
if (arg.type != Arg::DOUBLE && arg.type != Arg::LONG_DOUBLE) {
report_error_(s,
"precision specifier requires floating-point argument");
} }
if (value > INT_MAX)
report_error_(s, "number is too big in format");
spec.precision_ = static_cast<int>(value);
if (*s++ != '}')
throw FormatError("unmatched '{' in format");
--report_error_.num_open_braces;
} else {
report_error_(s, "missing precision in format");
}
if (arg.type != Arg::DOUBLE && arg.type != Arg::LONG_DOUBLE) {
report_error_(s,
"precision specifier requires floating-point argument");
} }
// Parse type.
if (*s != '}' && *s)
spec.type_ = static_cast<char>(*s++);
} }
if (*s++ != '}') // Parse type.
throw FormatError("unmatched '{' in format"); if (*s != '}' && *s)
start = s; spec.type_ = static_cast<char>(*s++);
}
// Format argument. if (*s++ != '}')
switch (arg.type) { throw FormatError("unmatched '{' in format");
start_ = s;
// Format argument.
switch (arg.type) {
case Arg::INT: case Arg::INT:
writer.FormatInt(arg.int_value, spec); writer_.FormatInt(arg.int_value, spec);
break; break;
case Arg::UINT: case Arg::UINT:
writer.FormatInt(arg.uint_value, spec); writer_.FormatInt(arg.uint_value, spec);
break; break;
case Arg::LONG_LONG: case Arg::LONG_LONG:
writer.FormatInt(arg.long_long_value, spec); writer_.FormatInt(arg.long_long_value, spec);
break; break;
case Arg::ULONG_LONG: case Arg::ULONG_LONG:
writer.FormatInt(arg.ulong_long_value, spec); writer_.FormatInt(arg.ulong_long_value, spec);
break; break;
case Arg::DOUBLE: case Arg::DOUBLE:
writer.FormatDouble(arg.double_value, spec); writer_.FormatDouble(arg.double_value, spec);
break; break;
case Arg::LONG_DOUBLE: case Arg::LONG_DOUBLE:
writer.FormatDouble(arg.long_double_value, spec); writer_.FormatDouble(arg.long_double_value, spec);
break; break;
case Arg::CHAR: { case Arg::CHAR: {
if (spec.type_ && spec.type_ != 'c') if (spec.type_ && spec.type_ != 'c')
@ -1044,45 +1034,66 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (spec.width_ > 1) { if (spec.width_ > 1) {
Char fill = static_cast<Char>(spec.fill()); Char fill = static_cast<Char>(spec.fill());
out = writer.GrowBuffer(spec.width_); out = writer_.GrowBuffer(spec.width_);
if (spec.align_ == ALIGN_RIGHT) { if (spec.align_ == ALIGN_RIGHT) {
std::fill_n(out, spec.width_ - 1, fill); std::fill_n(out, spec.width_ - 1, fill);
out += spec.width_ - 1; out += spec.width_ - 1;
} else if (spec.align_ == ALIGN_CENTER) { } else if (spec.align_ == ALIGN_CENTER) {
out = writer.FillPadding(out, spec.width_, 1, fill); out = writer_.FillPadding(out, spec.width_, 1, fill);
} else { } else {
std::fill_n(out + 1, spec.width_ - 1, fill); std::fill_n(out + 1, spec.width_ - 1, fill);
} }
} else { } else {
out = writer.GrowBuffer(1); out = writer_.GrowBuffer(1);
} }
*out = static_cast<Char>(arg.int_value); *out = static_cast<Char>(arg.int_value);
break; break;
} }
case Arg::STRING: case Arg::STRING:
writer.write_str(arg.string, spec); writer_.write_str(arg.string, spec);
break; break;
case Arg::WSTRING: case Arg::WSTRING:
writer.write_str(internal::CharTraits<Char>::convert(arg.wstring), spec); writer_.write_str(internal::CharTraits<Char>::convert(arg.wstring), spec);
break; break;
case Arg::POINTER: case Arg::POINTER:
if (spec.type_ && spec.type_ != 'p') if (spec.type_ && spec.type_ != 'p')
internal::ReportUnknownType(spec.type_, "pointer"); internal::ReportUnknownType(spec.type_, "pointer");
spec.flags_= HASH_FLAG; spec.flags_= HASH_FLAG;
spec.type_ = 'x'; spec.type_ = 'x';
writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec); writer_.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
break; break;
case Arg::CUSTOM: case Arg::CUSTOM:
if (spec.type_) arg.custom.format(this, arg.custom.value, s - 1);
internal::ReportUnknownType(spec.type_, "object");
arg.custom.format(&writer, arg.custom.value, spec);
break; break;
default: default:
assert(false); assert(false);
break; break;
}
} }
writer.buffer_.append(start, s); return s;
}
template <typename Char>
void fmt::internal::FormatParser<Char>::Format(
BasicStringRef<Char> format_str, const ArgList &args) {
const Char *s = start_ = format_str.c_str();
args_ = args;
next_arg_index_ = 0;
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");
report_error_.num_open_braces = 1;
writer_.buffer_.append(start_, s - 1);
Arg arg = ParseArgIndex(s);
s = format(s, arg);
}
writer_.buffer_.append(start_, s);
} }
void fmt::ReportSystemError( void fmt::ReportSystemError(
@ -1131,8 +1142,8 @@ template fmt::BasicWriter<char>::CharPtr
fmt::BasicWriter<char>::FillPadding(CharPtr buffer, fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill); unsigned total_size, std::size_t content_size, wchar_t fill);
template void fmt::BasicWriter<char>::FormatParser::Format( template void fmt::internal::FormatParser<char>::Format(
BasicWriter<char> &writer, BasicStringRef<char> format, const ArgList &args); BasicStringRef<char> format, const ArgList &args);
template void fmt::internal::PrintfParser<char>::Format( template void fmt::internal::PrintfParser<char>::Format(
BasicWriter<char> &writer, BasicStringRef<char> format, const ArgList &args); BasicWriter<char> &writer, BasicStringRef<char> format, const ArgList &args);
@ -1143,9 +1154,8 @@ template fmt::BasicWriter<wchar_t>::CharPtr
fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer, fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill); unsigned total_size, std::size_t content_size, wchar_t fill);
template void fmt::BasicWriter<wchar_t>::FormatParser::Format( template void fmt::internal::FormatParser<wchar_t>::Format(
BasicWriter<wchar_t> &writer, BasicStringRef<wchar_t> format, BasicStringRef<wchar_t> format, const ArgList &args);
const ArgList &args);
template void fmt::internal::PrintfParser<wchar_t>::Format( template void fmt::internal::PrintfParser<wchar_t>::Format(
BasicWriter<wchar_t> &writer, BasicStringRef<wchar_t> format, BasicWriter<wchar_t> &writer, BasicStringRef<wchar_t> format,

View File

@ -130,8 +130,13 @@ typedef BasicWriter<wchar_t> WWriter;
struct FormatSpec; struct FormatSpec;
namespace internal {
template <typename Char>
class FormatParser;
}
template <typename Char, typename T> template <typename Char, typename T>
void format(BasicWriter<Char> &w, const FormatSpec &spec, const T &value); void format(internal::FormatParser<Char> &f, const Char *format_str, const T &value);
/** /**
\rst \rst
@ -598,7 +603,7 @@ struct Arg {
Type type; Type type;
typedef void (*FormatFunc)( typedef void (*FormatFunc)(
void *writer, const void *arg, const FormatSpec &spec); void *formatter, const void *arg, const void *format_str);
struct CustomValue { struct CustomValue {
const void *value; const void *value;
@ -648,9 +653,9 @@ class MakeArg : public Arg {
// Formats an argument of a custom type, such as a user-defined class. // Formats an argument of a custom type, such as a user-defined class.
template <typename T> template <typename T>
static void format_custom_arg( static void format_custom_arg(
void *writer, const void *arg, const FormatSpec &spec) { void *formatter, const void *arg, const void *format_str) {
format(*static_cast<BasicWriter<Char>*>(writer), format(*static_cast<FormatParser<Char>*>(formatter),
spec, *static_cast<const T*>(arg)); static_cast<const Char*>(format_str), *static_cast<const T*>(arg));
} }
public: public:
@ -747,6 +752,32 @@ class ArgList {
}; };
namespace internal { namespace internal {
// Format string parser.
// TODO: rename to Formatter
template <typename Char>
class FormatParser {
private:
BasicWriter<Char> &writer_;
ArgList args_;
int next_arg_index_;
const Char *start_;
fmt::internal::FormatErrorReporter<Char> report_error_;
// Parses argument index and returns an argument with this index.
const internal::Arg &ParseArgIndex(const Char *&s);
void CheckSign(const Char *&s, const internal::Arg &arg);
public:
explicit FormatParser(BasicWriter<Char> &w) : writer_(w) {}
BasicWriter<Char> &writer() { return writer_; }
void Format(BasicStringRef<Char> format_str, const ArgList &args);
const Char *format(const Char *format_str, const internal::Arg &arg);
};
// Printf format string parser. // Printf format string parser.
template <typename Char> template <typename Char>
class PrintfParser { class PrintfParser {
@ -1225,23 +1256,7 @@ class BasicWriter {
// Do not implement! // Do not implement!
void operator<<(typename internal::CharTraits<Char>::UnsupportedStrType); void operator<<(typename internal::CharTraits<Char>::UnsupportedStrType);
// Format string parser. friend class internal::FormatParser<Char>;
class FormatParser {
private:
ArgList args_;
int next_arg_index_;
fmt::internal::FormatErrorReporter<Char> report_error_;
// Parses argument index and returns an argument with this index.
const internal::Arg &ParseArgIndex(const Char *&s);
void CheckSign(const Char *&s, const internal::Arg &arg);
public:
void Format(BasicWriter<Char> &writer,
BasicStringRef<Char> format, const ArgList &args);
};
friend class internal::PrintfParser<Char>; friend class internal::PrintfParser<Char>;
public: public:
@ -1321,8 +1336,8 @@ class BasicWriter {
See also `Format String Syntax`_. See also `Format String Syntax`_.
\endrst \endrst
*/ */
inline void write(BasicStringRef<Char> format, const ArgList &args) { void write(BasicStringRef<Char> format, const ArgList &args) {
FormatParser().Format(*this, format, args); internal::FormatParser<Char>(*this).Format(format, args);
} }
FMT_VARIADIC_VOID(write, fmt::BasicStringRef<Char>) FMT_VARIADIC_VOID(write, fmt::BasicStringRef<Char>)
@ -1396,14 +1411,11 @@ class BasicWriter {
template <typename StringChar> template <typename StringChar>
BasicWriter &operator<<(const StrFormatSpec<StringChar> &spec) { BasicWriter &operator<<(const StrFormatSpec<StringChar> &spec) {
const StringChar *s = spec.str(); const StringChar *s = spec.str();
// TODO: error if fill is not convertible to Char
write_str(s, std::char_traits<Char>::length(s), spec); write_str(s, std::char_traits<Char>::length(s), spec);
return *this; return *this;
} }
void write_str(const std::basic_string<Char> &s, const FormatSpec &spec) {
write_str(s.data(), s.size(), spec);
}
void clear() { buffer_.clear(); } void clear() { buffer_.clear(); }
}; };
@ -1469,7 +1481,6 @@ typename fmt::BasicWriter<Char>::CharPtr
} }
CharPtr p = GrowBuffer(width); CharPtr p = GrowBuffer(width);
CharPtr end = p + width; CharPtr end = p + width;
// TODO: error if fill is not convertible to Char
if (align == ALIGN_LEFT) { if (align == ALIGN_LEFT) {
std::copy(prefix, prefix + prefix_size, p); std::copy(prefix, prefix + prefix_size, p);
p += size; p += size;
@ -1575,12 +1586,13 @@ void BasicWriter<Char>::FormatInt(T value, const Spec &spec) {
} }
} }
// The default formatting function. // Formats a value.
template <typename Char, typename T> template <typename Char, typename T>
void format(BasicWriter<Char> &w, const FormatSpec &spec, const T &value) { void format(internal::FormatParser<Char> &f,
const Char *format_str, const T &value) {
std::basic_ostringstream<Char> os; std::basic_ostringstream<Char> os;
os << value; os << value;
w.write_str(os.str(), spec); f.format(format_str, internal::MakeArg<Char>(os.str()));
} }
// Reports a system error without throwing an exception. // Reports a system error without throwing an exception.

View File

@ -519,7 +519,7 @@ TEST(WriterTest, pad) {
EXPECT_EQ(" 33", (Writer() << pad(33ll, 7)).str()); EXPECT_EQ(" 33", (Writer() << pad(33ll, 7)).str());
EXPECT_EQ(" 44", (Writer() << pad(44ull, 7)).str()); EXPECT_EQ(" 44", (Writer() << pad(44ull, 7)).str());
BasicWriter<char> w; Writer w;
w.clear(); w.clear();
w << pad(42, 5, '0'); w << pad(42, 5, '0');
EXPECT_EQ("00042", w.str()); EXPECT_EQ("00042", w.str());
@ -1312,14 +1312,14 @@ TEST(FormatterTest, FormatUsingIOStreams) {
std::string s = format("The date is {0}", Date(2012, 12, 9)); std::string s = format("The date is {0}", Date(2012, 12, 9));
EXPECT_EQ("The date is 2012-12-9", s); EXPECT_EQ("The date is 2012-12-9", s);
Date date(2012, 12, 9); Date date(2012, 12, 9);
CheckUnknownTypes(date, "", "object"); CheckUnknownTypes(date, "s", "string");
} }
class Answer {}; class Answer {};
template <typename Char> template <typename Char>
void format(BasicWriter<Char> &w, const fmt::FormatSpec &spec, Answer) { void format(fmt::internal::FormatParser<Char> &f, const Char *, Answer) {
w.write_str("42", spec); f.writer() << "42";
} }
TEST(FormatterTest, CustomFormat) { TEST(FormatterTest, CustomFormat) {

View File

@ -185,7 +185,8 @@ TEST(UtilTest, MakeArg) {
EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type); EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type);
arg.custom.value = &t; arg.custom.value = &t;
fmt::Writer w; fmt::Writer w;
arg.custom.format(&w, &t, fmt::FormatSpec()); fmt::internal::FormatParser<char> formatter(w);
arg.custom.format(&formatter, &t, "}");
EXPECT_EQ("test", w.str()); EXPECT_EQ("test", w.str());
} }