Took PrintfArgFormatter out of internal

This commit is contained in:
Glen Stark 2016-06-02 15:35:27 +02:00
parent 534e26873f
commit a8d0ed0828
4 changed files with 29 additions and 27 deletions

View File

@ -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::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); BasicWriter<char> &writer, CStringRef format);
template int fmt::internal::CharTraits<char>::format_float( template int fmt::internal::CharTraits<char>::format_float(
@ -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::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); BasicWriter<wchar_t> &writer, WCStringRef format);
template int fmt::internal::CharTraits<wchar_t>::format_float( template int fmt::internal::CharTraits<wchar_t>::format_float(

View File

@ -1319,8 +1319,6 @@ class RuntimeError : public std::runtime_error {
RuntimeError() : std::runtime_error("") {} RuntimeError() : std::runtime_error("") {}
}; };
template <typename Char>
class PrintfArgFormatter;
template <typename Char> template <typename Char>
class ArgMap; class ArgMap;
@ -1939,15 +1937,19 @@ class FormatterBase {
} }
}; };
template <typename Char> class PrintfArgFormatter;
} // namespace internal
// A printf formatter. // A printf formatter.
template <typename Char, typename PAF = fmt::internal::PrintfArgFormatter<Char> > template <typename Char, typename PAF = fmt::internal::PrintfArgFormatter<Char> >
class PrintfFormatter : private FormatterBase { class PrintfFormatter : private internal::FormatterBase {
private: private:
void parse_flags(FormatSpec &spec, const Char *&s); void parse_flags(FormatSpec &spec, const Char *&s);
// Returns the argument with specified index or, if arg_index is equal // Returns the argument with specified index or, if arg_index is equal
// to the maximum unsigned value, the next argument. // 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)()); unsigned arg_index = (std::numeric_limits<unsigned>::max)());
// Parses argument index, flags and width and returns the argument index. // Parses argument index, flags and width and returns the argument index.
@ -1958,7 +1960,6 @@ class PrintfFormatter : private FormatterBase {
FMT_API void format(BasicWriter<Char> &writer, FMT_API void format(BasicWriter<Char> &writer,
BasicCStringRef<Char> format_str); BasicCStringRef<Char> format_str);
}; };
} // namespace internal
/** /**
\rst \rst
@ -3190,7 +3191,7 @@ FMT_API void print(CStringRef format_str, ArgList args);
template <typename Char> template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) { void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) {
internal::PrintfFormatter<Char>(args).format(w, format); PrintfFormatter<Char>(args).format(w, format);
} }
/** /**

View File

@ -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) { FormatSpec &spec, const Char *&s) {
for (;;) { for (;;) {
switch (*s++) { switch (*s++) {
@ -280,11 +283,11 @@ void fmt::internal::PrintfFormatter<Char, PAF>::parse_flags(
} }
template <typename Char, typename PAF> 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) { const Char *s, unsigned arg_index) {
(void)s; (void)s;
const char *error = 0; 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); next_arg(error) : FormatterBase::get_arg(arg_index - 1, error);
if (error) if (error)
FMT_THROW(FormatError(!*s ? "invalid format string" : error)); FMT_THROW(FormatError(!*s ? "invalid format string" : error));
@ -292,14 +295,14 @@ Arg fmt::internal::PrintfFormatter<Char, PAF>::get_arg(
} }
template <typename Char, typename PAF> 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) { const Char *&s, FormatSpec &spec) {
unsigned arg_index = UINT_MAX; unsigned arg_index = UINT_MAX;
Char c = *s; Char c = *s;
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
// Parse an argument index (if followed by '$') or a width possibly // Parse an argument index (if followed by '$') or a width possibly
// preceded with '0' flag(s). // 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 if (*s == '$') { // value is an argument index
++s; ++s;
arg_index = value; arg_index = value;
@ -317,16 +320,16 @@ unsigned fmt::internal::PrintfFormatter<Char, PAF>::parse_header(
parse_flags(spec, s); parse_flags(spec, s);
// Parse width. // Parse width.
if (*s >= '0' && *s <= '9') { if (*s >= '0' && *s <= '9') {
spec.width_ = parse_nonnegative_int(s); spec.width_ = internal::parse_nonnegative_int(s);
} else if (*s == '*') { } else if (*s == '*') {
++s; ++s;
spec.width_ = WidthHandler(spec).visit(get_arg(s)); spec.width_ = internal::WidthHandler(spec).visit(get_arg(s));
} }
return arg_index; return arg_index;
} }
template <typename Char, typename PAF > 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) { BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) {
const Char *start = format_str.c_str(); const Char *start = format_str.c_str();
const Char *s = start; const Char *s = start;
@ -350,18 +353,18 @@ template <typename Char, typename PAF >
if (*s == '.') { if (*s == '.') {
++s; ++s;
if ('0' <= *s && *s <= '9') { 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 == '*') { } else if (*s == '*') {
++s; ++s;
spec.precision_ = PrecisionHandler().visit(get_arg(s)); spec.precision_ = internal::PrecisionHandler().visit(get_arg(s));
} }
} }
Arg arg = get_arg(s, arg_index); internal::Arg arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && IsZeroInt().visit(arg)) if (spec.flag(HASH_FLAG) && internal::IsZeroInt().visit(arg))
spec.flags_ &= ~to_unsigned<int>(HASH_FLAG); spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
if (spec.fill_ == '0') { if (spec.fill_ == '0') {
if (arg.type <= Arg::LAST_NUMERIC_TYPE) if (arg.type <= internal::Arg::LAST_NUMERIC_TYPE)
spec.align_ = ALIGN_NUMERIC; spec.align_ = ALIGN_NUMERIC;
else else
spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
@ -403,7 +406,7 @@ template <typename Char, typename PAF >
if (!*s) if (!*s)
FMT_THROW(FormatError("invalid format string")); FMT_THROW(FormatError("invalid format string"));
spec.type_ = static_cast<char>(*s++); spec.type_ = static_cast<char>(*s++);
if (arg.type <= Arg::LAST_INTEGER_TYPE) { if (arg.type <= internal::Arg::LAST_INTEGER_TYPE) {
// Normalize type. // Normalize type.
switch (spec.type_) { switch (spec.type_) {
case 'i': case 'u': case 'i': case 'u':
@ -424,8 +427,6 @@ template <typename Char, typename PAF >
write(writer, start, s); write(writer, start, s);
} }
}// namespace internal
} // namespace fmt } // namespace fmt
#endif #endif

View File

@ -46,7 +46,7 @@ FMT_VARIADIC(std::string, custom_format, const char *)
std::string printfer(const char* fstr, fmt::ArgList args){ std::string printfer(const char* fstr, fmt::ArgList args){
fmt::MemoryWriter writer; fmt::MemoryWriter writer;
fmt::internal::PrintfFormatter<char, CustomPAF> pfer(args); fmt::PrintfFormatter<char, CustomPAF> pfer(args);
pfer.format(writer,fstr); pfer.format(writer,fstr);
return writer.str(); return writer.str();
} }