bit level matching on x3

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

bit level matching on x3

Takatoshi Kondo-2
Hello,

I'm trying to write bit level matching on SpiritX3.
For example, all of upper 4 bits is 1.

Here is the code:

http://melpon.org/wandbox/permlink/YzhZUNnHWPGQyPUs

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

int main() {
    using x3::byte_;

    // input data
    std::string s = {
        char(0b11110000), // match
        char(0b11110001), // not match
        char(0b11110011)  // not match
    };

    // rule
    auto match = +byte_(0b11110000);
    // auto match = +byte_(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;
}

Unlike char_, byte_ doesn't have range version interface.
The example above uses the rule exact matching to 0b11110000. So the
first byte of the input string is matched, but continuing two byte
doesn't match.

Is there any good way to do that?

I guess that something like byte wise pre-match processing...

---
Thanks,
Takatoshi Kondo

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: bit level matching on x3

Mike Gresens
This post was updated on .
Hi,

you are right. An 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 auto from, const auto to)
{
        const auto check = [from, to](auto& ctx)
        {
                const auto 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...
Reply | Threaded
Open this post in threaded view
|

Re: bit level matching on x3

Takatoshi Kondo-2
Thank you, Mike

On Sun, Oct 9, 2016 at 3:29 PM, Mike Gresens <[hidden email]> wrote:
> 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:

It works as I expected!

----
Thanks,
Takatoshi Kondo

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general