-
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
A tiny bit closer to the STL? #140
Comments
You can provide a buffer for writing directly into your custom container. Here's an example of a buffer for writing into #include "format.h"
class StringBuffer : public fmt::internal::Buffer<char> {
private:
std::string buffer_;
protected:
void grow(std::size_t size) {
buffer_.resize(size);
ptr_ = &buffer_[0];
capacity_ = size;
}
public:
std::string &str() { return buffer_; }
};
class StringWriter : public fmt::Writer {
private:
StringBuffer buffer_;
public:
StringWriter() : fmt::Writer(buffer_) {}
std::string &str() { return buffer_.str(); }
};
int main() {
StringWriter w;
w.write("Hello, {}!", "world");
w.str(); // get the output string
} Currently this requires deriving from an internal class ( Hope this works for you. As for supporting general iterators, it will require some modification to the library and I'm not sure that it's worth the trouble as buffers are usually represented by contiguous regions of memory. |
I followed your example and it worked like a charm. I also implemented a I can't tell if you should move Kudos for writing C++ Format the way you wrote it. It offers the features I was missing with other libraries, it's easy to integrate and a breeze to use. Here are the final helper classes in case someone stumble upon the same requirements: EDIT: this is an even more generic way so one can optionally append data to the container instead of discarding existing data.
|
Thanks for the nice feedback. I made Preliminary documentation is available here: http://cppformat.github.io/dev/api.html#_CPPv2N3fmt6BufferE. |
I defined a custom STL container derived from
std::basic_string<CharT>
. Some quick digging into cppformat's source showed that I cannot ask cppformat to write directly into my container, requiring me to use a BasicWriter derived instance (i.e.:BasicMemoryWriter
).Every options I found or could imagine requires the use of a temporary buffer that will finally be copied into my container. I also investigated the "derivate BasicWriter" option in order to achieve that but it isn't clear to me what method(s) I would have to override. Several it appears.
I absolutely have no clue of the feasibility/complexity of this but since it seems cppformat just write/append data to the destination container, so an additional formatting function that accepts a
std::back_inserter
iterator would definitely be nice. Something like:As an example, the utf8-cpp library uses this technique and it is quite clean and straightforward.
Or maybe is there a clean way to derivate
BasicWriter
or other? I would be happy to submit a patch.Thanks for your utterly useful lib anyway :)
The text was updated successfully, but these errors were encountered: