| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| #include <boost/regex.hpp> |
| #include <iostream> |
| #include <fstream> |
| #include <sstream> |
| #include <string> |
| #include <cstring> |
|
|
| #ifdef BOOST_NO_STDC_NAMESPACE |
| namespace std{ using ::memmove; } |
| #endif |
|
|
| |
| boost::regex e("<[^>]*>"); |
| |
| unsigned int tags = 0; |
| |
| const char* next_pos = 0; |
|
|
| bool grep_callback(const boost::match_results<const char*>& m) |
| { |
| if(m[0].matched == false) |
| { |
| |
| next_pos = m[0].first; |
| } |
| else |
| ++tags; |
| return true; |
| } |
|
|
| void search(std::istream& is) |
| { |
| char buf[4096]; |
| next_pos = buf + sizeof(buf); |
| bool have_more = true; |
| while(have_more) |
| { |
| |
| std::ptrdiff_t leftover = (buf + sizeof(buf)) - next_pos; |
| |
| std::ptrdiff_t size = next_pos - buf; |
| |
| std::memmove(buf, next_pos, leftover); |
| |
| is.read(buf + leftover, size); |
| std::streamsize read = is.gcount(); |
| |
| have_more = read == size; |
| |
| next_pos = buf + sizeof(buf); |
| |
| boost::regex_grep<bool(*)(const boost::cmatch&), const char*>(grep_callback, |
| static_cast<const char*>(buf), |
| static_cast<const char*>(buf + read + leftover), |
| e, |
| boost::match_default | boost::match_partial); |
| } |
| } |
|
|
| int main(int argc, char* argv[]) |
| { |
| if(argc > 1) |
| { |
| for(int i = 1; i < argc; ++i) |
| { |
| std::ifstream fs(argv[i]); |
| if(fs.bad()) continue; |
| search(fs); |
| fs.close(); |
| } |
| } |
| else |
| { |
| std::string one("<META NAME=\"keywords\" CONTENT=\"regex++, regular expressions, regular expression library, C++\">"); |
| std::string what; |
| while(what.size() < 10000) |
| { |
| what.append(one); |
| what.append(13, ' '); |
| } |
| std::stringstream ss; |
| ss.str(what); |
| search(ss); |
| } |
| std::cout << "total tag count was " << tags << std::endl; |
| return 0; |
| } |
|
|
|
|
|
|
|
|
|
|