在精神规则中定义功能会发出警告

时间:2015-06-17 06:06:08

标签: c++ boost-spirit boost-spirit-qi

qi :: repeat(1,2)规则中的功能让我保持警告,我不想忽略该警告,所以我想优化这段代码,如分离规则来自解析方法。

qi :: phrase_parse 正在执行规则中的相同操作,但我想对规则进行操作并为pharse_parse功能提供规则。

std::ifstream ifs("f:/test.txt");
 std::string line;

//In header in my code
std::vector<unsigned long long> v_BF_Char;


//qi::int_parser<uintmax_t, 16> hex_int;
static qi::uint_parser<unsigned long long, 16, 16, 16> hex_int;

while (std::getline(ifs, line))
{
    typedef std::string::const_iterator It;
    It begin = line.begin(), end = line.end();

    // rule for grammer
    qi::rule<It, unsigned long long()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';


    bool ok = qi::phrase_parse(begin, end,
           *('<' >> qi::repeat(1,2)[ hex_int ] >> '>'),  qi::space, v_BF_Char);

并希望做这样的事情

bool ok = qi::phrase_parse(begin, end,
           braced_hex ,  qi::space, v_BF_Char);

test.txt 包含

<51dc> <04001C0180000000000000000EE317BC>
<05001C0180000000> <04001C0180000000000000000EE317BC>
<51dc> <30ea30f330ae30c330c8>
<0000> <fffd>
<003d> <00a5>
<005d> <00a6>
<005e> <007d>
<005f> <0303>
<0060> <2019>
<0061> <005c>
<0062> <2018>
<0063> <007c>
<0064> <007e>
<0068> <2044>
<0069> <0192>
<006a> <00a7>
<006b> <00a4>
<006c> <201c>

我的代码仅适用于第二行,并在向量中提取值,但不提取其他行值。

1 个答案:

答案 0 :(得分:2)

修复规则的公开属性:

qi::rule<It, std::vector<unsigned long long>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

重复exposes a vector of hex_int's exposed attribute type

现场演示:

<强> Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <fstream>

namespace qi = boost::spirit::qi;

int main() {
    std::ifstream ifs("input.txt");

    //In header in my code
    std::vector<unsigned long long> v_BF_Char;

    //qi::int_parser<uintmax_t, 16> hex_int;
    static qi::uint_parser<uint64_t, 16, 16, 16> hex_int;

    std::string line;
    while (std::getline(ifs, line))
    {
        typedef std::string::const_iterator It;
        It begin = line.begin(), end = line.end();

        // rule for grammer
        qi::rule<It, std::vector<uint64_t>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

        bool ok = qi::phrase_parse(begin, end, *braced_hex,  qi::space, v_BF_Char);
        assert(ok);
    }

    std::cout << "Parsed: " << v_BF_Char.size() << " 64bit elements\n";
}

打印:

Parsed: 50 64bit elements

E.g。对于此输入:

<1353cd278dd1f003636bc155006ac5ce> <1e83b053032565f0> <d1e97c841e68153a5d82d57df3074a21>
<92adee538fd147a337ebc8a4fc8d0ad3> <0ed9fb22ab42b3a4> <756ad64486054d22c62329e8dcaef0c5>
<16eeaec1108b1159b49c6bf884564519> <b4b87d1fd1aa10af> <1f710495fd863a1d191355adf1b33d5a>
<947ac523b4450ec26446840ccde3965b> <faa860f7763b23dc> <571decbfd0fcfe9a4047f72c101b9d87>
<50d726028b79b1a531a2c3752a4fdde7> <644e057721fa7fe1> <6bf66d2e1ae50351db53eddcee5fae41>
<6916580258e94f2be66eb71f103d3023> <a427df9bd05edd6d> <d896cfe92e6634867fcab5c6fc2de60b>
<9e50d5c9cda9e9a2fbf78eeb10f3a6bd> <9cff72edea319328> <0aabc7f36fcd058a2dfa7bb94602919a>
<923832f107c94d4a04b1de96241fda14> <003c7554390cabaf> <c43d58504fc6659bb226707efc0221b8>
<1040a8d23eac10e9e4b6abb2efcde1bd> <f38ac3906542529d> <ffbd836c54b0f498d358e4ea50170c94>
<7c4c6fd86a60cf7b4ac62faa0395c06b> <61156478683a6b01> <fb92ef7030068f25471e8049fb0f7cd3>
  

生成的随机数据
od -A none -t x8 -w256 < /dev/urandom | head | awk '{ print "<" $1 $2 "> <" $3 "> <" $4 $5 ">" }'