1

I need to parse a wstring to int, float or a string itself. I found a question similar to mine Here but I don't understand how to use the parser. I don't find the test_parser method. The question is: after implementing the custom rules for the parser, how to use them?

Community
  • 1
  • 1
Stefano
  • 3,213
  • 9
  • 60
  • 101

1 Answers1

2

Parsing a wstring to an int is straight forward:

wstring str(L"1234");
int i = 0;
qi::parse(str.begin(), str.end(), qi::int_, i);
assert(i == 1234);

Similarily, parsing a float looks like:

wstring str(L"1234.567");
double d = 0;
qi::parse(str.begin(), str.end(), qi::double_, d);
assert(d == 1234.567);

I'm not sure what you mean by 'parsing a string'. If you mean it as parsing a quoted string you could write it as:

wstring str(L"\"abc\"");
wstring s;
qi::parse(str.begin(), str.end(), '"' >> *~qi::char_('"') >> '"', s);
assert(s == L"abc");

the expession '"' >> *~qi::char_('"') >> '"' means: a quote ('"') followed by (>>) zero or more (*) characters which are not a quote (~qi::char_('"')) followed by (>>) another quote ('"') .

hkaiser
  • 11,403
  • 1
  • 30
  • 35