帮助管理 iostream
假设我得到一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你很接近!使streambuf为空的方法是
为它分配空字符串。
(string_buff.str().clear() 只是清空内容的副本:-)
You are close! The way to make the streambuf empty is
That will assign it the empty string.
(string_buff.str().clear() just empties a copy of the contents :-)
使用 Boost.String。
或者,如果您需要将行尾替换为其他空格:
并且 yes ,
stringbuf.str()
返回字符串的副本,而不是对字符串的引用。Use Boost.String.
or, if you need the line-ends replaced with other whitespace:
And yes,
stringbuf.str()
returns a copy of, not a reference to the string.