How to parse a comma delimited number into an unsigned?
Juan Dent <[email protected]> Sun, 11 Oct 2020 16:09:27 +0000
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <BYAPR14MB3077F6063FDBAEF889D0141A9B060@BYAPR14MB3077.namprd14.prod.outlook.com> |
--===============3342328865451573821==
Content-Language: en-US
Content-Type: multipart/alternative;
boundary="_000_BYAPR14MB3077F6063FDBAEF889D0141A9B060BYAPR14MB3077namp_"
--_000_BYAPR14MB3077F6063FDBAEF889D0141A9B060BYAPR14MB3077namp_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Hi,
I have the grammar correct but it is giving me 3 output values in a vector =
instead of the whole number without comas=85
uint_parser<unsigned, 10, 1, 3> uint3_p; // 1..3 digits
uint_parser<unsigned, 10, 3, 3> uint3_3_p; // exactly 3 digits
vector<unsigned> values;
test_parser_attr("12,345,678", uint3_p >> *(',' >> uint3_3_p), values);
this calls :
template <typename P, typename T>
void test_parser_attr(
char const* input, P const& p, T& attr, bool full_match =3D true)
{
using boost::spirit::qi::parse;
char const* f(input);
char const* l(f + strlen(f));
if (parse(f, l, p, attr) && (!full_match || (f =3D=3D l)))
std::cout << "ok" << std::endl;
else
std::cout << "fail" << std::endl;
}
To get the number I have to add the vector elements being sure to multiply =
by the correct factor, like so:
unsigned sum{ 0 };
unsigned factor =3D 1;
for (int i =3D values.size() - 1; i >=3D 0; --i)
{
sum +=3D values[i] * factor;
factor *=3D 1000;
}
std::cout << sum << std::endl;
Is this the way or is there something similar or easier =96 maybe using rul=
es?
Thanks!!
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=3D550986> for Window=
s 10
--_000_BYAPR14MB3077F6063FDBAEF889D0141A9B060BYAPR14MB3077namp_
Content-Type: text/html; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<html xmlns:o=3D"urn:schemas-microsoft-com:office:office" xmlns:w=3D"urn:sc=
hemas-microsoft-com:office:word" xmlns:m=3D"http://schemas.microsoft.com/of=
fice/2004/12/omml" xmlns=3D"http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DWindows-1=
252">
<meta name=3D"Generator" content=3D"Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang=3D"EN-US" link=3D"blue" vlink=3D"#954F72">
<div class=3D"WordSection1">
<div>
<p class=3D"MsoNormal">Hi,</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">I have the grammar correct but it is giving me 3 out=
put values in a vector instead of the whole number without comas=85</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">uint_parser<unsigned, 10, 1, 3> uint3_p; =
// 1..3 digits</p>
<p class=3D"MsoNormal">uint_parser<unsigned, 10, 3, 3> uint3_3_p;&nbs=
p; // exactly 3 digits</p>
<p class=3D"MsoNormal">vector<unsigned> values;</p>
<p class=3D"MsoNormal">test_parser_attr("12,345,678", uint3_p >=
;> *(',' >> uint3_3_p), values);</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">this calls :</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">template <typename P, typename T></p>
<p class=3D"MsoNormal">void test_parser_attr(</p>
<p class=3D"MsoNormal"> char const* input, P const& p=
, T& attr, bool full_match =3D true)</p>
<p class=3D"MsoNormal">{</p>
<p class=3D"MsoNormal"> using boost::spirit::qi::parse;</=
p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal"> char const* f(input);</p>
<p class=3D"MsoNormal"> char const* l(f + strlen(f));</p>
<p class=3D"MsoNormal"> if (parse(f, l, p, attr) &&am=
p; (!full_match || (f =3D=3D l)))</p>
<p class=3D"MsoNormal"> std::cout=
<< "ok" << std::endl;</p>
<p class=3D"MsoNormal"> else</p>
<p class=3D"MsoNormal"> std::cout=
<< "fail" << std::endl;</p>
<p class=3D"MsoNormal">}</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">To get the number I have to add the vector elements =
being sure to multiply by the correct factor, like so:</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal"> unsigned =
sum{ 0 };</p>
<p class=3D"MsoNormal"> &nbs=
p; =
unsigned factor =3D 1;</p>
<p class=3D"MsoNormal"> &nbs=
p; =
for (int i =3D values.size() - 1; i >=3D 0; --i)</p>
<p class=3D"MsoNormal"> &nbs=
p; =
{</p>
<p class=3D"MsoNormal"> &nbs=
p; &=
nbsp; sum +=3D =
values[i] * factor;</p>
<p class=3D"MsoNormal"> &nbs=
p; &=
nbsp; factor *=
=3D 1000;</p>
<p class=3D"MsoNormal"> &nbs=
p; =
}</p>
<p class=3D"MsoNormal"> std::cout=
<< sum << std::endl;</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">Is this the way or is there something similar or eas=
ier =96 maybe using rules?</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">Thanks!!</p>
</div>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">Sent from <a href=3D"https://go.microsoft.com/fwlink=
/?LinkId=3D550986">
Mail</a> for Windows 10</p>
<p class=3D"MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>
--_000_BYAPR14MB3077F6063FDBAEF889D0141A9B060BYAPR14MB3077namp_--
--===============3342328865451573821==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============3342328865451573821==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general
--===============3342328865451573821==--