You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
boost::regex has a wonderful feature boost::regex_constants::match_extra. It works well.
But when using regex_iterator the contents of the captures() collection is not cleared before running next regex_search.
regex_iterator_implementation::next() should clear all captures (capture_sequence_type) before invoking next regex_search.
(( As a workaround get_captures() method can be used to clear captures for all submatches from the calling code on each iteration))
To demonstate the problem the following code will unexpectedly output
123456321789123654
instead of
123456789
#include <iostream>
#include <string>
#define BOOST_REGEX_MATCH_EXTRA
#include <boost/regex.hpp>
int main()
{
std::string str = "BEG123END BEG456END BEG789END";
boost::regex r("BEG(?:(?<group>\\d))+END");
boost::sregex_iterator beg(str.cbegin(), str.cend(), r, boost::regex_constants::match_extra);
for (boost::sregex_iterator i = beg; i != boost::sregex_iterator(); ++i)
{
const boost::ssub_match::capture_sequence_type& captures = (*i)["group"].captures();
for (const boost::ssub_match& c : captures)
{
std::cout << c.str();
}
}
}
The text was updated successfully, but these errors were encountered:
Another issue with current (1.76) regex_iterator_implementation::next() implementation is that memory consumption grows exponentially with every iteration and quickly exhausts all avaialble memory and beyond. Looks like a logical memory leak.
I had to patch the next() method to fix it.
After I started resetting 'what' memeber variable upon every iteration all problems went away.
bool next()
{
//if(what.prefix().first != what[0].second)
// flags |= match_prev_avail;
BidirectionalIterator next_start = what[0].second;
match_flag_type f(flags);
if(!what.length() || (f & regex_constants::match_posix))
f |= regex_constants::match_not_initial_null;
//if(base != next_start)
// f |= regex_constants::match_not_bob;
what = match_results<BidirectionalIterator>(); // reset to free up memory
bool result = regex_search(next_start, end, what, re, f, base);
if(result)
what.set_base(base);
return result;
}
boost::regex has a wonderful feature boost::regex_constants::match_extra. It works well.
But when using regex_iterator the contents of the captures() collection is not cleared before running next regex_search.
regex_iterator_implementation::next() should clear all captures (capture_sequence_type) before invoking next regex_search.
(( As a workaround get_captures() method can be used to clear captures for all submatches from the calling code on each iteration))
To demonstate the problem the following code will unexpectedly output
123456321789123654
instead of
123456789
The text was updated successfully, but these errors were encountered: