Re: Parse a size prefixed protocol

Stephan Menzel <[email protected]> Mon, 30 Jul 2018 15:40:44 +0200
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <CAEQ568uPqKtD_3pLLcc7dOcEuSV48SzoOrkVV+tyP6kyuRHZow@mail.gmail.com>
On Mon, Jul 30, 2018 at 12:32 PM, Stephan Menzel <[email protected]>
wrote:

> Will give almost the correct result. But when I parse this:
>
> "$5\r\nH\0llo"        // just the e replaced by \0 for test
>
> it gives me a 6 byte string instead of 5. The "H\0llo" is there but it if
> prefixed by garbage.
>
>
>
I guess I have figured it out.
Reason for the faulty prefix was, that the attribute contained the size as
a first character. I could not suppress this by omit[] but it seems to work
when I put it into another rule. Like this:


template <typename InputIterator>
struct bulk_string_parser : qi::grammar<InputIterator, std::string()> {

bulk_string_parser()
: bulk_string_parser::base_type(m_bulk_start, "bulk_string")
, m_size(0) {

using qi::labels::_1;
using qi::_val;

m_prefix      = '$' >> qi::ulong_[phx::ref(m_size) = _1] >> "\r\n";
m_bulk_start %= m_prefix >> qi::repeat(phx::ref(m_size))[qi::char_] >>
"\r\n";
}

std::size_t                             m_size;
qi::rule<InputIterator>                 m_prefix;
qi::rule<InputIterator, std::string()>  m_bulk_start;
};

This works now. Even with the null byte. Surprisingly, it only works when
using multipass iterators. The same parser, running on
std::string::const_iterator fails. Presumably because the resulting string
only reaches until the null terminator.

So, not quite a solution but enough to move on.

Thanks a bunch!

Stephan

------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general