You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
In this example, ArrayRef would be a simplified version of array_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:
char path_buf[4096];
auto path = format(path_buf, "{}/{}", dir, filename);
Thank you for the suggestion. The library already doesn't do dynamic memory allocations if the result of formatting is less than fmt::internal::INLINE_BUFFER_SIZE (which you can configure to better suit your application):
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:
@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 fmt::internal::INLINE_BUFFER_SIZE constant if you embed C++ Format in your application (although in most cases the default should be fine). Deriving your own class is only necessary if you want to support a custom buffer, e.g. fixed-sized as in the above example.
Activity
vitaut commentedon Feb 9, 2015
Thank you for the suggestion. The library already doesn't do dynamic memory allocations if the result of formatting is less than
fmt::internal::INLINE_BUFFER_SIZE(which you can configure to better suit your application):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:
vitaut commentedon Feb 10, 2015
Closing for now, but feel free to reopen if the alternatives I described don't fit your use cases.
patlecat commentedon Feb 17, 2015
@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?
vitaut commentedon Feb 17, 2015
@patlecat No, you can change the value of
fmt::internal::INLINE_BUFFER_SIZEconstant if you embed C++ Format in your application (although in most cases the default should be fine). Deriving your own class is only necessary if you want to support a custom buffer, e.g. fixed-sized as in the above example.vitaut commentedon Mar 2, 2015
C++ Format now supports formatting to fixed-size arrays: http://cppformat.readthedocs.org/en/latest/reference.html#fmt::BasicArrayWriter.
Example:
patlecat commentedon Mar 2, 2015
Does this represent any performance gain in any way or is it just a convenience for C code?
vitaut commentedon Mar 2, 2015
There is no performance gain. It's mostly for the case where you can't replace the destination array with
MemoryWriter.