-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BasicContainerWriter utility added #450
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Formatting library for C++ - standard container utilities | ||
|
||
Copyright (c) 2012 - 2016, Victor Zverovich | ||
All rights reserved. | ||
|
||
For the license information refer to format.h. | ||
*/ | ||
|
||
#ifndef FMT_CONTAINER_H_ | ||
#define FMT_CONTAINER_H_ | ||
|
||
#include "format.h" | ||
|
||
namespace fmt { | ||
|
||
namespace internal { | ||
|
||
/** | ||
\rst | ||
A "buffer" that appends data to a standard container (e.g. typically a | ||
``std::vector`` or ``std::basic_string``). | ||
\endrst | ||
*/ | ||
template <typename Container> | ||
class ContainerBuffer : public Buffer<typename Container::value_type> { | ||
private: | ||
Container& container_; | ||
|
||
protected: | ||
virtual void grow(std::size_t size) FMT_OVERRIDE { | ||
container_.resize(size); | ||
this->ptr_ = &container_[0]; | ||
this->capacity_ = size; | ||
} | ||
|
||
public: | ||
explicit ContainerBuffer(Container& container) : container_(container) { | ||
this->size_ = container_.size(); | ||
if (this->size_ > 0) { | ||
this->ptr_ = &container_[0]; | ||
this->capacity_ = this->size_; | ||
} | ||
} | ||
}; | ||
} // namespace internal | ||
|
||
/** | ||
\rst | ||
This class template provides operations for formatting and appending data | ||
to a standard *container* like ``std::vector`` or ``std::basic_string``. | ||
|
||
**Example**:: | ||
|
||
void vecformat(std::vector<char>& dest, fmt::BasicCStringRef<char> format, | ||
fmt::ArgList args) { | ||
fmt::BasicContainerWriter<std::vector<char> > appender(dest); | ||
appender.write(format, args); | ||
} | ||
FMT_VARIADIC(void, vecformat, std::vector<char>&, | ||
fmt::BasicCStringRef<char>); | ||
\endrst | ||
*/ | ||
template <class Container> | ||
class BasicContainerWriter | ||
: public BasicWriter<typename Container::value_type> { | ||
private: | ||
internal::ContainerBuffer<Container> buffer_; | ||
|
||
public: | ||
/** | ||
\rst | ||
Constructs a :class:`fmt::BasicContainerWriter` object. | ||
\endrst | ||
*/ | ||
explicit BasicContainerWriter(Container& dest) | ||
: BasicWriter<typename Container::value_type>(buffer_), buffer_(dest) {} | ||
}; | ||
|
||
} // namespace fmt | ||
|
||
#endif // FMT_CONTAINER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Tests of container utilities | ||
|
||
Copyright (c) 2012 - 2016, Victor Zverovich | ||
All rights reserved. | ||
|
||
For the license information refer to format.h. | ||
*/ | ||
|
||
#include "fmt/container.h" | ||
#include "gtest/gtest.h" | ||
|
||
using fmt::internal::ContainerBuffer; | ||
|
||
TEST(ContainerBufferTest, Empty) { | ||
std::string data; | ||
ContainerBuffer<std::string> buffer(data); | ||
EXPECT_EQ(0u, buffer.size()); | ||
EXPECT_EQ(0u, buffer.capacity()); | ||
} | ||
|
||
TEST(ContainerBufferTest, Reserve) { | ||
std::string data; | ||
ContainerBuffer<std::string> buffer(data); | ||
std::size_t capacity = std::string().capacity() + 10; | ||
buffer.reserve(capacity); | ||
EXPECT_EQ(0u, buffer.size()); | ||
EXPECT_EQ(capacity, buffer.capacity()); | ||
} | ||
|
||
TEST(ContainerBufferTest, Resize) { | ||
std::string data; | ||
ContainerBuffer<std::string> buffer(data); | ||
std::size_t size = std::string().capacity() + 10; | ||
buffer.resize(size); | ||
EXPECT_EQ(size, buffer.size()); | ||
EXPECT_EQ(size, buffer.capacity()); | ||
} | ||
|
||
TEST(ContainerBufferTest, Append) { | ||
std::string data("Why so"); | ||
const std::string serious(" serious"); | ||
ContainerBuffer<std::string> buffer(data); | ||
buffer.append(serious.c_str(), serious.c_str() + serious.length()); | ||
EXPECT_EQ("Why so serious", data); | ||
EXPECT_EQ(data.length(), buffer.size()); | ||
} | ||
|
||
TEST(BasicContainerWriterTest, String) { | ||
std::string data; | ||
fmt::BasicContainerWriter<std::string> out(data); | ||
out << "The answer is " << 42 << "\n"; | ||
EXPECT_EQ("The answer is 42\n", data); | ||
EXPECT_EQ(17u, out.size()); | ||
} | ||
|
||
TEST(BasicContainerWriterTest, WString) { | ||
std::wstring data; | ||
fmt::BasicContainerWriter<std::wstring> out(data); | ||
out << "The answer is " << 42 << "\n"; | ||
EXPECT_EQ(L"The answer is 42\n", data); | ||
EXPECT_EQ(17u, out.size()); | ||
} | ||
|
||
TEST(BasicContainerWriterTest, Vector) { | ||
std::vector<char> data; | ||
fmt::BasicContainerWriter<std::vector<char> > out(data); | ||
out << "The answer is " << 42 << "\n"; | ||
EXPECT_EQ(17u, data.size()); | ||
EXPECT_EQ(out.size(), data.size()); | ||
} | ||
|
||
TEST(BasicContainerWriterTest, StringAppend) { | ||
std::string data("The"); | ||
fmt::BasicContainerWriter<std::string> out(data); | ||
EXPECT_EQ(3u, data.size()); | ||
EXPECT_EQ(3u, out.size()); | ||
out << " answer is " << 42 << "\n"; | ||
EXPECT_EQ("The answer is 42\n", data); | ||
EXPECT_EQ(17u, out.size()); | ||
} | ||
|
||
TEST(BasicContainerWriterTest, VectorAppend) { | ||
std::vector<char> data; | ||
data.push_back('T'); | ||
data.push_back('h'); | ||
data.push_back('e'); | ||
fmt::BasicContainerWriter<std::vector<char> > out(data); | ||
EXPECT_EQ(3u, data.size()); | ||
EXPECT_EQ(3u, out.size()); | ||
out << " answer is " << 42 << "\n"; | ||
EXPECT_EQ(17u, data.size()); | ||
EXPECT_EQ(17u, out.size()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like capacity will be uninitialized if size is zero. Could you add a test case that catches this? Also I'd omit the zero check altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it compile? =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of the test is to avoid assuming that it is safe to initialize
ptr_
(thus assuming&container_[0]
is always safe) before being sure it is (i.e. after thecontainer_.resize()
call). Although admittedly it should be fine with standard containers with most compilers, but we cannot assumeContainer
type is standard here, or derived from a standard.Doesn't
fmt::Buffer
's default constructor setcapacity_
to zero?You mean like
TEST(BasicContainerWriterTest, String)
, which already tests that case unless I misunderstood you, but explicitly forContainerBuffer
?The corrected example compiles on MSVC2015 SP3. Tested before committing of course :)
I cannot confirm it compiles just fine on other configs though, or that it is even C++98 compliant, like it's supposed to be for fmt. Do note however I followed the same example pattern found in the documentation of the
FMT_VARIADIC
macro, which has avoid
return type as well.Also, please note MSVC2015 SP3 is the only compiler I have access to ATM since I have quite limited access and bandwidth to Internet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good points. Thanks!