如何在boost ::循环缓冲区上使用boost :: regex?

时间:2016-11-07 16:10:01

标签: c++ boost boost-regex boost-circularbuffer

我正在使用boost :: circular_buffer捕获数据,并希望现在对内容执行正则表达式搜索,但是我很难获得boost :: regex以了解如何查看缓冲区。

以下是基于示例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...
    }

我(我觉得很可以理解)在我尝试编译时遇到此错误:

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>          ]

...除了每次运行正则表达式时从循环缓冲区中按字符创建一个std :: string字符,我还能做什么呢?

如果有所不同,我正在使用Boost v1.54。

1 个答案:

答案 0 :(得分:1)

你需要使用缓冲区的迭代器类型:

<强> 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...
    }
}