Boost logo

Boost :

Subject: Re: [boost] hex/unhex()?
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2011-11-02 08:12:02


> >> > As Marshal pointed out, Spirit supports that easily:
> >> >
> >> >    std::string input("61626f6465");
> >> >    std::string str;
> >> >    if (qi::parse(input.begin(), input.end(), qi::hex, str))
> >> >        assert(str == "abode");
> >>
> >> Is there no range-based variant of parse()?
> >>
> >> > and
> >> >
> >> >    std::string str("abode");
> >> >    std::string output;
> >> >    if (karma::generate(back_inserter(output), karma::hex, str))
> >> >        assert(output == "61626f6465");
> >> >
> >> > If you want to avoid the cost of possible reallocations in output,
> >> > just
> >> call output.reserve(...) upfront. Otherwise both code snippets
> >> generate code equivalent to hand written assembly and have been shown
> >> to be as fast as it can get.
> >>
> >> When would generate() fail (return false)?
> >> AFAIK to hex can't fail and in that case a variant that returns
> >> std::string would be nicer.
> >
> > Actually, please disregard what I wrote, it is complete nonsense. I
> > misread your initial mail.
> >
> > Note to self: drink coffee before responding to email!
>
> Why? Your functions seem quite close to the desired functionality.

Here is what Spirit can do:

    std::string input("61626f6465");
    unsigned ui;
    if (qi::parse(input.begin(), input.end(), qi::hex, ui))
        assert(ui == 0x61626f6465);

and

    unsigned ui = 0x61626f6465;
    std::string output;
    if (karma::generate(back_inserter(output), karma::hex, ui))
        assert(output == "61626f6465");

As I said, I wrote nonsense with regard to your question. Sorry for the
confusion.

Regards Hartmut
---------------
http://boost-spirit.com
http://stellar.cct.lsu.edu


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk