| 1 | #include <iostream>
|
|---|
| 2 | #include <boost/spirit/include/qi.hpp>
|
|---|
| 3 |
|
|---|
| 4 | namespace qi = boost::spirit::qi;
|
|---|
| 5 | using qi::omit;
|
|---|
| 6 | using qi::repeat;
|
|---|
| 7 | using std::cout;
|
|---|
| 8 | using std::endl;
|
|---|
| 9 | using qi::string;
|
|---|
| 10 | using qi::char_;
|
|---|
| 11 |
|
|---|
| 12 | typedef qi::rule<std::string::const_iterator, std::string()> strrule_t;
|
|---|
| 13 |
|
|---|
| 14 | void test(const std::string input, strrule_t rule)
|
|---|
| 15 | {
|
|---|
| 16 | std::string target;
|
|---|
| 17 | std::string::const_iterator i = input.begin(), ie = input.end();
|
|---|
| 18 |
|
|---|
| 19 | if (qi::parse(i, ie, rule, target))
|
|---|
| 20 | {
|
|---|
| 21 | cout << "Success: '" << target << "'" << endl;
|
|---|
| 22 | }
|
|---|
| 23 | else
|
|---|
| 24 | {
|
|---|
| 25 | cout << "Failed to match." << endl;
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | int main()
|
|---|
| 30 | {
|
|---|
| 31 | strrule_t shortHexValue = repeat(4)[char_("a-fA-F0-9")];
|
|---|
| 32 | strrule_t longHexValue = repeat(8)[char_("a-fA-F0-9")];
|
|---|
| 33 | strrule_t hexValue1 = omit[string("0x")] >> ( longHexValue | shortHexValue );
|
|---|
| 34 | strrule_t hexValue2 = omit[string("0x")] >> ( shortHexValue | longHexValue );
|
|---|
| 35 |
|
|---|
| 36 | test( "0x123FAA22", hexValue1 );
|
|---|
| 37 | test( "0x12AA", hexValue1 );
|
|---|
| 38 | test( "0x123FAA22", hexValue2 );
|
|---|
| 39 | test( "0x1A22", hexValue2 );
|
|---|
| 40 |
|
|---|
| 41 | return 0;
|
|---|
| 42 | }
|
|---|