2
0
mirror of https://github.com/boostorg/spirit.git synced 2026-01-19 04:42:11 +00:00

Merge pull request #374 from Kojoley/ts_real_policies

ts_real_policies: Fixes and optimizations
This commit is contained in:
Nikita Kniazev
2018-03-15 03:52:39 +03:00
committed by GitHub
5 changed files with 29 additions and 40 deletions

View File

@@ -155,9 +155,9 @@ struct ts_real_policies : boost::spirit::qi::ureal_policies<T>
}
// Thousands separated numbers
template <typename Iterator, typename Attribute>
template <typename Iterator, typename Accumulator>
static bool
parse_n(Iterator& first, Iterator const& last, Attribute& attr)
parse_n(Iterator& first, Iterator const& last, Accumulator& result)
{
using boost::spirit::qi::uint_parser;
namespace qi = boost::spirit::qi;
@@ -165,24 +165,18 @@ struct ts_real_policies : boost::spirit::qi::ureal_policies<T>
uint_parser<unsigned, 10, 1, 3> uint3;
uint_parser<unsigned, 10, 3, 3> uint3_3;
T result = 0;
if (parse(first, last, uint3, result))
{
bool hit = false;
T n;
Iterator save = first;
Accumulator n;
Iterator iter = first;
while (qi::parse(first, last, ',') && qi::parse(first, last, uint3_3, n))
while (qi::parse(iter, last, ',') && qi::parse(iter, last, uint3_3, n))
{
result = result * 1000 + n;
save = first;
hit = true;
first = iter;
}
first = save;
if (hit)
attr = result;
return hit;
return true;
}
return false;
}

View File

@@ -57,9 +57,9 @@ struct ts_real_policies : boost::spirit::qi::ureal_policies<T>
}
// Thousands separated numbers
template <typename Iterator, typename Attribute>
template <typename Iterator, typename Accumulator>
static bool
parse_n(Iterator& first, Iterator const& last, Attribute& attr)
parse_n(Iterator& first, Iterator const& last, Accumulator& result)
{
using boost::spirit::qi::uint_parser;
namespace qi = boost::spirit::qi;
@@ -67,25 +67,18 @@ struct ts_real_policies : boost::spirit::qi::ureal_policies<T>
uint_parser<unsigned, 10, 1, 3> uint3;
uint_parser<unsigned, 10, 3, 3> uint3_3;
typedef typename boost::spirit::traits::real_accumulator<T>::type acc_type;
acc_type result = 0;
if (parse(first, last, uint3, result))
{
bool hit = false;
acc_type n;
Iterator save = first;
Accumulator n;
Iterator iter = first;
while (qi::parse(first, last, ',') && qi::parse(first, last, uint3_3, n))
while (qi::parse(iter, last, ',') && qi::parse(iter, last, uint3_3, n))
{
result = result * 1000 + n;
save = first;
hit = true;
first = iter;
}
first = save;
if (hit)
attr = result;
return hit;
return true;
}
return false;
}

View File

@@ -59,6 +59,10 @@ main()
real_parser<double, ts_real_policies<double> > ts_real;
double d;
BOOST_TEST(test("123.01", ts_real));
BOOST_TEST(test_attr("123.01", ts_real, d)
&& compare(d, 123.01));
BOOST_TEST(test("123,456,789.01", ts_real));
BOOST_TEST(test_attr("123,456,789.01", ts_real, d)
&& compare(d, 123456789.01));

View File

@@ -54,9 +54,9 @@ struct ts_real_policies : boost::spirit::x3::ureal_policies<T>
}
// Thousands separated numbers
template <typename Iterator, typename Attribute>
template <typename Iterator, typename Accumulator>
static bool
parse_n(Iterator& first, Iterator const& last, Attribute& attr)
parse_n(Iterator& first, Iterator const& last, Accumulator& result)
{
using boost::spirit::x3::uint_parser;
namespace x3 = boost::spirit::x3;
@@ -64,24 +64,18 @@ struct ts_real_policies : boost::spirit::x3::ureal_policies<T>
uint_parser<unsigned, 10, 1, 3> uint3;
uint_parser<unsigned, 10, 3, 3> uint3_3;
T result = 0;
if (parse(first, last, uint3, result))
{
bool hit = false;
T n;
Iterator save = first;
Accumulator n;
Iterator iter = first;
while (x3::parse(first, last, ',') && x3::parse(first, last, uint3_3, n))
while (x3::parse(iter, last, ',') && x3::parse(iter, last, uint3_3, n))
{
result = result * 1000 + n;
save = first;
hit = true;
first = iter;
}
first = save;
if (hit)
attr = result;
return hit;
return true;
}
return false;
}

View File

@@ -59,6 +59,10 @@ main()
real_parser<double, ts_real_policies<double> > ts_real;
double d;
BOOST_TEST(test("123.01", ts_real));
BOOST_TEST(test_attr("123.01", ts_real, d)
&& compare(d, 123.01));
BOOST_TEST(test("123,456,789.01", ts_real));
BOOST_TEST(test_attr("123,456,789.01", ts_real, d)
&& compare(d, 123456789.01));