Skip to content

Commit

Permalink
Took PrintfArgFormatter out of internal
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Stark committed Jun 2, 2016
1 parent 534e268 commit a8d0ed0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions fmt/format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ template void fmt::internal::FixedBuffer<char>::grow(std::size_t);

template void fmt::internal::ArgMap<char>::init(const fmt::ArgList &args);

template void fmt::internal::PrintfFormatter<char>::format(
template void fmt::PrintfFormatter<char>::format(
BasicWriter<char> &writer, CStringRef format);

template int fmt::internal::CharTraits<char>::format_float(
Expand All @@ -518,7 +518,7 @@ template void fmt::internal::FixedBuffer<wchar_t>::grow(std::size_t);

template void fmt::internal::ArgMap<wchar_t>::init(const fmt::ArgList &args);

template void fmt::internal::PrintfFormatter<wchar_t>::format(
template void fmt::PrintfFormatter<wchar_t>::format(
BasicWriter<wchar_t> &writer, WCStringRef format);

template int fmt::internal::CharTraits<wchar_t>::format_float(
Expand Down
13 changes: 7 additions & 6 deletions fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,6 @@ class RuntimeError : public std::runtime_error {
RuntimeError() : std::runtime_error("") {}
};

template <typename Char>
class PrintfArgFormatter;

template <typename Char>
class ArgMap;
Expand Down Expand Up @@ -1939,15 +1937,19 @@ class FormatterBase {
}
};

template <typename Char> class PrintfArgFormatter;

} // namespace internal

// A printf formatter.
template <typename Char, typename PAF = fmt::internal::PrintfArgFormatter<Char> >
class PrintfFormatter : private FormatterBase {
class PrintfFormatter : private internal::FormatterBase {
private:
void parse_flags(FormatSpec &spec, const Char *&s);

// Returns the argument with specified index or, if arg_index is equal
// to the maximum unsigned value, the next argument.
Arg get_arg(const Char *s,
internal::Arg get_arg(const Char *s,
unsigned arg_index = (std::numeric_limits<unsigned>::max)());

// Parses argument index, flags and width and returns the argument index.
Expand All @@ -1958,7 +1960,6 @@ class PrintfFormatter : private FormatterBase {
FMT_API void format(BasicWriter<Char> &writer,
BasicCStringRef<Char> format_str);
};
} // namespace internal

/**
\rst
Expand Down Expand Up @@ -3190,7 +3191,7 @@ FMT_API void print(CStringRef format_str, ArgList args);

template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) {
internal::PrintfFormatter<Char>(args).format(w, format);
PrintfFormatter<Char>(args).format(w, format);
}

/**
Expand Down
37 changes: 19 additions & 18 deletions fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,11 @@ class PrintfArgFormatter :
}
};

template <typename Char, typename PAF>
void fmt::internal::PrintfFormatter<Char, PAF>::parse_flags(

}// namespace internal

template <typename Char, typename PAF>
void fmt::PrintfFormatter<Char, PAF>::parse_flags(
FormatSpec &spec, const Char *&s) {
for (;;) {
switch (*s++) {
Expand All @@ -280,26 +283,26 @@ void fmt::internal::PrintfFormatter<Char, PAF>::parse_flags(
}

template <typename Char, typename PAF>
Arg fmt::internal::PrintfFormatter<Char, PAF>::get_arg(
internal::Arg fmt::PrintfFormatter<Char, PAF>::get_arg(
const Char *s, unsigned arg_index) {
(void)s;
const char *error = 0;
Arg arg = arg_index == UINT_MAX ?
internal::Arg arg = arg_index == UINT_MAX ?
next_arg(error) : FormatterBase::get_arg(arg_index - 1, error);
if (error)
FMT_THROW(FormatError(!*s ? "invalid format string" : error));
return arg;
}

template <typename Char, typename PAF>
unsigned fmt::internal::PrintfFormatter<Char, PAF>::parse_header(
unsigned fmt::PrintfFormatter<Char, PAF>::parse_header(
const Char *&s, FormatSpec &spec) {
unsigned arg_index = UINT_MAX;
Char c = *s;
if (c >= '0' && c <= '9') {
// Parse an argument index (if followed by '$') or a width possibly
// preceded with '0' flag(s).
unsigned value = parse_nonnegative_int(s);
unsigned value = internal::parse_nonnegative_int(s);
if (*s == '$') { // value is an argument index
++s;
arg_index = value;
Expand All @@ -317,16 +320,16 @@ unsigned fmt::internal::PrintfFormatter<Char, PAF>::parse_header(
parse_flags(spec, s);
// Parse width.
if (*s >= '0' && *s <= '9') {
spec.width_ = parse_nonnegative_int(s);
spec.width_ = internal::parse_nonnegative_int(s);
} else if (*s == '*') {
++s;
spec.width_ = WidthHandler(spec).visit(get_arg(s));
spec.width_ = internal::WidthHandler(spec).visit(get_arg(s));
}
return arg_index;
}

template <typename Char, typename PAF >
void fmt::internal::PrintfFormatter<Char,PAF>::format(
void fmt::PrintfFormatter<Char,PAF>::format(
BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) {
const Char *start = format_str.c_str();
const Char *s = start;
Expand All @@ -350,18 +353,18 @@ template <typename Char, typename PAF >
if (*s == '.') {
++s;
if ('0' <= *s && *s <= '9') {
spec.precision_ = static_cast<int>(parse_nonnegative_int(s));
spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
} else if (*s == '*') {
++s;
spec.precision_ = PrecisionHandler().visit(get_arg(s));
spec.precision_ = internal::PrecisionHandler().visit(get_arg(s));
}
}

Arg arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && IsZeroInt().visit(arg))
spec.flags_ &= ~to_unsigned<int>(HASH_FLAG);
internal::Arg arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && internal::IsZeroInt().visit(arg))
spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
if (spec.fill_ == '0') {
if (arg.type <= Arg::LAST_NUMERIC_TYPE)
if (arg.type <= internal::Arg::LAST_NUMERIC_TYPE)
spec.align_ = ALIGN_NUMERIC;
else
spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
Expand Down Expand Up @@ -403,7 +406,7 @@ template <typename Char, typename PAF >
if (!*s)
FMT_THROW(FormatError("invalid format string"));
spec.type_ = static_cast<char>(*s++);
if (arg.type <= Arg::LAST_INTEGER_TYPE) {
if (arg.type <= internal::Arg::LAST_INTEGER_TYPE) {
// Normalize type.
switch (spec.type_) {
case 'i': case 'u':
Expand All @@ -424,8 +427,6 @@ template <typename Char, typename PAF >
write(writer, start, s);
}

}// namespace internal

} // namespace fmt

#endif
2 changes: 1 addition & 1 deletion test/custom-formatter-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ FMT_VARIADIC(std::string, custom_format, const char *)

std::string printfer(const char* fstr, fmt::ArgList args){
fmt::MemoryWriter writer;
fmt::internal::PrintfFormatter<char, CustomPAF> pfer(args);
fmt::PrintfFormatter<char, CustomPAF> pfer(args);
pfer.format(writer,fstr);
return writer.str();
}
Expand Down

0 comments on commit a8d0ed0

Please sign in to comment.