Merge ArrayWriter into FixedBuffer

This commit is contained in:
Victor Zverovich 2017-02-14 20:12:39 -05:00
parent fefaf07b6f
commit c2f021692f
3 changed files with 40 additions and 86 deletions

View File

@ -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 <typename T>
@ -2843,9 +2843,17 @@ class BasicMemoryWriter : public basic_writer<Char> {
typedef BasicMemoryWriter<char> MemoryWriter;
typedef BasicMemoryWriter<wchar_t> 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 <typename Char>
class FixedBuffer : public fmt::basic_buffer<Char> {
class FixedBuffer : public basic_buffer<Char> {
public:
/**
\rst
@ -2854,54 +2862,20 @@ class FixedBuffer : public fmt::basic_buffer<Char> {
\endrst
*/
FixedBuffer(Char *array, std::size_t size)
: fmt::basic_buffer<Char>(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<char> |
+--------------+---------------------------+
| WArrayWriter | BasicArrayWriter<wchar_t> |
+--------------+---------------------------+
\endrst
*/
template <typename Char>
class BasicArrayWriter : public basic_writer<Char> {
private:
FixedBuffer<Char> buffer_;
public:
BasicArrayWriter(Char *array, std::size_t size)
: basic_writer<Char>(buffer_), buffer_(array, size) {}
: basic_buffer<Char>(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 <std::size_t SIZE>
explicit BasicArrayWriter(Char (&array)[SIZE])
: basic_writer<Char>(buffer_), buffer_(array, SIZE) {}
};
explicit FixedBuffer(Char (&array)[SIZE]) : basic_buffer<Char>(array, SIZE) {}
typedef BasicArrayWriter<char> ArrayWriter;
typedef BasicArrayWriter<wchar_t> 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.

View File

@ -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<char> 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 {{"));

View File

@ -213,19 +213,6 @@ TEST(BufferTest, Clear) {
EXPECT_EQ(20u, buffer.capacity());
}
TEST(BufferTest, PushBack) {
int data[15];
MockBuffer<int> 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<char> 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<char> 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<char> 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<char> 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);