Jon Cage Jon Cage - 28 days ago 67
C++ Question

How can I use boost::regex on a boost::circular buffer?

I'm capturing data in a boost::circular_buffer and would like to now perform a regex search on the contents but I'm having some difficulty getting boost::regex to understand how to look at the buffer.

Here's a stripped down version of the sort of thing I'd like to do based on the example here:

// Set up a pre-populated data buffer as an example
std::string test = "Fli<usefuldata>bble";
boost::circular_buffer<char> dataBuff;
for (std::string::iterator it = test.begin(); it != test.end(); ++it)
{
dataBuff.push_back(*it);
}

// Set up the regex
static const boost::regex e("<\\w*>");
std::string::const_iterator start, end;
start = dataBuff.begin(); // <-- error C2679
end = dataBuff.end();
boost::match_flag_type flags = boost::match_default;

// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
if (regex_search(start, end, what, e, flags))
{
// Do something - we found what we're after...
}


I (quite understandably I think) get this error when I try to compile:

1>c:\projects\ProtocolBufferProcessor\ProtocolBufferProcessor.h(53): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'boost::cb_details::iterator<Buff,Traits>' (or there is no acceptable conversion)
1> with
1> [
1> Buff=boost::circular_buffer<char>,
1> Traits=boost::cb_details::nonconst_traits<std::allocator<char>>
1> ]
1> c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(435): could be 'std::_String_iterator<_Elem,_Traits,_Alloc> &std::_String_iterator<_Elem,_Traits,_Alloc>::operator =(const std::_String_iterator<_Elem,_Traits,_Alloc> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> while trying to match the argument list '(std::_String_iterator<_Elem,_Traits,_Alloc>, boost::cb_details::iterator<Buff,Traits>)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> and
1> [
1> Buff=boost::circular_buffer<char>,
1> Traits=boost::cb_details::nonconst_traits<std::allocator<char>>
1> ]


...but is there anything I can do other than create a std::string character by character from the circular buffer each time I run the regex?

I'm using Boost v1.54 if that makes a difference.

Answer

you need to use the buffer's iterator type:

Live On Coliru

#include <boost/circular_buffer.hpp>
#include <boost/regex.hpp>

using buffer_t = boost::circular_buffer<char>;

int main() {
    // Set up a pre-populated data buffer as an example
    std::string test = "Fli<usefuldata>bble";
    buffer_t dataBuff;
    for (std::string::iterator it = test.begin(); it != test.end(); ++it)
    {
        dataBuff.push_back(*it);
    }

    // Set up the regex
    static const boost::regex e("<\\w*>");

    buffer_t::const_iterator start = dataBuff.begin(); // <-- error C2679
    buffer_t::const_iterator end = dataBuff.end();
    boost::match_flag_type flags = boost::match_default;

    // Try and find something of interest in the buffer
    boost::match_results<buffer_t::const_iterator> what;
    if (regex_search(start, end, what, e, flags))
    {
        // Do something - we found what we're after...
    }
}
Comments