Skip to content

Commit

Permalink
Merge pull request #374 from Kojoley/ts_real_policies
Browse files Browse the repository at this point in the history
ts_real_policies: Fixes and optimizations
  • Loading branch information
Kojoley committed Mar 15, 2018
2 parents 4213d53 + 02c3c30 commit 8085c08
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
20 changes: 7 additions & 13 deletions example/qi/reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,34 +155,28 @@ 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;

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;
}
Expand Down
21 changes: 7 additions & 14 deletions test/qi/real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,28 @@ 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;

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;
}
Expand Down
4 changes: 4 additions & 0 deletions test/qi/real3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
20 changes: 7 additions & 13 deletions test/x3/real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,28 @@ 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;

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;
}
Expand Down
4 changes: 4 additions & 0 deletions test/x3/real3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 8085c08

Please sign in to comment.