Re: Bug in real_parser
Seth <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <[email protected]> |
On 17-09-16 15:28, peterkochlarsen wrote:
> Now as_double("045.000W") returns 45, but as_double("045.000E") returns
> 45000.
I don't believe that for a second. Firstly the code doesn't compile (`d`
is undeclared). Secondly, on correct input it will return `-1`. I think
you need better backup to claim bugs.
Here's what I think you're trying to do, and the results are FINE if you
check the errors:
#include <boost/spirit/include/qi.hpp>
#include <iostream>
double as_double(std::string const& s)
{
namespace qi = boost::spirit::qi;
double d;
auto begin = std::begin(s);
if (qi::phrase_parse(begin, std::end(s), qi::double_/* >> qi::eoi*/,
qi::space, d))
return d;
return -1;
}
int main()
{
for (std::string const& s : {
"4", "04", "045.", ".0", "7E0", "7.E0",
"-4", "-04", "-045.", "-.0", "-7E0", "-7.E0",
"+4", "+04", "+045.", "+.0", "+7E0", "+7.E0",
"-4", "-04", "-045.", "-.0", "-7E0", "-7.E0",
".4e7", ".4e-7",
// and then your case
"045.000W",
"045.000E",
})
{
auto d = as_double(s);
std::cout << "'" << s << "' -> " << d;
if (d == -1)
std::cout << " (ERR)\n";
else
std::cout << "\n";
}
}
Prints
'4' -> 4
'04' -> 4
'045.' -> 45
'.0' -> 0
'7E0' -> 7
'7.E0' -> 7
'-4' -> -4
'-04' -> -4
'-045.' -> -45
'-.0' -> -0
'-7E0' -> -7
'-7.E0' -> -7
'+4' -> 4
'+04' -> 4
'+045.' -> 45
'+.0' -> 0
'+7E0' -> 7
'+7.E0' -> 7
'-4' -> -4
'-04' -> -4
'-045.' -> -45
'-.0' -> -0
'-7E0' -> -7
'-7.E0' -> -7
'.4e7' -> 4e+06
'.4e-7' -> 4e-08
'045.000W' -> 45
'045.000E' -> -1 (ERR)
If you insist that `045.000W` should fail (why?) then you should
probably write your parser to indicate it (you can also check that the
`begin` iterator is equal to `s.end()`):
if (qi::phrase_parse(begin, std::end(s), qi::double_ >> qi::eoi,
qi::space, d))
------------------------------------------------------------------------------