Boost Spirit implementation
Here is the mandatory Boost Spirit (Qi) based implementation. For good measure, including formatting using Boost Spirit (Karma):
#include <string>
#include <iostream>
#include <fstream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma=boost::spirit::karma;
namespace qi =boost::spirit::qi;
static qi::uint_parser<unsigned long long, 16, 16, 16> hex16_p; // parse long hex
static karma::uint_generator<unsigned long long, 16> hex16_f; // format long hex
int main(int argc, char** args)
{
std::ifstream ifs("input.data");
std::string line;
while (std::getline(ifs, line))
{
std::string::iterator begin = line.begin(), end = line.end();
int f0;
std::string f1;
std::vector<unsigned long long> f2;
bool ok = parse(begin, end,
qi::int_ // an integer
>> *qi::alpha // alternatively: *(qi::char_ - '\'')
>> '\'' >> +hex16_p >> '\'' // accepts 'n x 16' hex digits
, f0, f1, f2);
if (ok)
std::cout << "Parsed: " << karma::format(
karma::int_
<< ' ' << karma::string
<< ' ' << ("0x" << hex16_f) % ' '
, f0, f1, f2) << std::endl;
else
std::cerr << "Parse failed: " << line << std::endl;
}
return 0;
}
Test run:
Parsed: 0 ULL 0x4001c0180000000 0xee317bc
Parsed: 52 L 0x4001c0180000000
see Tweaks and samples below for info on how to tweak e.g. hex output
Benchmark
I had benchmarked @Cubbi's version and the above as written on 100,000x the sample inputs you provided. This initially gave Cubbi's version a slight advantage: 0.786s
versus 0.823s
.
Now, that of course wasn't fair comparison, since my code is constructing the parser on the fly each time. With that taken out of the loop like so:
typedef std::string::iterator It;
const static qi::rule<It> parser = qi::int_ >> *qi::alpha >> '\'' >> +hex16_p >> '\'';
bool ok = parse(begin, end, parser, f0, f1, f2);
Boost Spirit comes out a clear winner with only 0.093s
; already a factor 8.5x faster, and that is even with the karma formatter still being constructed each iteration.
with the output formatting commented out in both versions, Boost Spirit is >11x faster
Tweaks, samples
Note how you can easily tweak things:
// >> '\'' >> +hex16_p >> '\'' // accepts 'n x 16' hex digits
>> '\'' >> qi::repeat(1,2)[ hex16_p ] >> '\'' // accept 16 or 32 digits
Or format the hex output just like the input:
// ("0x" << hex16_f) % ' '
karma::right_align(16, '0')[ karma::upper [ hex16_f ] ] % ""
Changed sample output:
0ULL'04001C0180000000000000000EE317BC'
Parsed: 0 ULL 04001C0180000000000000000EE317BC
52L'04001C0180000000'
Parsed: 52 L 04001C0180000000
HTH