关于
os.rdbuf()->sputn(seq, n)
当然很有趣,但没有达到预期的效果。
我确实打开了GCC C++库代码并从那里“偷窃”。清理之后,代码如下:
inline std::ostream& operator << (std::ostream& os, const StringRef &sr){
// following is based on gcc __ostream_insert() code:
// https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.2/ostream__insert_8h-source.html
std::streamsize const width = os.width();
std::streamsize const size = static_cast<std::streamsize>( sr.size() );
std::streamsize const fill_size = width - size;
bool const left = (os.flags() & std::ios::adjustfield) == std::ios::left;
auto osfill = [](std::ostream& os, auto const count, char const c){
for(std::streamsize i = 0; i < count; ++i)
os.put(c);
};
if (fill_size > 0 && left == false)
osfill(os, fill_size, os.fill());
os.write(sr.data(), size);
if (fill_size > 0 && left == true)
osfill(os, fill_size, os.fill());
return os;
}