我需要检查解析的 qi::uint_
的值是否小于 256。
我偶然发现了一个SO post概述了在解析基本类型后运行检查的以下语法(本例中为 qi::double_
)。
raw [ double_ [_val = _1] ] [ _pass = !isnan_(_val) && px::size(_1)<=4 ]
这里,raw[...]
返回一个指向解析的 qi::double_
值的迭代器,最终的语义操作用于“测试”结果值。
从前面的示例推断,我假设我可以使用类似的方法检查边界。
raw [ uint_ [_val = _1] ] [ _pass = _val<=256 ]
不幸的是,我收到以下错误。
boost.spirit.qi.bounds.cpp:51:105: error: invalid operands to binary expression ('const boost::spirit::_val_type'
(aka 'const actor<attribute<0> >') and 'int')
if (qi::parse(str.begin(), str.end(), qi::raw[qi::uint_[qi::_val = qi::_1]][qi::_pass = qi::_val<=256]).full)
~~~~~~~~^ ~~~
文档和示例对于基本解析器来说非常有用,但随着更高级的主题,它开始逐渐减少;比如这个。
如何从 qi::_val
转换或提取无符号整数值以针对 256 进行测试?
最佳答案
您错过了一个事实:raw[]
exposes an iterator range 。另一个答案使用了它,因为“额外”约束是指输入长度(以字符为单位)。
你不需要它,所以你宁愿直接使用类似的东西:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
int main ()
{
using It = std::string::const_iterator;
qi::rule<It, double()> r
= qi::double_ [ qi::_pass = (qi::_1 < 256.0), qi::_val = qi::_1 ];
for (std::string const s: { "1.23", ".123", "2.e6", "inf", "-inf", "3.2323", "nan" })
{
It f = s.begin(), l = s.end();
double result;
if (parse(f, l, r, result))
std::cout << "accepted: '" << s << "' -> " << result;
else std::cout << "rejected: '" << s << "'";
if (f!=l)
std::cout << " (remaining: '" << std::string(f,l) << "')\n";
else std::cout << "\n";
}
}
打印
accepted: '1.23' -> 1.23
accepted: '.123' -> 0.123
rejected: '2.e6' (remaining: '2.e6')
rejected: 'inf' (remaining: 'inf')
accepted: '-inf' -> -inf
accepted: '3.2323' -> 3.2323
rejected: 'nan' (remaining: 'nan')
注释:
[action1, action2]
是提供多个语句的 Phoenix 方式(在本例中与[action1][action2]
非常相似) .您甚至可以不使用
_val=
赋值,因为这就是默认属性传播。为了在语义操作规则上启用默认属性传播,请使用
operator%=
定义它:r %= qi::double_ [ qi::_pass = (qi::_1 < 256.0) ];
打印相同的输出。
关于c++ - Boost.Spirit.Qi - 针对原始数据类型的边界检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51272067/