128 bit string to array using boost::spirit::*

牧云@^-^@ 提交于 2019-12-08 16:27:31

Not literally staying with the question, if you really wanted just to parse the hexadecimal representation of a 128 bit integer, you can do so portably by using uint128_t defined in Boost Multiprecision:

qi::int_parser<uint128_t, 16, 16, 16> uint128_hex;

uint128_t parsed;
bool r = qi::parse(f, l, uint128_hex, parsed);

This is bound to be the quickest way especially on platforms where 128bit types are supported in the instruction set.

Live On Coliru

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/spirit/include/qi.hpp>

namespace qi  = boost::spirit::qi;

int main() {
    using boost::multiprecision::uint128_t;
    using It = std::string::const_iterator;
    qi::int_parser<uint128_t, 16, 16, 16> uint128_hex;

    std::string const src("00112233445566778899aabbccddeeff");
    auto f(src.begin()), l(src.end());

    uint128_t parsed;
    bool r = qi::parse(f, l, uint128_hex, parsed);

    if (r) std::cout << "Parse succeeded: " << std::hex << std::showbase << parsed << "\n";
    else   std::cout << "Parse failed at '" << std::string(f,l) << "'\n";

}

There's a sad combination of factors that lead to this being a painful edge case

  • Boost Fusion can adapt (boost::)array<> but it it requires the parser to result in a tuple of elements, not a container
  • Boost Fusion can adapt these sequences, but need to be configure to allow 16 elements:

    #define FUSION_MAX_VECTOR_SIZE 16
    
  • Even when you do, the qi::repeat(n)[] parser directive expects the attribute to be a container type.

You might work around all this in an ugly way (e.g. Live On Coliru). This makes everything hard to work with down the road.

I'd prefer a tiny semantic action here to make the result being assigned from qi::repeat(n)[]:

    using data_t = boost::array<uint8_t, 16>;
    data_t dst {};

        qi::rule<It, data_t(), qi::locals<data_t::iterator> > rule = 
            qi::eps [ qi::_a = phx::begin(qi::_val) ]
            >> qi::repeat(16) [
                    uint8_hex [ *qi::_a++ = qi::_1 ]
            ];

This works without too much noise. The idea is to take the start iterator and write to the next element each iteraton.

Live On Coliru

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

namespace qi  = boost::spirit::qi;
namespace phx = boost::phoenix;

int main() {
    using It = std::string::const_iterator;
    qi::int_parser<uint8_t, 16, 2, 2> uint8_hex;

    std::string const src("00112233445566778899aabbccddeeff");
    auto f(src.begin()), l(src.end());

    using data_t = boost::array<uint8_t, 16>;
    data_t dst {};

        qi::rule<It, data_t(), qi::locals<data_t::iterator> > rule = 
            qi::eps [ qi::_a = phx::begin(qi::_val) ]
            >> qi::repeat(16) [
                    uint8_hex [ *qi::_a++ = qi::_1 ]
            ];

    bool r = qi::parse(f, l, rule, dst);

    if (r) {
        std::cout << "Parse succeeded\n";

        for(unsigned i : dst) std::cout << std::hex << std::showbase << i << " ";
        std::cout << "\n";
    } else {
        std::cout << "Parse failed at '" << std::string(f,l) << "'\n";
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!