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
I cannot find a way to do this, so I suppose it is a feature request (unless I miss something).
I want to have a static_string allocated, then write some data into it via fmt::format_to_n (or you could imagine snprintf, etc), and then tell it "here is your new length." I am trying to avoid an extra copy.
eg:
static_string<10> str;
auto ret = fmt::format_to_n(
str.data(),
str.capacity(),
"foo {}"
123);
auto length = ret.out - str.data();
str.set_length_to(length); // this function does not exist
I cannot use resize() because that will stomp the data with char(), which is 0.
Note: one alternative is to pass an insertion iterator, like so:
fmt::format_to_n(
std::back_inserter(str),
...);
However, this repeatedly calls push_back on the string, which repeatedly null-terminates the data unnecessarily.
Only the final null terminator matters, so the others are wasted work.
Or maybe there is a way to do this already, which I am missing?
The text was updated successfully, but these errors were encountered:
Currently, there is no way to do this. However, in Boost.JSON we permit access to characters in the range [size(), capacity()) for this exact purpose -- @vinniefalco@pdimov thoughts?
Not strictly related to static_string, but the design of resize_and_overwrite is defective because of how it mandates an inversion of the flow of control. It makes some useful idioms impossible to express, in favor of "safety no matter what" (which was never a core C++ principle).
I cannot find a way to do this, so I suppose it is a feature request (unless I miss something).
I want to have a
static_string
allocated, then write some data into it viafmt::format_to_n
(or you could imaginesnprintf
, etc), and then tell it "here is your new length." I am trying to avoid an extra copy.eg:
I cannot use
resize()
because that will stomp the data withchar()
, which is0
.Note: one alternative is to pass an insertion iterator, like so:
However, this repeatedly calls
push_back
on the string, which repeatedly null-terminates the data unnecessarily.Only the final null terminator matters, so the others are wasted work.
Or maybe there is a way to do this already, which I am missing?
The text was updated successfully, but these errors were encountered: