-
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
Issues due to removal of ArrayWriter #764
Comments
#include "fmt/format.h"
template <typename OutputIt>
class fixed_iterator {
private:
typedef std::iterator_traits<OutputIt> traits;
OutputIt out_;
std::size_t limit_;
std::size_t count_;
public:
typedef std::output_iterator_tag iterator_category;
typedef typename traits::value_type value_type;
typedef typename traits::difference_type difference_type;
typedef typename traits::pointer pointer;
typedef typename traits::reference reference;
fixed_iterator(OutputIt out, std::size_t limit)
: out_(out), limit_(limit), count_(0) {}
OutputIt base() const { return out_; }
std::size_t count() const { return count_; }
fixed_iterator& operator++() {
if (count_++ < limit_)
++out_;
return *this;
}
fixed_iterator operator++(int) {
auto it = *this;
++*this;
return it;
}
reference operator*() const {
if (count_ >= limit_)
throw std::runtime_error("end of buffer");
return *out_;
}
};
int main() {
char buff[1024];
auto it = fmt::format_to(fixed_iterator<char*>(buff, sizeof(buff)), "{}, ", 42);
bool condition1 = true, condition2 = true;
if (condition1)
it = fmt::format_to(it, "{}, ", .42);
if (condition2)
it = fmt::format_to(it, "{}, ", "string-blah");
// truncates trash
*it = 0;
fmt::print("{}\n", buff);
// buff contains: "42, .42, string-blah, ";
}
This is just a matter of adding a new |
Thank you for your help, To add more overload functions to make fmt::format_to_n() supporting wchar_t type, I have tried as below:
They are almost same as the version for char. Unfortunately, they do not work.
|
I added an overload of Re adding something like |
With your new update, format_to_n() works with wchar_t like a charm. Thanks for your effort. Now I am able to simulate two "legacy" classes: BasicArrayWriter and BasicMemoryWriter.
For some people, two classes may be redundant but for some others like me they may relief the pain in upgrading from fmt-4.x to fmt-5.0. They are not 100% compatible to the ones which were in fmt-4.x but for most of features, they are. I think two classes do not bloat much, if you don't mind please add them to the project. |
I am not sure if these are worth including in the project, but I'll point anyone interested to your code snippets in case they have troubles migrating. Thanks! |
With fmt-4.x, I often append multiple formatted stuffs to a fixed buffer in a quite neat way like this:
With fmt-5.0, ArrayWriter and MemoryWriter are somehow replaced by format_to_n().
Though with a single format, the new version looks better then previous one,
it is quite clumsy compare to the previous version in case someone needs to format multiple times with different things.
This is what I have tried to do the same with the new version:
I wonder if there is another better way to make it neater?.
Thanks.
Update:
To avoid opening a new issue, I would append more thing here.
I realize that the fmt::format_to_n() does not support wchar_t also.
The text was updated successfully, but these errors were encountered: