Defining function in spirit rule gives warning

故事扮演 提交于 2020-01-15 08:15:27

问题


qi::repeat(1,2) funtion in rule is giving me warrning and i dont want to ignore that warning so i want to optimized this code like separating the rule from the parsering method.

qi::phrase_parse is doing the same thing which is in the the rule but i want to sperate the rule and give rule to the pharse_parse funtion.

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);

and want to do somthing like this

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

test.txt Contain

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

My code is working for only 2nd line and extract values in vector but not other line values.


回答1:


Fix the rule's exposed attribute:

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

Repeat exposes a vector of hex_int's exposed attribute type

Live demo:

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";
}

Prints:

Parsed: 50 64bit elements

E.g. for this input:

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

random data generated with

od -A none -t x8 -w256 < /dev/urandom | head | awk '{ print "<" $1 $2 "> <" $3 "> <" $4 $5 ">" }'


来源:https://stackoverflow.com/questions/30883431/defining-function-in-spirit-rule-gives-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!