readString(): Read directly into std::string

When reading a huge string, this halves memory consumption.

(Strictly speaking, this appears only valid in C++17, but who cares...)
This commit is contained in:
Eelco Dolstra 2017-03-01 14:54:11 +01:00
parent 07a0b8ca67
commit 374908726b
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -207,10 +207,10 @@ size_t readString(unsigned char * buf, size_t max, Source & source)
string readString(Source & source) string readString(Source & source)
{ {
auto len = readNum<size_t>(source); auto len = readNum<size_t>(source);
auto buf = std::make_unique<unsigned char[]>(len); std::string res(len, 0);
source(buf.get(), len); source((unsigned char*) res.data(), len);
readPadding(len, source); readPadding(len, source);
return string((char *) buf.get(), len); return res;
} }
Source & operator >> (Source & in, string & s) Source & operator >> (Source & in, string & s)