eliminating \n and \r from string [boost::iostreams]
Hello
I use boost::iostreams input filter stream to filter the input file.
Heres the example code:
Code:
using namespace std;
using namespace boost::iostreams;
namespace io = boost::iostreams;
class long_line_counter : public boost::iostreams::line_filter {
public:
explicit long_line_counter(int max_length = 80)
: max_(max_length), count_(0) { }
int count() const { return count_; }
vector<string> m_list;
private:
std::string do_filter(const std::string& line) {
if (line.size() < max_) m_list.push_back(line);
++count_;
return line;
}
int max_;
int count_;
};
int main() {
try {
filtering_istream fin;
long_line_counter llc(80);
file_source fss("outest.fil", ios::in | ios::binary);
fin.push(boost::ref(llc));
fin.push(boost::ref(fss));
char buf[4012];
while ( fin.read(buf, 4012) ) {
}
std::cout << llc.count() << "\n";
std::cout << llc.mail_list.size() << "\n";
}
catch (exception &e) {
cout << "exception: " << e.what() << std::endl;
}
return 0;
}
Now the problem is that the boost::iostreams line_filter will pass the line (string) along with the \r and \n symbols to the do_filter() function.. So if it reads line "customer: Steve Porter\r\n" it will pass the same string to the function instead of string without '\r\n'.
I've been wondering whats the most efficient way to eliminate/remove '\r' and '\n' symbols from this std::string.
Should I do char by char check, or maybe use regex? Is there any more efficient way since I have to do this many thousand times in a short period.
Thanks for help!