forked from fmtlib/fmt
Pass correct formatters to make_format_args
This commit is contained in:
@ -1494,8 +1494,8 @@ class basic_format_args {
|
|||||||
|
|
||||||
basic_format_args() : types_(0) {}
|
basic_format_args() : types_(0) {}
|
||||||
|
|
||||||
template <typename F, typename... Args>
|
template <typename... Args>
|
||||||
basic_format_args(const format_arg_store<F, Args...> &store)
|
basic_format_args(const format_arg_store<Formatter, Args...> &store)
|
||||||
: types_(store.TYPES) {
|
: types_(store.TYPES) {
|
||||||
set_data(store.data());
|
set_data(store.data());
|
||||||
}
|
}
|
||||||
|
@ -69,19 +69,24 @@ struct ConvertToIntImpl<T, true> {
|
|||||||
|
|
||||||
// Write the content of w to os.
|
// Write the content of w to os.
|
||||||
void write(std::ostream &os, Writer &w);
|
void write(std::ostream &os, Writer &w);
|
||||||
|
|
||||||
|
template <typename Char, typename T>
|
||||||
|
BasicStringRef<Char> format_value(
|
||||||
|
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> &buffer,
|
||||||
|
const T &value) {
|
||||||
|
internal::FormatBuf<Char> format_buf(buffer);
|
||||||
|
std::basic_ostream<Char> output(&format_buf);
|
||||||
|
output << value;
|
||||||
|
return BasicStringRef<Char>(&buffer[0], format_buf.size());
|
||||||
|
}
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
// Formats a value.
|
// Formats a value.
|
||||||
template <typename Char, typename ArgFormatter, typename T>
|
template <typename Char, typename ArgFormatter, typename T>
|
||||||
void format_value(BasicFormatter<Char, ArgFormatter> &f,
|
void format_value(BasicFormatter<Char, ArgFormatter> &f,
|
||||||
const Char *&format_str, const T &value) {
|
const Char *&format_str, const T &value) {
|
||||||
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
|
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
|
||||||
|
auto str = internal::format_value(buffer, value);
|
||||||
internal::FormatBuf<Char> format_buf(buffer);
|
|
||||||
std::basic_ostream<Char> output(&format_buf);
|
|
||||||
output << value;
|
|
||||||
|
|
||||||
BasicStringRef<Char> str(&buffer[0], format_buf.size());
|
|
||||||
typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
|
typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
|
||||||
format_str = f.format(format_str, MakeArg(str));
|
format_str = f.format(format_str, MakeArg(str));
|
||||||
}
|
}
|
||||||
|
26
fmt/printf.h
26
fmt/printf.h
@ -281,9 +281,14 @@ class PrintfArgFormatter
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** This template formats data and writes the output to a writer. */
|
/** This template formats data and writes the output to a writer. */
|
||||||
template <typename Char, typename ArgFormatter = PrintfArgFormatter<Char> >
|
template <typename CharType,
|
||||||
|
typename ArgFormatter = PrintfArgFormatter<CharType> >
|
||||||
class PrintfFormatter :
|
class PrintfFormatter :
|
||||||
private internal::FormatterBase<PrintfFormatter<Char, ArgFormatter>> {
|
private internal::FormatterBase<PrintfFormatter<CharType, ArgFormatter>> {
|
||||||
|
public:
|
||||||
|
/** The character type for the output. */
|
||||||
|
typedef CharType Char;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BasicWriter<Char> &writer_;
|
BasicWriter<Char> &writer_;
|
||||||
|
|
||||||
@ -312,6 +317,8 @@ class PrintfFormatter :
|
|||||||
BasicWriter<Char> &w)
|
BasicWriter<Char> &w)
|
||||||
: Base(args), writer_(w) {}
|
: Base(args), writer_(w) {}
|
||||||
|
|
||||||
|
BasicWriter<Char> &writer() { return writer_; }
|
||||||
|
|
||||||
/** Formats stored arguments and writes the output to the writer. */
|
/** Formats stored arguments and writes the output to the writer. */
|
||||||
FMT_API void format(BasicCStringRef<Char> format_str);
|
FMT_API void format(BasicCStringRef<Char> format_str);
|
||||||
};
|
};
|
||||||
@ -488,6 +495,13 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) {
|
|||||||
this->write(writer_, start, s);
|
this->write(writer_, start, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formats a value.
|
||||||
|
template <typename Char, typename T>
|
||||||
|
void format_value(PrintfFormatter<Char> &f, const Char *&, const T &value) {
|
||||||
|
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
|
||||||
|
f.writer() << internal::format_value(buffer, value);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
|
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
|
||||||
basic_format_args<PrintfFormatter<Char>> args) {
|
basic_format_args<PrintfFormatter<Char>> args) {
|
||||||
@ -512,7 +526,7 @@ inline std::string vsprintf(CStringRef format,
|
|||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
inline std::string sprintf(CStringRef format_str, const Args & ... args) {
|
inline std::string sprintf(CStringRef format_str, const Args & ... args) {
|
||||||
return vsprintf(format_str, make_format_args<BasicFormatter<char>>(args...));
|
return vsprintf(format_str, make_format_args<PrintfFormatter<char>>(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::wstring vsprintf(WCStringRef format,
|
inline std::wstring vsprintf(WCStringRef format,
|
||||||
@ -524,7 +538,7 @@ inline std::wstring vsprintf(WCStringRef format,
|
|||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
|
inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
|
||||||
auto vargs = make_format_args<BasicFormatter<wchar_t>>(args...);
|
auto vargs = make_format_args<PrintfFormatter<wchar_t>>(args...);
|
||||||
return vsprintf(format_str, vargs);
|
return vsprintf(format_str, vargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -562,7 +576,7 @@ inline int vprintf(CStringRef format,
|
|||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
inline int printf(CStringRef format_str, const Args & ... args) {
|
inline int printf(CStringRef format_str, const Args & ... args) {
|
||||||
return vprintf(format_str, make_format_args<BasicFormatter<char>>(args...));
|
return vprintf(format_str, make_format_args<PrintfFormatter<char>>(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int vfprintf(std::ostream &os, CStringRef format_str,
|
inline int vfprintf(std::ostream &os, CStringRef format_str,
|
||||||
@ -585,7 +599,7 @@ inline int vfprintf(std::ostream &os, CStringRef format_str,
|
|||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
inline int fprintf(std::ostream &os, CStringRef format_str,
|
inline int fprintf(std::ostream &os, CStringRef format_str,
|
||||||
const Args & ... args) {
|
const Args & ... args) {
|
||||||
auto vargs = make_format_args<BasicFormatter<char>>(args...);
|
auto vargs = make_format_args<PrintfFormatter<char>>(args...);
|
||||||
return vfprintf(os, format_str, vargs);
|
return vfprintf(os, format_str, vargs);
|
||||||
}
|
}
|
||||||
} // namespace fmt
|
} // namespace fmt
|
||||||
|
@ -58,7 +58,7 @@ std::string custom_vformat(const char *format_str,
|
|||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
std::string custom_format(const char *format_str, const Args & ... args) {
|
std::string custom_format(const char *format_str, const Args & ... args) {
|
||||||
auto va = fmt::make_format_args<fmt::BasicFormatter<char>>(args...);
|
auto va = fmt::make_format_args<CustomFormatter>(args...);
|
||||||
return custom_vformat(format_str, va);
|
return custom_vformat(format_str, va);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ std::string custom_vsprintf(
|
|||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
std::string custom_sprintf(const char *format_str, const Args & ... args) {
|
std::string custom_sprintf(const char *format_str, const Args & ... args) {
|
||||||
auto va = fmt::make_format_args<fmt::BasicFormatter<char>>(args...);
|
auto va = fmt::make_format_args<CustomPrintfFormatter>(args...);
|
||||||
return custom_vsprintf(format_str, va);
|
return custom_vsprintf(format_str, va);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1644,7 +1644,7 @@ void custom_vformat(const char *format_str,
|
|||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void custom_format(const char *format_str, const Args & ... args) {
|
void custom_format(const char *format_str, const Args & ... args) {
|
||||||
auto va = fmt::make_format_args<fmt::BasicFormatter<char>>(args...);
|
auto va = fmt::make_format_args<CustomFormatter>(args...);
|
||||||
return custom_vformat(format_str, va);
|
return custom_vformat(format_str, va);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user