fmt::format<Char, T> inefficiency #92
This commit is contained in:
parent
d9e0f5c04e
commit
5343eab4c8
50
format.h
50
format.h
@ -38,6 +38,7 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
#ifndef FMT_USE_IOSTREAMS
|
#ifndef FMT_USE_IOSTREAMS
|
||||||
# define FMT_USE_IOSTREAMS 1
|
# define FMT_USE_IOSTREAMS 1
|
||||||
@ -478,6 +479,11 @@ class Buffer {
|
|||||||
|
|
||||||
T &operator[](std::size_t index) { return ptr_[index]; }
|
T &operator[](std::size_t index) { return ptr_[index]; }
|
||||||
const T &operator[](std::size_t index) const { return ptr_[index]; }
|
const T &operator[](std::size_t index) const { return ptr_[index]; }
|
||||||
|
|
||||||
|
|
||||||
|
template<class Elem, class Traits = std::char_traits<Elem> >
|
||||||
|
friend class basic_formatbuf;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -2027,6 +2033,11 @@ class BasicWriter {
|
|||||||
*/
|
*/
|
||||||
std::size_t size() const { return buffer_.size(); }
|
std::size_t size() const { return buffer_.size(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns underlying buffer.
|
||||||
|
*/
|
||||||
|
Buffer<Char>& buffer() const { return buffer_; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Returns a pointer to the output buffer content. No terminating null
|
Returns a pointer to the output buffer content. No terminating null
|
||||||
character is appended.
|
character is appended.
|
||||||
@ -2628,16 +2639,39 @@ class BasicArrayWriter : public BasicWriter<Char> {
|
|||||||
typedef BasicArrayWriter<char> ArrayWriter;
|
typedef BasicArrayWriter<char> ArrayWriter;
|
||||||
typedef BasicArrayWriter<wchar_t> WArrayWriter;
|
typedef BasicArrayWriter<wchar_t> WArrayWriter;
|
||||||
|
|
||||||
|
template<class Elem, class Traits = std::char_traits<Elem> >
|
||||||
|
class basic_formatbuf : public std::basic_streambuf<Elem, Traits> {
|
||||||
|
|
||||||
|
typedef typename std::basic_streambuf<Elem, Traits>::int_type int_type;
|
||||||
|
|
||||||
|
Buffer<Elem>& buffer_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
basic_formatbuf(BasicFormatter<Elem> &formatter) : buffer_(formatter.writer().buffer()) {
|
||||||
|
setp(buffer_.ptr_, buffer_.ptr_ + buffer_.size_, buffer_.ptr_ + buffer_.capacity_);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int_type overflow(int_type _Meta = Traits::eof()) {
|
||||||
|
buffer_.grow(buffer_.capacity_ * 2);
|
||||||
|
setp(buffer_.ptr_, buffer_.ptr_ + buffer_.size_, buffer_.ptr_ + buffer_.capacity_);
|
||||||
|
|
||||||
|
return traits_type::to_int_type(*pptr());
|
||||||
|
}
|
||||||
|
|
||||||
|
int_type flush() {
|
||||||
|
buffer_.size_ = pptr() - pbase();
|
||||||
|
return traits_type::to_int_type(*pptr());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Formats a value.
|
// Formats a value.
|
||||||
template <typename Char, typename T>
|
template <typename Char, typename T>
|
||||||
void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value) {
|
void format(BasicFormatter<Char> &formatter, const Char *&format_str, const T &value) {
|
||||||
std::basic_ostringstream<Char> os;
|
|
||||||
os << value;
|
basic_formatbuf<Char> format_buf(formatter);
|
||||||
std::basic_string<Char> str = os.str();
|
std::basic_ostream<Char> output(&format_buf);
|
||||||
internal::Arg arg = internal::MakeValue<Char>(str);
|
output << value;
|
||||||
arg.type = static_cast<internal::Arg::Type>(
|
format_buf.flush();
|
||||||
internal::MakeValue<Char>::type(str));
|
|
||||||
format_str = f.format(format_str, arg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reports a system error without throwing an exception.
|
// Reports a system error without throwing an exception.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user