Re: bit level matching on x3
Mike Gresens <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <[email protected]> |
Hi,
you are right. A out-of-box byte_(from, to) rule would be very nice!
Because this rule is not provided by spirit, you can write it yourself:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/binary.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
const auto byte_rule = [](const std::uint8_t from, const std::uint8_t to)
{
const auto check = [from, to](auto& ctx)
{
const std::uint8_t value = x3::_attr(ctx);
x3::_val(ctx) = value;
x3::_pass(ctx) = from <= value && value <= to;
};
return x3::byte_ [check];
};
int main() {
using x3::byte_;
// input data
std::string s = {
char(0b11110000),
char(0b11110001),
char(0b11110011)
};
// rule
//auto match = +byte_(0b11110000);
auto match = +byte_rule(0b11110000, 0b11111111); // no range version
like char_
auto it = s.begin();
auto end = s.end();
bool r = x3::phrase_parse(
it, end,
match, x3::space);
std::cout << std::boolalpha << r << std::endl;
std::cout << std::distance(it, end) << " bytes remaining." << std::endl;
}
Best regards,
Mike...
--
View this message in context: http://boost.2283326.n4.nabble.com/bit-level-matching-on-x3-tp4688691p4688693.html
Sent from the spirit-general mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot