boost::spirit::qi::parse strange behaviour with radix

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

boost::spirit::qi::parse strange behaviour with radix

Ernest Zaslavsky

I observe some strange behavior

The below code works like expected:

namespace bsq = boost::spirit::qi;

std::string input = "FFFFFFFF";

int result = 0;

auto itBeg = input.cbegin();

auto itEnd = input.cend();

if(!bsq::parse(itBeg, itEnd, bsq::hex, result) || itBeg != itEnd)

{

     throw std::exception();

}

std::cout << input << " means " << result << std::endl;

However, this one fails to parse:

std::string input = "FFFFFFFF";

int result = 0;

auto itBeg = input.cbegin();

auto itEnd = input.cend();

if(!bsq::parse(itBeg, itEnd, bsq::int_parser<int, 16>(), result) || itBeg != itEnd)

{

   throw std::exception();

}

std::cout << input << " means " << result << std::endl;

 

Isnt int_parser<int, 16, 1, -1> is the same as bsq::hex? Am I doing something wrong?

Read More


------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: boost::spirit::qi::parse strange behaviour with radix

Stephen Torri
It is a good idea to provide a full working example for people to compile and run in order to assist you.

Stephen

On Oct 26, 2015 8:57 AM, Ernest Zaslavsky <[hidden email]> wrote:

>
> I observe some strange behavior
>
> The below code works like expected:
>
> namespace bsq = boost::spirit::qi;
>
> std::string input = "FFFFFFFF";
>
> int result = 0;
>
> auto itBeg = input.cbegin();
>
> auto itEnd = input.cend();
>
> if(!bsq::parse(itBeg, itEnd, bsq::hex, result) || itBeg != itEnd)
>
> {
>
>      throw std::exception();
>
> }
>
> std::cout << input << " means " << result << std::endl;
>
> However, this one fails to parse:
>
> std::string input = "FFFFFFFF";
>
> int result = 0;
>
> auto itBeg = input.cbegin();
>
> auto itEnd = input.cend();
>
> if(!bsq::parse(itBeg, itEnd, bsq::int_parser<int, 16>(), result) || itBeg != itEnd)
>
> {
>
>    throw std::exception();
>
> }
>
> std::cout << input << " means " << result << std::endl;
>
>  
>
> Isnt int_parser<int, 16, 1, -1> is the same as bsq::hex? Am I doing something wrong?
------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: boost::spirit::qi::parse strange behaviour with radix

Ernest Zaslavsky
Live on Coliru
http://coliru.stacked-crooked.com/a/ca6ade7b996b6dee

Interesting observation. If I change the int_parser to uint_parser it work as expected - the result is -1
So the additional question is what does "uint-ness" in the parser mean? Uint-ness of input or result?


-----Original Message-----
From: Stephen Torri [mailto:[hidden email]]
Sent: Monday, October 26, 2015 4:41 PM
To: Spirit General <[hidden email]>
Subject: Re: [Spirit-general] boost::spirit::qi::parse strange behaviour with radix

It is a good idea to provide a full working example for people to compile and run in order to assist you.

Stephen

On Oct 26, 2015 8:57 AM, Ernest Zaslavsky <[hidden email]> wrote:

>
> I observe some strange behavior
>
> The below code works like expected:
>
> namespace bsq = boost::spirit::qi;
>
> std::string input = "FFFFFFFF";
>
> int result = 0;
>
> auto itBeg = input.cbegin();
>
> auto itEnd = input.cend();
>
> if(!bsq::parse(itBeg, itEnd, bsq::hex, result) || itBeg != itEnd)
>
> {
>
>      throw std::exception();
>
> }
>
> std::cout << input << " means " << result << std::endl;
>
> However, this one fails to parse:
>
> std::string input = "FFFFFFFF";
>
> int result = 0;
>
> auto itBeg = input.cbegin();
>
> auto itEnd = input.cend();
>
> if(!bsq::parse(itBeg, itEnd, bsq::int_parser<int, 16>(), result) || itBeg != itEnd)
>
> {
>
>    throw std::exception();
>
> }
>
> std::cout << input << " means " << result << std::endl;
>
>  
>
> Isnt int_parser<int, 16, 1, -1> is the same as bsq::hex? Am I doing something wrong?
------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: boost::spirit::qi::parse strange behaviour with radix

sehe
On 10/27/2015 05:41 AM, Ernest Zaslavsky wrote:
> Interesting observation. If I change the int_parser to uint_parser it work as expected - the result is -1
> So the additional question is what does "uint-ness" in the parser mean? Uint-ness of input or result?
I already answered on SO

Uintness is about the parser. The parser doesn't parse a signed number.
Therefore 0xFFFFFFFF overflows capacity for signed data type.

If you want "C++ behaviour" (silent integer conversions):

     int32_t bla = 0xFFFFFFFF; // bla == -1

then simply do the same (note that 0xFFFFFFFF is by definition an
unsigned literal as well), so parse the unsigned and coerce to  your
type. Spirit inserts the static_cast


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: boost::spirit::qi::parse strange behaviour with radix

Ernest Zaslavsky
Thanks!


-----Original Message-----
From: Seth [mailto:[hidden email]]
Sent: Tuesday, October 27, 2015 11:03 AM
To: Spirit General Mailing List <[hidden email]>
Subject: Re: [Spirit-general] boost::spirit::qi::parse strange behaviour with radix

On 10/27/2015 05:41 AM, Ernest Zaslavsky wrote:
> Interesting observation. If I change the int_parser to uint_parser it
> work as expected - the result is -1 So the additional question is what does "uint-ness" in the parser mean? Uint-ness of input or result?
I already answered on SO

Uintness is about the parser. The parser doesn't parse a signed number.
Therefore 0xFFFFFFFF overflows capacity for signed data type.

If you want "C++ behaviour" (silent integer conversions):

     int32_t bla = 0xFFFFFFFF; // bla == -1

then simply do the same (note that 0xFFFFFFFF is by definition an unsigned literal as well), so parse the unsigned and coerce to  your type. Spirit inserts the static_cast


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general