Reputation: 10539
I am trying to output non null terminating string, but keep iomanip formatting, e.g. std::left, std::setw etc.
My current code looks like this:
inline std::ostream& operator << (std::ostream& os, const StringRef &sr){
//return os.write(sr.data(), sr.size() );
// almost the same, but std::setw() works
return __ostream_insert(sr.data(), sr.size() );
}
This work OK on Linux with gcc, but fail on MacOS with clang.
Upvotes: 0
Views: 464
Reputation: 10539
The suggestion about os.rdbuf()->sputn(seq, n)
was certainly interesting, but it did not made the expected result.
I did open GCC C++ library code and "stole" from there. After the cleanup, code is something like this:
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;
}
Upvotes: 2