Re: Parse a size prefixed protocol

Stephan Menzel <[email protected]> Mon, 30 Jul 2018 12:32:41 +0200
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <CAEQ568t=nyc36bYP4rJLkAQK0Z=ixOTcHm9gifd2-ZrR5Nhmpg@mail.gmail.com>
On Mon, Jul 30, 2018 at 12:22 PM, Mike Gresens <[email protected]>
wrote:

> Example from
> https://www.boost.org/doc/libs/1_67_0/libs/spirit/doc/
> html/spirit/qi/reference/directive/repeat.html
>
> std::string str;int n;test_parser_attr("\x0bHello World",
>     char_[phx::ref(n) = _1] >> repeat(phx::ref(n))[char_], str);std::cout << n << ',' << str << std::endl;  // will print "11,Hello World"
>
>
Thank you!
I found this pretty much immediately after I posted my message and tried it
out already. It fails for me. This parser here:

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

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

    using qi::labels::_1;

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

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

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.

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