diff --git a/fmt/format.h b/fmt/format.h index 5b8f5bd0..fff6adfa 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -586,7 +586,7 @@ inline T *make_ptr(T *ptr, std::size_t) { return ptr; } /** \rst - A buffer supporting a subset of ``std::vector``'s operations. + A contiguous memory buffer with an optional growing ability. \endrst */ template @@ -2843,9 +2843,17 @@ class BasicMemoryWriter : public basic_writer { typedef BasicMemoryWriter MemoryWriter; typedef BasicMemoryWriter WMemoryWriter; -// A fixed-size buffer. +/** + \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 fmt::basic_buffer { +class FixedBuffer : public basic_buffer { public: /** \rst @@ -2854,54 +2862,20 @@ class FixedBuffer : public fmt::basic_buffer { \endrst */ FixedBuffer(Char *array, std::size_t size) - : fmt::basic_buffer(array, size) {} - - protected: - FMT_API void grow(std::size_t size); -}; - -/** - \rst - This class template provides operations for formatting and writing data - into a fixed-size array. For writing into a dynamically growing buffer - use :class:`fmt::BasicMemoryWriter`. - - Any write method will throw ``std::runtime_error`` if the output doesn't fit - into the array. - - You can use one of the following typedefs for common character types: - - +--------------+---------------------------+ - | Type | Definition | - +==============+===========================+ - | ArrayWriter | BasicArrayWriter | - +--------------+---------------------------+ - | WArrayWriter | BasicArrayWriter | - +--------------+---------------------------+ - \endrst - */ -template -class BasicArrayWriter : public basic_writer { - private: - FixedBuffer buffer_; - - public: - BasicArrayWriter(Char *array, std::size_t size) - : basic_writer(buffer_), buffer_(array, size) {} + : basic_buffer(array, size) {} /** \rst - Constructs a :class:`fmt::BasicArrayWriter` object for *array* of the + Constructs a :class:`fmt::FixedBuffer` object for *array* of the size known at compile time. \endrst */ template - explicit BasicArrayWriter(Char (&array)[SIZE]) - : basic_writer(buffer_), buffer_(array, SIZE) {} -}; + explicit FixedBuffer(Char (&array)[SIZE]) : basic_buffer(array, SIZE) {} -typedef BasicArrayWriter ArrayWriter; -typedef BasicArrayWriter WArrayWriter; + 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. diff --git a/test/format-test.cc b/test/format-test.cc index 02b99fd1..56ea7136 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -474,13 +474,6 @@ TEST(WriterTest, WWriter) { EXPECT_EQ(L"cafe", write_wstr(0xcafe, type='x')); } -TEST(ArrayWriterTest, Ctor) { - char array[10] = "garbage"; - fmt::ArrayWriter w(array, sizeof(array)); - EXPECT_EQ(0u, w.size()); - EXPECT_STREQ("", w.c_str()); -} - TEST(FormatToTest, FormatWithoutArgs) { fmt::internal::MemoryBuffer buffer; format_to(buffer, "test"); @@ -498,29 +491,6 @@ TEST(FormatToTest, Format) { EXPECT_EQ("part1part2", to_string(buffer)); } -TEST(ArrayWriterTest, CompileTimeSizeCtor) { - char array[10] = "garbage"; - fmt::ArrayWriter w(array); - EXPECT_EQ(0u, w.size()); - EXPECT_STREQ("", w.c_str()); - format_to(w.buffer(), "{:10}", 1); -} - -TEST(ArrayWriterTest, BufferOverflow) { - char array[10]; - fmt::ArrayWriter w(array, sizeof(array)); - format_to(w.buffer(), "{:10}", 1); - EXPECT_THROW_MSG(format_to(w.buffer(), "{}", 1), std::runtime_error, - "buffer overflow"); -} - -TEST(ArrayWriterTest, WChar) { - wchar_t array[10]; - fmt::WArrayWriter w(array); - format_to(w.buffer(), L"{}", 42); - EXPECT_EQ(L"42", w.str()); -} - TEST(FormatterTest, Escape) { EXPECT_EQ("{", format("{{")); EXPECT_EQ("before {", format("before {{")); diff --git a/test/util-test.cc b/test/util-test.cc index 9466c372..ab149555 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -213,19 +213,6 @@ TEST(BufferTest, Clear) { EXPECT_EQ(20u, buffer.capacity()); } -TEST(BufferTest, PushBack) { - int data[15]; - MockBuffer buffer(data, 10); - buffer.push_back(11); - EXPECT_EQ(11, buffer[0]); - EXPECT_EQ(1u, buffer.size()); - buffer.resize(10); - EXPECT_CALL(buffer, do_grow(11)); - buffer.push_back(22); - EXPECT_EQ(22, buffer[10]); - EXPECT_EQ(11u, buffer.size()); -} - TEST(BufferTest, Append) { char data[15]; MockBuffer buffer(data, 10); @@ -393,6 +380,29 @@ TEST(MemoryBufferTest, ExceptionInDeallocate) { EXPECT_CALL(alloc, deallocate(&mem2[0], 2 * size)); } +TEST(FixedBufferTest, Ctor) { + char array[10] = "garbage"; + fmt::FixedBuffer buffer(array, sizeof(array)); + EXPECT_EQ(0u, buffer.size()); + EXPECT_EQ(10u, buffer.capacity()); + EXPECT_EQ(array, buffer.data()); +} + +TEST(FixedBufferTest, CompileTimeSizeCtor) { + char array[10] = "garbage"; + fmt::FixedBuffer buffer(array); + EXPECT_EQ(0u, buffer.size()); + EXPECT_EQ(10u, buffer.capacity()); + EXPECT_EQ(array, buffer.data()); +} + +TEST(FixedBufferTest, BufferOverflow) { + char array[10]; + fmt::FixedBuffer buffer(array); + buffer.resize(10); + EXPECT_THROW_MSG(buffer.resize(11), std::runtime_error, "buffer overflow"); +} + TEST(UtilTest, Increment) { char s[10] = "123"; increment(s);