首页 > 解决方案 > 如何将非空终止字符串输出到 iostream,但保持格式化

问题描述

我正在尝试输出非空终止字符串,但保持 iomanip 格式,例如 std::left、std::setw 等。

我当前的代码如下所示:

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() );
}

这在带有 gcc 的 Linux 上工作正常,但在带有 clang 的 MacOS 上失败。

标签: c++iostream

解决方案


关于的建议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;
}

推荐阅读