diff --git a/fmt/format.cc b/fmt/format.cc index 563a8f7d..3823c73c 100644 --- a/fmt/format.cc +++ b/fmt/format.cc @@ -205,7 +205,7 @@ void format_error_code(buffer &out, int error_code, void report_error(FormatFunc func, int error_code, string_view message) FMT_NOEXCEPT { - internal::MemoryBuffer full_message; + memory_buffer full_message; func(full_message, error_code, message); // Use Writer::data instead of Writer::c_str to avoid potential memory // allocation. @@ -217,10 +217,10 @@ void report_error(FormatFunc func, int error_code, FMT_FUNC void SystemError::init( int err_code, CStringRef format_str, args args) { error_code_ = err_code; - internal::MemoryBuffer buf; - format_system_error(buf, err_code, vformat(format_str, args)); + memory_buffer buffer; + format_system_error(buffer, err_code, vformat(format_str, args)); std::runtime_error &base = *this; - base = std::runtime_error(to_string(buf)); + base = std::runtime_error(to_string(buffer)); } template @@ -382,7 +382,7 @@ FMT_FUNC void internal::format_windows_error( FMT_FUNC void format_system_error( buffer &out, int error_code, string_view message) FMT_NOEXCEPT { FMT_TRY { - internal::MemoryBuffer buffer; + memory_buffer buffer; buffer.resize(internal::INLINE_BUFFER_SIZE); for (;;) { char *system_message = &buffer[0]; @@ -422,7 +422,7 @@ FMT_FUNC void report_windows_error( #endif FMT_FUNC void vprint(std::FILE *f, CStringRef format_str, args args) { - internal::MemoryBuffer buffer; + memory_buffer buffer; vformat_to(buffer, format_str, args); std::fwrite(buffer.data(), 1, buffer.size(), f); } @@ -444,7 +444,7 @@ void printf(basic_writer &w, BasicCStringRef format, args args); FMT_FUNC int vfprintf(std::FILE *f, CStringRef format, printf_args args) { - internal::MemoryBuffer buffer; + memory_buffer buffer; printf(buffer, format, args); std::size_t size = buffer.size(); return std::fwrite( diff --git a/fmt/format.h b/fmt/format.h index bb3bbfd8..d4de5ea6 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -659,13 +659,39 @@ inline std::basic_string to_string(const basic_buffer& buffer) { return std::basic_string(buffer.data(), buffer.size()); } -namespace internal { +/** + \rst + A dynamically growing memory buffer for trivially copyable/constructible types + with the first SIZE elements stored in the object itself. -// A memory buffer for trivially copyable/constructible types with the first -// SIZE elements stored in the object itself. -template | + +----------------+------------------------------+ + | wmemory_buffer | basic_memory_buffer | + +----------------+------------------------------+ + + **Example**:: + + memory_buffer out; + format_to(out, "The answer is {}.", 42); + + This will write the following output to the ``out`` object: + + .. code-block:: none + + The answer is 42. + + The output can be converted to an ``std::string`` with ``to_string(out)``. + \endrst + */ +// +template > -class MemoryBuffer : private Allocator, public basic_buffer { +class basic_memory_buffer : private Allocator, public basic_buffer { private: T data_[SIZE]; @@ -678,14 +704,14 @@ class MemoryBuffer : private Allocator, public basic_buffer { void grow(std::size_t size); public: - explicit MemoryBuffer(const Allocator &alloc = Allocator()) + explicit basic_memory_buffer(const Allocator &alloc = Allocator()) : Allocator(alloc), basic_buffer(data_, SIZE) {} - ~MemoryBuffer() { deallocate(); } + ~basic_memory_buffer() { deallocate(); } #if FMT_USE_RVALUE_REFERENCES private: // Move data from other to this buffer. - void move(MemoryBuffer &other) { + void move(basic_memory_buffer &other) { Allocator &this_alloc = *this, &other_alloc = other; this_alloc = std::move(other_alloc); this->size_ = other.size_; @@ -693,7 +719,7 @@ class MemoryBuffer : private Allocator, public basic_buffer { if (other.ptr_ == other.data_) { this->ptr_ = data_; std::uninitialized_copy(other.data_, other.data_ + this->size_, - make_ptr(data_, this->capacity_)); + internal::make_ptr(data_, this->capacity_)); } else { this->ptr_ = other.ptr_; // Set pointer to the inline array so that delete is not called @@ -703,11 +729,22 @@ class MemoryBuffer : private Allocator, public basic_buffer { } public: - MemoryBuffer(MemoryBuffer &&other) { + /** + \rst + Constructs a :class:`fmt::basic_memory_buffer` object moving the content + of the other object to it. + \endrst + */ + basic_memory_buffer(basic_memory_buffer &&other) { move(other); } - MemoryBuffer &operator=(MemoryBuffer &&other) { + /** + \rst + Moves the content of the other ``basic_memory_buffer`` object to this one. + \endrst + */ + basic_memory_buffer &operator=(basic_memory_buffer &&other) { assert(this != &other); deallocate(); move(other); @@ -720,14 +757,14 @@ class MemoryBuffer : private Allocator, public basic_buffer { }; template -void MemoryBuffer::grow(std::size_t size) { +void basic_memory_buffer::grow(std::size_t size) { std::size_t new_capacity = this->capacity_ + this->capacity_ / 2; if (size > new_capacity) new_capacity = size; T *new_ptr = this->allocate(new_capacity); // The following code doesn't throw, so the raw pointer above doesn't leak. std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_, - make_ptr(new_ptr, new_capacity)); + internal::make_ptr(new_ptr, new_capacity)); std::size_t old_capacity = this->capacity_; T *old_ptr = this->ptr_; this->capacity_ = new_capacity; @@ -739,6 +776,45 @@ void MemoryBuffer::grow(std::size_t size) { Allocator::deallocate(old_ptr, old_capacity); } +typedef basic_memory_buffer memory_buffer; +typedef basic_memory_buffer wmemory_buffer; + +/** + \rst + A fixed-size memory buffer. For a dynamically growing buffer use + :class:`fmt::basic_memory_buffer`. + + Trying to increase the buffer size past the initial capacity will throw + ``std::runtime_error``. + \endrst + */ +template +class FixedBuffer : public basic_buffer { + public: + /** + \rst + Constructs a :class:`fmt::FixedBuffer` object for *array* of the + given size. + \endrst + */ + FixedBuffer(Char *array, std::size_t size) + : basic_buffer(array, size) {} + + /** + \rst + Constructs a :class:`fmt::FixedBuffer` object for *array* of the + size known at compile time. + \endrst + */ + template + explicit FixedBuffer(Char (&array)[SIZE]) : basic_buffer(array, SIZE) {} + + protected: + FMT_API void grow(std::size_t size); +}; + +namespace internal { + template class BasicCharTraits { public: @@ -1047,7 +1123,7 @@ struct ConvertToInt { #define FMT_DISABLE_CONVERSION_TO_INT(Type) \ template <> \ - struct ConvertToInt { enum { value = 0 }; } + struct ConvertToInt { enum { value = 0 }; } // Silence warnings about convering float to int. FMT_DISABLE_CONVERSION_TO_INT(float); @@ -2762,108 +2838,6 @@ void basic_writer::write_double(T value, const format_specs &spec) { grow_buffer(n); } -/** - \rst - This class template provides operations for formatting and writing data - into a memory buffer that grows dynamically. - - You can use one of the following typedefs for common character types - and the standard allocator: - - +---------------+-----------------------------------------------------+ - | Type | Definition | - +===============+=====================================================+ - | MemoryWriter | BasicMemoryWriter> | - +---------------+-----------------------------------------------------+ - | WMemoryWriter | BasicMemoryWriter> | - +---------------+-----------------------------------------------------+ - - **Example**:: - - MemoryWriter out; - out << "The answer is " << 42 << "\n"; - out.write("({:+f}, {:+f})", -3.14, 3.14); - - This will write the following output to the ``out`` object: - - .. code-block:: none - - The answer is 42 - (-3.140000, +3.140000) - - The output can be converted to an ``std::string`` with ``out.str()`` or - accessed as a C string with ``out.c_str()``. - \endrst - */ -template > -class BasicMemoryWriter : public basic_writer { - private: - internal::MemoryBuffer buffer_; - - public: - explicit BasicMemoryWriter(const Allocator& alloc = Allocator()) - : basic_writer(buffer_), buffer_(alloc) {} - -#if FMT_USE_RVALUE_REFERENCES - /** - \rst - Constructs a :class:`fmt::BasicMemoryWriter` object moving the content - of the other object to it. - \endrst - */ - BasicMemoryWriter(BasicMemoryWriter &&other) - : basic_writer(buffer_), buffer_(std::move(other.buffer_)) { - } - - /** - \rst - Moves the content of the other ``BasicMemoryWriter`` object to this one. - \endrst - */ - BasicMemoryWriter &operator=(BasicMemoryWriter &&other) { - buffer_ = std::move(other.buffer_); - return *this; - } -#endif -}; - -typedef BasicMemoryWriter MemoryWriter; -typedef BasicMemoryWriter WMemoryWriter; - -/** - \rst - A fixed-size memory buffer. For a dynamically growing buffer use - :class:`fmt::internal::MemoryBuffer`. - - Trying to increase the buffer size past the initial capacity will throw - ``std::runtime_error``. - \endrst - */ -template -class FixedBuffer : public basic_buffer { - public: - /** - \rst - Constructs a :class:`fmt::FixedBuffer` object for *array* of the - given size. - \endrst - */ - FixedBuffer(Char *array, std::size_t size) - : basic_buffer(array, size) {} - - /** - \rst - Constructs a :class:`fmt::FixedBuffer` object for *array* of the - size known at compile time. - \endrst - */ - template - explicit FixedBuffer(Char (&array)[SIZE]) : basic_buffer(array, SIZE) {} - - protected: - FMT_API void grow(std::size_t size); -}; - // Reports a system error without throwing an exception. // Can be used to report errors from destructors. FMT_API void report_system_error(int error_code, @@ -2959,7 +2933,7 @@ inline void format_to(wbuffer &buf, WCStringRef format_str, } inline std::string vformat(CStringRef format_str, args args) { - internal::MemoryBuffer buffer; + memory_buffer buffer; vformat_to(buffer, format_str, args); return to_string(buffer); } @@ -2979,7 +2953,7 @@ inline std::string format(CStringRef format_str, const Args & ... args) { } inline std::wstring vformat(WCStringRef format_str, wargs args) { - internal::MemoryBuffer buffer; + wmemory_buffer buffer; vformat_to(buffer, format_str, args); return to_string(buffer); } diff --git a/fmt/ostream.cc b/fmt/ostream.cc index f900664c..f9136482 100644 --- a/fmt/ostream.cc +++ b/fmt/ostream.cc @@ -28,7 +28,7 @@ FMT_FUNC void write(std::ostream &os, buffer &buf) { } FMT_FUNC void vprint(std::ostream &os, CStringRef format_str, args args) { - internal::MemoryBuffer buffer; + memory_buffer buffer; vformat_to(buffer, format_str, args); internal::write(os, buffer); } diff --git a/fmt/ostream.h b/fmt/ostream.h index 45103734..8672ac6c 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -83,7 +83,7 @@ void format_value(basic_buffer &buffer, const T &value) { template void format_value(basic_buffer &buf, const T &value, basic_context &ctx) { - internal::MemoryBuffer buffer; + basic_memory_buffer buffer; internal::format_value(buffer, value); basic_string_view str(buffer.data(), buffer.size()); do_format_arg< arg_formatter >( diff --git a/fmt/printf.h b/fmt/printf.h index 0f3a200a..02c33fba 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -522,7 +522,7 @@ void printf(basic_buffer &buf, BasicCStringRef format, typedef basic_args> printf_args; inline std::string vsprintf(CStringRef format, printf_args args) { - internal::MemoryBuffer buffer; + memory_buffer buffer; printf(buffer, format, args); return to_string(buffer); } @@ -543,7 +543,7 @@ inline std::string sprintf(CStringRef format_str, const Args & ... args) { inline std::wstring vsprintf( WCStringRef format, basic_args> args) { - internal::MemoryBuffer buffer; + wmemory_buffer buffer; printf(buffer, format, args); return to_string(buffer); } @@ -590,7 +590,7 @@ inline int printf(CStringRef format_str, const Args & ... args) { } inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) { - internal::MemoryBuffer buffer; + memory_buffer buffer; printf(buffer, format_str, args); internal::write(os, buffer); return static_cast(buffer.size()); diff --git a/fmt/time.h b/fmt/time.h index f4ec4812..c4700fbc 100644 --- a/fmt/time.h +++ b/fmt/time.h @@ -24,7 +24,7 @@ void format_value(buffer &buf, const std::tm &tm, context &ctx) { ++end; if (*end != '}') FMT_THROW(format_error("missing '}' in format string")); - internal::MemoryBuffer format; + memory_buffer format; format.append(s, end + 1); format[format.size() - 1] = '\0'; std::size_t start = buf.size(); diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index c4d1f1e8..3e226d71 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -46,7 +46,7 @@ class CustomPrintfArgFormatter : public printf_arg_formatter { }; std::string custom_vformat(fmt::CStringRef format_str, fmt::args args) { - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; // Pass custom argument formatter as a template arg to vwrite. fmt::vformat_to(buffer, format_str, args); return std::string(buffer.data(), buffer.size()); @@ -64,7 +64,7 @@ typedef fmt::printf_context std::string custom_vsprintf( const char* format_str, fmt::basic_args args) { - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; CustomPrintfFormatter formatter(format_str, args); formatter.format(buffer); return std::string(buffer.data(), buffer.size()); diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 9102db03..f12b6450 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -105,33 +105,33 @@ TEST(FormatTest, StrError) { TEST(FormatTest, FormatErrorCode) { std::string msg = "error 42", sep = ": "; { - fmt::MemoryWriter w; - w.write("garbage"); - fmt::format_error_code(w.buffer(), 42, "test"); - EXPECT_EQ("test: " + msg, w.str()); + fmt::memory_buffer buffer; + format_to(buffer, "garbage"); + fmt::format_error_code(buffer, 42, "test"); + EXPECT_EQ("test: " + msg, to_string(buffer)); } { - fmt::MemoryWriter w; + fmt::memory_buffer buffer; std::string prefix( fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size() + 1, 'x'); - fmt::format_error_code(w.buffer(), 42, prefix); - EXPECT_EQ(msg, w.str()); + fmt::format_error_code(buffer, 42, prefix); + EXPECT_EQ(msg, to_string(buffer)); } int codes[] = {42, -1}; for (std::size_t i = 0, n = sizeof(codes) / sizeof(*codes); i < n; ++i) { // Test maximum buffer size. msg = fmt::format("error {}", codes[i]); - fmt::MemoryWriter w; + fmt::memory_buffer buffer; std::string prefix( fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size(), 'x'); - fmt::format_error_code(w.buffer(), codes[i], prefix); - EXPECT_EQ(prefix + sep + msg, w.str()); + fmt::format_error_code(buffer, codes[i], prefix); + EXPECT_EQ(prefix + sep + msg, to_string(buffer)); std::size_t size = fmt::internal::INLINE_BUFFER_SIZE; - EXPECT_EQ(size, w.size()); - w.clear(); + EXPECT_EQ(size, buffer.size()); + buffer.clear(); // Test with a message that doesn't fit into the buffer. prefix += 'x'; - fmt::format_error_code(w.buffer(), codes[i], prefix); - EXPECT_EQ(msg, w.str()); + fmt::format_error_code(buffer, codes[i], prefix); + EXPECT_EQ(msg, to_string(buffer)); } } diff --git a/test/format-test.cc b/test/format-test.cc index bf2b153e..2e1baaf2 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -75,8 +75,8 @@ using fmt::format; using fmt::format_error; using fmt::string_view; using fmt::CStringRef; -using fmt::MemoryWriter; -using fmt::WMemoryWriter; +using fmt::memory_buffer; +using fmt::wmemory_buffer; using fmt::fill; using fmt::type; using fmt::width; @@ -109,7 +109,8 @@ void std_format(long double value, std::wstring &result) { // as writing it to std::basic_ostringstream. template ::testing::AssertionResult check_write(const T &value, const char *type) { - fmt::BasicMemoryWriter writer; + fmt::basic_memory_buffer buffer; + fmt::basic_writer writer(buffer); writer.write(value); std::basic_string actual = writer.str(); std::basic_string expected; @@ -177,90 +178,16 @@ TEST(WriterTest, NotCopyAssignable) { #endif TEST(WriterTest, Ctor) { - MemoryWriter w; + memory_buffer buf; + fmt::basic_writer w(buf); EXPECT_EQ(0u, w.size()); EXPECT_STREQ("", w.c_str()); EXPECT_EQ("", w.str()); } -#if FMT_USE_RVALUE_REFERENCES - -void check_move_writer(const std::string &str, MemoryWriter &w) { - MemoryWriter w2(std::move(w)); - // Move shouldn't destroy the inline content of the first writer. - EXPECT_EQ(str, w.str()); - EXPECT_EQ(str, w2.str()); -} - -TEST(WriterTest, MoveCtor) { - MemoryWriter w; - w.write("test"); - check_move_writer("test", w); - // This fills the inline buffer, but doesn't cause dynamic allocation. - std::string s; - for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i) - s += '*'; - w.clear(); - w.write(s); - check_move_writer(s, w); - const char *inline_buffer_ptr = w.data(); - // Adding one more character causes the content to move from the inline to - // a dynamically allocated buffer. - w.write('*'); - MemoryWriter w2(std::move(w)); - // Move should rip the guts of the first writer. - EXPECT_EQ(inline_buffer_ptr, w.data()); - EXPECT_EQ(s + '*', w2.str()); -} - -void CheckMoveAssignWriter(const std::string &str, MemoryWriter &w) { - MemoryWriter w2; - w2 = std::move(w); - // Move shouldn't destroy the inline content of the first writer. - EXPECT_EQ(str, w.str()); - EXPECT_EQ(str, w2.str()); -} - -TEST(WriterTest, MoveAssignment) { - MemoryWriter w; - w.write("test"); - CheckMoveAssignWriter("test", w); - // This fills the inline buffer, but doesn't cause dynamic allocation. - std::string s; - for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i) - s += '*'; - w.clear(); - w.write(s); - CheckMoveAssignWriter(s, w); - const char *inline_buffer_ptr = w.data(); - // Adding one more character causes the content to move from the inline to - // a dynamically allocated buffer. - w.write('*'); - MemoryWriter w2; - w2 = std::move(w); - // Move should rip the guts of the first writer. - EXPECT_EQ(inline_buffer_ptr, w.data()); - EXPECT_EQ(s + '*', w2.str()); -} - -#endif // FMT_USE_RVALUE_REFERENCES - -TEST(WriterTest, Allocator) { - typedef testing::StrictMock< MockAllocator > MockAllocator; - typedef AllocatorRef TestAllocator; - MockAllocator alloc; - fmt::BasicMemoryWriter w((TestAllocator(&alloc))); - std::size_t size = - static_cast(1.5 * fmt::internal::INLINE_BUFFER_SIZE); - std::vector mem(size); - EXPECT_CALL(alloc, allocate(size)).WillOnce(testing::Return(&mem[0])); - for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE + 1; ++i) - w.write('*'); - EXPECT_CALL(alloc, deallocate(&mem[0], size)); -} - TEST(WriterTest, Data) { - MemoryWriter w; + memory_buffer buf; + fmt::basic_writer w(buf); w.write(42); EXPECT_EQ("42", std::string(w.data(), w.size())); } @@ -312,13 +239,15 @@ TEST(WriterTest, WriteLongDouble) { } TEST(WriterTest, WriteDoubleAtBufferBoundary) { - MemoryWriter writer; + memory_buffer buf; + fmt::basic_writer writer(buf); for (int i = 0; i < 100; ++i) writer.write(1.23456789); } TEST(WriterTest, WriteDoubleWithFilledBuffer) { - MemoryWriter writer; + memory_buffer buf; + fmt::basic_writer writer(buf); // Fill the buffer. for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i) writer.write(' '); @@ -349,7 +278,8 @@ TEST(WriterTest, WriteWideString) { template std::string write_str(T... args) { - MemoryWriter writer; + memory_buffer buf; + fmt::basic_writer writer(buf); using namespace fmt; writer.write(args...); return writer.str(); @@ -357,7 +287,8 @@ std::string write_str(T... args) { template std::wstring write_wstr(T... args) { - WMemoryWriter writer; + wmemory_buffer buf; + fmt::basic_writer writer(buf); using namespace fmt; writer.write(args...); return writer.str(); @@ -447,7 +378,8 @@ TEST(WriterTest, pad) { EXPECT_EQ(" 33", write_str(33ll, width=7)); EXPECT_EQ(" 44", write_str(44ull, width=7)); - MemoryWriter w; + memory_buffer buf; + fmt::basic_writer w(buf); w.clear(); w.write(42, fmt::width=5, fmt::fill='0'); EXPECT_EQ("00042", w.str()); @@ -475,13 +407,13 @@ TEST(WriterTest, WWriter) { } TEST(FormatToTest, FormatWithoutArgs) { - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; format_to(buffer, "test"); EXPECT_EQ("test", std::string(buffer.data(), buffer.size())); } TEST(FormatToTest, Format) { - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; format_to(buffer, "part{0}", 1); EXPECT_EQ(strlen("part1"), buffer.size()); EXPECT_EQ("part1", std::string(buffer.data(), buffer.size())); @@ -1370,18 +1302,9 @@ TEST(FormatterTest, FormatExamples) { EXPECT_EQ("42", format("{}", 42)); EXPECT_EQ("42", format(std::string("{}"), 42)); - MemoryWriter out; - out.write("The answer is "); - out.write(42); - out.write("\n"); - - { - MemoryWriter writer; - for (int i = 0; i < 10; i++) - format_to(writer.buffer(), "{}", i); - std::string s = writer.str(); // s == 0123456789 - EXPECT_EQ("0123456789", s); - } + memory_buffer out; + format_to(out, "The answer is {}.", 42); + EXPECT_EQ("The answer is 42.", to_string(out)); const char *filename = "nonexistent"; FILE *ftest = safe_fopen(filename, "r"); @@ -1522,7 +1445,7 @@ TEST(StrTest, Convert) { } std::string vformat_message(int id, const char *format, fmt::args args) { - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; format_to(buffer, "[{}] ", id); vformat_to(buffer, format, args); return to_string(buffer); @@ -1612,7 +1535,7 @@ class MockArgFormatter : public fmt::internal::ArgFormatterBase { }; void custom_vformat(fmt::CStringRef format_str, fmt::args args) { - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; fmt::vformat_to(buffer, format_str, args); } diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 4de17ee3..97c4bee1 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -319,9 +319,9 @@ TEST(StreamingAssertionsTest, EXPECT_WRITE) { } TEST(UtilTest, FormatSystemError) { - fmt::MemoryWriter out; - fmt::format_system_error(out.buffer(), EDOM, "test message"); - EXPECT_EQ(out.str(), format_system_error(EDOM, "test message")); + fmt::memory_buffer out; + fmt::format_system_error(out, EDOM, "test message"); + EXPECT_EQ(to_string(out), format_system_error(EDOM, "test message")); } #if FMT_USE_FILE_DESCRIPTORS diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 325216d8..34daf4db 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -104,7 +104,7 @@ std::string read(File &f, std::size_t count) { #endif // FMT_USE_FILE_DESCRIPTORS std::string format_system_error(int error_code, fmt::string_view message) { - fmt::internal::MemoryBuffer out; - fmt::format_system_error(out, error_code, message); + fmt::memory_buffer out; + format_system_error(out, error_code, message); return to_string(out); } diff --git a/test/ostream-test.cc b/test/ostream-test.cc index c28635b7..2ab22248 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -64,7 +64,7 @@ struct TestArgFormatter : fmt::arg_formatter { }; TEST(OStreamTest, CustomArg) { - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; fmt::context ctx("}", fmt::args()); fmt::format_specs spec; TestArgFormatter af(buffer, ctx, spec); @@ -121,7 +121,7 @@ TEST(OStreamTest, Print) { TEST(OStreamTest, WriteToOStream) { std::ostringstream os; - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; const char *foo = "foo"; buffer.append(foo, foo + std::strlen(foo)); fmt::internal::write(os, buffer); diff --git a/test/util-test.cc b/test/util-test.cc index 5978d914..f9ca8e5a 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -54,8 +54,8 @@ using fmt::basic_arg; using fmt::basic_buffer; +using fmt::basic_memory_buffer; using fmt::string_view; -using fmt::internal::MemoryBuffer; using fmt::internal::value; using testing::_; @@ -238,7 +238,7 @@ TEST(BufferTest, AppendAllocatesEnoughStorage) { } TEST(MemoryBufferTest, Ctor) { - MemoryBuffer buffer; + basic_memory_buffer buffer; EXPECT_EQ(0u, buffer.size()); EXPECT_EQ(123u, buffer.capacity()); } @@ -248,9 +248,9 @@ TEST(MemoryBufferTest, Ctor) { typedef AllocatorRef< std::allocator > TestAllocator; void check_move_buffer(const char *str, - MemoryBuffer &buffer) { + basic_memory_buffer &buffer) { std::allocator *alloc = buffer.get_allocator().get(); - MemoryBuffer buffer2(std::move(buffer)); + basic_memory_buffer buffer2(std::move(buffer)); // Move shouldn't destroy the inline content of the first buffer. EXPECT_EQ(str, std::string(&buffer[0], buffer.size())); EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size())); @@ -262,7 +262,7 @@ void check_move_buffer(const char *str, TEST(MemoryBufferTest, MoveCtor) { std::allocator alloc; - MemoryBuffer buffer((TestAllocator(&alloc))); + basic_memory_buffer buffer((TestAllocator(&alloc))); const char test[] = "test"; buffer.append(test, test + 4); check_move_buffer("test", buffer); @@ -274,15 +274,16 @@ TEST(MemoryBufferTest, MoveCtor) { // Adding one more character causes the content to move from the inline to // a dynamically allocated buffer. buffer.push_back('b'); - MemoryBuffer buffer2(std::move(buffer)); + basic_memory_buffer buffer2(std::move(buffer)); // Move should rip the guts of the first buffer. EXPECT_EQ(inline_buffer_ptr, &buffer[0]); EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size())); EXPECT_GT(buffer2.capacity(), 5u); } -void check_move_assign_buffer(const char *str, MemoryBuffer &buffer) { - MemoryBuffer buffer2; +void check_move_assign_buffer( + const char *str, basic_memory_buffer &buffer) { + basic_memory_buffer buffer2; buffer2 = std::move(buffer); // Move shouldn't destroy the inline content of the first buffer. EXPECT_EQ(str, std::string(&buffer[0], buffer.size())); @@ -291,7 +292,7 @@ void check_move_assign_buffer(const char *str, MemoryBuffer &buffer) { } TEST(MemoryBufferTest, MoveAssignment) { - MemoryBuffer buffer; + basic_memory_buffer buffer; const char test[] = "test"; buffer.append(test, test + 4); check_move_assign_buffer("test", buffer); @@ -303,7 +304,7 @@ TEST(MemoryBufferTest, MoveAssignment) { // Adding one more character causes the content to move from the inline to // a dynamically allocated buffer. buffer.push_back('b'); - MemoryBuffer buffer2; + basic_memory_buffer buffer2; buffer2 = std::move(buffer); // Move should rip the guts of the first buffer. EXPECT_EQ(inline_buffer_ptr, &buffer[0]); @@ -315,7 +316,7 @@ TEST(MemoryBufferTest, MoveAssignment) { TEST(MemoryBufferTest, Grow) { typedef AllocatorRef< MockAllocator > Allocator; - typedef MemoryBuffer Base; + typedef basic_memory_buffer Base; MockAllocator alloc; struct TestMemoryBuffer : Base { TestMemoryBuffer(Allocator alloc) : Base(alloc) {} @@ -341,12 +342,12 @@ TEST(MemoryBufferTest, Grow) { TEST(MemoryBufferTest, Allocator) { typedef AllocatorRef< MockAllocator > TestAllocator; - MemoryBuffer buffer; + basic_memory_buffer buffer; EXPECT_EQ(0, buffer.get_allocator().get()); StrictMock< MockAllocator > alloc; char mem; { - MemoryBuffer buffer2((TestAllocator(&alloc))); + basic_memory_buffer buffer2((TestAllocator(&alloc))); EXPECT_EQ(&alloc, buffer2.get_allocator().get()); std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE; EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem)); @@ -358,7 +359,7 @@ TEST(MemoryBufferTest, Allocator) { TEST(MemoryBufferTest, ExceptionInDeallocate) { typedef AllocatorRef< MockAllocator > TestAllocator; StrictMock< MockAllocator > alloc; - MemoryBuffer buffer((TestAllocator(&alloc))); + basic_memory_buffer buffer((TestAllocator(&alloc))); std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE; std::vector mem(size); { @@ -435,7 +436,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) { ::Test t; fmt::internal::value arg(t); CustomContext ctx = {false}; - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; arg.custom.format(buffer, &t, &ctx); EXPECT_TRUE(ctx.called); } @@ -579,7 +580,7 @@ TEST(UtilTest, CustomArg) { EXPECT_CALL(visitor, visit(_)).WillOnce( testing::Invoke([&](fmt::internal::CustomValue custom) { EXPECT_EQ(&test, custom.value); - fmt::internal::MemoryBuffer buffer; + fmt::memory_buffer buffer; fmt::context ctx("}", fmt::args()); custom.format(buffer, &test, &ctx); EXPECT_EQ("test", std::string(buffer.data(), buffer.size())); @@ -710,14 +711,14 @@ void check_throw_error(int error_code, FormatErrorMessage format) { } catch (const fmt::SystemError &e) { error = e; } - fmt::internal::MemoryBuffer message; + fmt::memory_buffer message; format(message, error_code, "test error"); EXPECT_EQ(to_string(message), error.what()); EXPECT_EQ(error_code, error.error_code()); } TEST(UtilTest, FormatSystemError) { - fmt::internal::MemoryBuffer message; + fmt::memory_buffer message; fmt::format_system_error(message, EDOM, "test"); EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), to_string(message)); @@ -735,7 +736,7 @@ TEST(UtilTest, SystemError) { } TEST(UtilTest, ReportSystemError) { - fmt::internal::MemoryBuffer out; + fmt::memory_buffer out; fmt::format_system_error(out, EDOM, "test error"); out.push_back('\n'); EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"),