4 ms·
Yep. stringstreams make copies of passed strings too. I had to hack around that once like this: struct ZeroCopyBuf : public std::streambuf { ZeroCopyBuf
by linuxhansl 3y ago
Yep. stringstreams make copies of passed strings too. I had to hack around that once like this:
struct ZeroCopyBuf : public std::streambuf
{
ZeroCopyBuf(const std::string &s) : ZeroCopyBuf(s.c_str(), s.length()) {}
ZeroCopyBuf(const char *c, std::size_t l) : ZeroCopyBuf(const_cast<char*>(c), l) {}
ZeroCopyBuf(char *c, std::size_t l) { setg(c, c, c + l); }
};
...
std::string s ...;
ZeroCopyBuf buf(s);
std::istream is(&buf);
Terrible.
C++-20 makes it better by adding move semantics to most methods... Still.
- nly 3y agoJust use the Boost Iostreams library The array_source device can be used with any buffer (char*, size_t)