Initialize next_arg_index_ in ctor (#187)

This commit is contained in:
vitaut 2015-07-28 08:09:29 -07:00
parent c4d0bc1ac9
commit 200037683e
3 changed files with 15 additions and 20 deletions

View File

@ -930,10 +930,8 @@ unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
template <typename Char>
void fmt::internal::PrintfFormatter<Char>::format(
BasicWriter<Char> &writer, BasicCStringRef<Char> format_str,
const ArgList &args) {
BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) {
const Char *start = format_str.c_str();
set_args(args);
const Char *s = start;
while (*s) {
Char c = *s++;
@ -1199,11 +1197,9 @@ const Char *fmt::BasicFormatter<Char>::format(
}
template <typename Char>
void fmt::BasicFormatter<Char>::format(
BasicCStringRef<Char> format_str, const ArgList &args) {
void fmt::BasicFormatter<Char>::format(BasicCStringRef<Char> format_str) {
const Char *s = format_str.c_str();
const Char *start = s;
set_args(args);
while (*s) {
Char c = *s++;
if (c != '{' && c != '}') continue;
@ -1275,11 +1271,10 @@ template void fmt::internal::FixedBuffer<char>::grow(std::size_t);
template const char *fmt::BasicFormatter<char>::format(
const char *&format_str, const fmt::internal::Arg &arg);
template void fmt::BasicFormatter<char>::format(
CStringRef format, const ArgList &args);
template void fmt::BasicFormatter<char>::format(CStringRef format);
template void fmt::internal::PrintfFormatter<char>::format(
BasicWriter<char> &writer, CStringRef format, const ArgList &args);
BasicWriter<char> &writer, CStringRef format);
template int fmt::internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format,
@ -1297,11 +1292,10 @@ template const wchar_t *fmt::BasicFormatter<wchar_t>::format(
const wchar_t *&format_str, const fmt::internal::Arg &arg);
template void fmt::BasicFormatter<wchar_t>::format(
BasicCStringRef<wchar_t> format, const ArgList &args);
BasicCStringRef<wchar_t> format);
template void fmt::internal::PrintfFormatter<wchar_t>::format(
BasicWriter<wchar_t> &writer, WCStringRef format,
const ArgList &args);
BasicWriter<wchar_t> &writer, WCStringRef format);
template int fmt::internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format,

View File

@ -1280,7 +1280,7 @@ class FormatterBase {
protected:
const ArgList &args() const { return args_; }
void set_args(const ArgList &args) {
explicit FormatterBase(const ArgList &args) {
args_ = args;
next_arg_index_ = 0;
}
@ -1316,8 +1316,8 @@ class PrintfFormatter : private FormatterBase {
unsigned parse_header(const Char *&s, FormatSpec &spec);
public:
void format(BasicWriter<Char> &writer,
BasicCStringRef<Char> format_str, const ArgList &args);
explicit PrintfFormatter(const ArgList &args) : FormatterBase(args) {}
void format(BasicWriter<Char> &writer, BasicCStringRef<Char> format_str);
};
} // namespace internal
@ -1343,11 +1343,12 @@ class BasicFormatter : private internal::FormatterBase {
internal::Arg parse_arg_name(const Char *&s);
public:
explicit BasicFormatter(BasicWriter<Char> &w) : writer_(w) {}
BasicFormatter(const ArgList &args, BasicWriter<Char> &w)
: FormatterBase(args), writer_(w) {}
BasicWriter<Char> &writer() { return writer_; }
void format(BasicCStringRef<Char> format_str, const ArgList &args);
void format(BasicCStringRef<Char> format_str);
const Char *format(const Char *&format_str, const internal::Arg &arg);
};
@ -1989,7 +1990,7 @@ class BasicWriter {
\endrst
*/
void write(BasicCStringRef<Char> format, ArgList args) {
BasicFormatter<Char>(*this).format(format, args);
BasicFormatter<Char>(args, *this).format(format);
}
FMT_VARIADIC_VOID(write, BasicCStringRef<Char>)
@ -2665,7 +2666,7 @@ void print(std::ostream &os, CStringRef format_str, ArgList args);
template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) {
internal::PrintfFormatter<Char>().format(w, format, args);
internal::PrintfFormatter<Char>(args).format(w, format);
}
/**

View File

@ -561,7 +561,7 @@ TEST(ArgTest, MakeArg) {
EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type);
EXPECT_EQ(&t, arg.custom.value);
fmt::MemoryWriter w;
fmt::BasicFormatter<char> formatter(w);
fmt::BasicFormatter<char> formatter(fmt::ArgList(), w);
const char *s = "}";
arg.custom.format(&formatter, &t, &s);
EXPECT_EQ("test", w.str());