mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-11 21:38:05 +00:00
Pass correct formatters to make_format_args
This commit is contained in:
parent
dafbec7553
commit
18dfa257d0
@ -1494,8 +1494,8 @@ class basic_format_args {
|
||||
|
||||
basic_format_args() : types_(0) {}
|
||||
|
||||
template <typename F, typename... Args>
|
||||
basic_format_args(const format_arg_store<F, Args...> &store)
|
||||
template <typename... Args>
|
||||
basic_format_args(const format_arg_store<Formatter, Args...> &store)
|
||||
: types_(store.TYPES) {
|
||||
set_data(store.data());
|
||||
}
|
||||
|
@ -69,19 +69,24 @@ struct ConvertToIntImpl<T, true> {
|
||||
|
||||
// Write the content of w to os.
|
||||
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
|
||||
|
||||
// Formats a value.
|
||||
template <typename Char, typename ArgFormatter, typename T>
|
||||
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::FormatBuf<Char> format_buf(buffer);
|
||||
std::basic_ostream<Char> output(&format_buf);
|
||||
output << value;
|
||||
|
||||
BasicStringRef<Char> str(&buffer[0], format_buf.size());
|
||||
auto str = internal::format_value(buffer, value);
|
||||
typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
|
||||
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. */
|
||||
template <typename Char, typename ArgFormatter = PrintfArgFormatter<Char> >
|
||||
template <typename CharType,
|
||||
typename ArgFormatter = PrintfArgFormatter<CharType> >
|
||||
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:
|
||||
BasicWriter<Char> &writer_;
|
||||
|
||||
@ -312,6 +317,8 @@ class PrintfFormatter :
|
||||
BasicWriter<Char> &w)
|
||||
: Base(args), writer_(w) {}
|
||||
|
||||
BasicWriter<Char> &writer() { return writer_; }
|
||||
|
||||
/** Formats stored arguments and writes the output to the writer. */
|
||||
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);
|
||||
}
|
||||
|
||||
// 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>
|
||||
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
|
||||
basic_format_args<PrintfFormatter<Char>> args) {
|
||||
@ -512,7 +526,7 @@ inline std::string vsprintf(CStringRef format,
|
||||
*/
|
||||
template <typename... 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,
|
||||
@ -524,7 +538,7 @@ inline std::wstring vsprintf(WCStringRef format,
|
||||
|
||||
template <typename... 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);
|
||||
}
|
||||
|
||||
@ -562,7 +576,7 @@ inline int vprintf(CStringRef format,
|
||||
*/
|
||||
template <typename... 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,
|
||||
@ -585,7 +599,7 @@ inline int vfprintf(std::ostream &os, CStringRef format_str,
|
||||
template <typename... Args>
|
||||
inline int fprintf(std::ostream &os, CStringRef format_str,
|
||||
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);
|
||||
}
|
||||
} // namespace fmt
|
||||
|
@ -58,7 +58,7 @@ std::string custom_vformat(const char *format_str,
|
||||
|
||||
template <typename... 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);
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ std::string custom_vsprintf(
|
||||
|
||||
template <typename... 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);
|
||||
}
|
||||
|
||||
|
@ -1644,7 +1644,7 @@ void custom_vformat(const char *format_str,
|
||||
|
||||
template <typename... 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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user