-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add support for fixed sized buffers #105
Comments
Thank you for the suggestion. The library already doesn't do dynamic memory allocations if the result of formatting is less than fmt::MemoryWriter w;
w.write("{}/{}", dir, filename); // no dynamic memory allocation for small output
w.c_str(); // returns a pointer to the internal buffer The advantage of this method is that it works with large output by using dynamic memory allocation (only) if necessary. Also you can define your own function (or a writer class) that prints directly to a fixed buffer: #include "format.h"
class FixedBuffer : public fmt::internal::Buffer<char> {
public:
FixedBuffer(char *array, std::size_t size)
: fmt::internal::Buffer<char>(array, size) {}
protected:
void grow(std::size_t size) {
throw std::runtime_error("buffer overflow");
}
};
class FixedWriter : public fmt::Writer {
private:
FixedBuffer buffer_;
public:
FixedWriter(char *array, std::size_t size)
: fmt::Writer(buffer_), buffer_(array, size) {}
};
int main() {
char buffer[100];
FixedWriter w(buffer, sizeof(buffer));
w.write("Hello, {}!", "world");
} |
Closing for now, but feel free to reopen if the alternatives I described don't fit your use cases. |
@vitaut So fmt::internal::INLINE_BUFFER_SIZE should and can only be configured by deriving my own class like in your example above? Otherwise how is it defined? |
@patlecat No, you can change the value of |
C++ Format now supports formatting to fixed-size arrays: http://cppformat.readthedocs.org/en/latest/reference.html#fmt::BasicArrayWriter. Example:
|
Does this represent any performance gain in any way or is it just a convenience for C code? |
There is no performance gain. It's mostly for the case where you can't replace the destination array with |
This library looks very promising. One thing that's missing is efficient snprintf() like functionality which can print directly to a fixed size buffer with truncation but without any memory allocations.
I would suggest an interface like this:
In this example,
ArrayRef
would be a simplified version ofarray_view
which is proposed for C++17. You don't need all of the multi-dimensional and striding support, just a generic adapter for any array type container.http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3851.pdf
The returned
StringRef
references a sub-array of buffer that was filled in by the formatting.Usage example:
The text was updated successfully, but these errors were encountered: