帮助管理 iostream

发布于 2024-10-22 14:33:03 字数 1522 浏览 3 评论 0原文

假设我得到一个 stringbuf,其中包含某些必须删除的某些字符序列的内容:

std::stringbuf string_buff;
std::iostream io_stream (&string_buff);
io_stream << "part-one\r\npart-two\r\npart-three\r\nEND";

在那里,必须删除 CRLF 对,所以我测试了一些:

int pos = 0;
while (true) {
    pos = string_buff.str().rfind("\r\n");
    if (pos == string_buff.str().npos) {
        break;
    } else {
        std::string preamble = string_buff.str().substr(0, pos);
        std::string postamble = string_buff.str().substr(pos +2);
        io_stream.seekp(0);
        io_stream << preamble << postamble;
    }
}

但序列保持相同的长度。所以,我得到以下结果:

 part-onepart-twopart-threeENDNDNDND

我想有一些方法可以做到这一点 - 而且更优雅 - 但我找不到方法。

顺便一提。看来直接操作内部字符串是行不通的。我会说这样的话:

 string_buff.str().clear();

既不是

 io_stream.clear();

 io_stream.flush();

不幸的是,我最初的方法是错误的

正如我之前提到的,真正的问题与 boost::asio::streambuf 有关,而我的错误是试图模仿它,在单独的控制台应用程序中使用 std::istream 进行测试。

当然,使用 asio::streambuf ,你不能做一些 as

 strembuf.str("");

所以真实的情况是这样的:

 boost::asio::streambuf stream_buff;
 std::iostream response_stream(&stream_buff);
 response_stream << "part-one\r\npart-two\r\npart-three\r\nEND";

对于造成的混乱,我深表歉意。

问题仍然是一样的:如何从输入中删除 CRLF 或任何其他字符序列?

Suppose that I get a stringbuf with some content that include certain character sequences who must be removed:

std::stringbuf string_buff;
std::iostream io_stream (&string_buff);
io_stream << "part-one\r\npart-two\r\npart-three\r\nEND";

There, the CRLF pairs must be removed, so I've tested some as:

int pos = 0;
while (true) {
    pos = string_buff.str().rfind("\r\n");
    if (pos == string_buff.str().npos) {
        break;
    } else {
        std::string preamble = string_buff.str().substr(0, pos);
        std::string postamble = string_buff.str().substr(pos +2);
        io_stream.seekp(0);
        io_stream << preamble << postamble;
    }
}

But the sequence remains of the same length. So, I get the following result:

 part-onepart-twopart-threeENDNDNDND

I suppose that there are some way to do this -and more elegant- but I'm unable to find the way.

By the way. It seems that the direct manipulation on the inner string does not work. I say tings like:

 string_buff.str().clear();

Neither

 io_stream.clear();

or

 io_stream.flush();

Unfortunately I mistaken in my initial approach

As I mentioned earlier, the real problem is related to a boost::asio::streambuf and my mistake was in try to mimic that, with a std::istream in a separate console application for test purposes.

Of course, with an asio::streambuf y can't do some as

 strembuf.str("");

So the real situation is this:

 boost::asio::streambuf stream_buff;
 std::iostream response_stream(&stream_buff);
 response_stream << "part-one\r\npart-two\r\npart-three\r\nEND";

My apologies for the confussion.

The question remains the same: How can I remove the CRLF -or any other- character sequence from the input?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云仙小弟 2024-10-29 14:33:03

你很接近!使streambuf为空的方法是

String_buff.str("");

为它分配空字符串。

(string_buff.str().clear() 只是清空内容的副本:-)

You are close! The way to make the streambuf empty is

String_buff.str("");

That will assign it the empty string.

(string_buff.str().clear() just empties a copy of the contents :-)

坏尐絯℡ 2024-10-29 14:33:03

使用 Boost.String

string s(string_buf.str());
boost::erase_all(s, "\r\n");
string_buf.str(s);

或者,如果您需要将行尾替换为其他空格:

string s(string_buf.str());
boost::replace_all(s, "\r\n", " ");
string_buf.str(s);

并且 yesstringbuf.str() 返回字符串的副本,而不是对字符串的引用

Use Boost.String.

string s(string_buf.str());
boost::erase_all(s, "\r\n");
string_buf.str(s);

or, if you need the line-ends replaced with other whitespace:

string s(string_buf.str());
boost::replace_all(s, "\r\n", " ");
string_buf.str(s);

And yes, stringbuf.str() returns a copy of, not a reference to the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文