// Configuration Interface
//===--------------------------------------------------------------------===//
+ /// If possible, pre-allocate \p ExtraSize bytes for stream data.
+ /// i.e. it extends internal buffers to keep additional ExtraSize bytes.
+ /// So that the stream could keep at least tell() + ExtraSize bytes
+ /// without re-allocations. reserveExtraSpace() does not change
+ /// the size/data of the stream.
+ virtual void reserveExtraSpace(uint64_t ExtraSize) {}
+
/// Set the stream to be buffered, with an automatically determined buffer
/// size.
void SetBuffered();
flush();
return OS;
}
+
+ void reserveExtraSpace(uint64_t ExtraSize) override {
+ OS.reserve(tell() + ExtraSize);
+ }
};
/// A raw_ostream that writes to an SmallVector or SmallString. This is a
/// Return a StringRef for the vector contents.
StringRef str() const { return StringRef(OS.data(), OS.size()); }
+
+ void reserveExtraSpace(uint64_t ExtraSize) override {
+ OS.reserve(tell() + ExtraSize);
+ }
};
/// A raw_ostream that discards all output.
TiedStream << "0";
EXPECT_EQ("acegostuv", TiedToBuffer);
}
+
+TEST(raw_ostreamTest, reserve_stream) {
+ std::string Str;
+ raw_string_ostream OS(Str);
+ OS << "11111111111111111111";
+ uint64_t CurrentPos = OS.tell();
+ OS.reserveExtraSpace(1000);
+ EXPECT_TRUE(Str.capacity() >= CurrentPos + 1000);
+ OS << "hello";
+ OS << 1;
+ OS << 'w' << 'o' << 'r' << 'l' << 'd';
+ OS.flush();
+ EXPECT_EQ("11111111111111111111hello1world", Str);
+}
}