- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
> boost::spirit::hex % '*' 题: 如何解析有两颗星的-6ren">
我的数据定义为:
std::string data("START34*23*43**");
"START" >> boost::spirit::hex % '*'
最佳答案
目前尚不清楚您在问什么。假设您只是想“忽略”(或接受)结尾的星号,这是您的罪魁祸首:
if (first != last) // fail if we did not get a full match
return false;
#include <boost/spirit/include/qi.hpp>
#include <iomanip>
namespace qi = boost::spirit::qi;
template <typename Iterator>
bool parse_numbers(Iterator& first, Iterator last, std::vector<unsigned>& v) {
return qi::phrase_parse(first, last, ("START" >> qi::hex % '*'), qi::space, v);
}
int main() {
for (std::string const data : {
"START34*23*43",
"START34 * 23 * 43",
"START34 * 23 * 43 *",
"START34 * 23 * 43**",
"START34 * 23 * 43* *",
})
{
auto f = data.begin(), l = data.end();
std::vector<unsigned> v;
if (parse_numbers(f, l, v)) {
std::cout << std::quoted(data) << " Parses OK: " << std::endl;
for (auto i = 0u; i < v.size(); ++i)
std::cout << i << ": " << v[i] << std::endl;
} else {
std::cout << "Parsing failed\n";
}
if (f != l) {
std::cout << "Remaining unparsed: "
<< std::quoted(std::string(f, l)) << "\n";
}
}
}
"START34*23*43" Parses OK:
0: 52
1: 35
2: 67
"START34 * 23 * 43" Parses OK:
0: 52
1: 35
2: 67
"START34 * 23 * 43 *" Parses OK:
0: 52
1: 35
2: 67
Remaining unparsed: "*"
"START34 * 23 * 43**" Parses OK:
0: 52
1: 35
2: 67
Remaining unparsed: "**"
"START34 * 23 * 43* *" Parses OK:
0: 52
1: 35
2: 67
Remaining unparsed: "* *"
-qi::hex % '*'
而不是
qi::hex % '*'
,这只是使
hex
成为可选的。
#include <boost/spirit/include/qi.hpp>
#include <iomanip>
namespace qi = boost::spirit::qi;
template <typename Iterator>
bool parse_numbers(Iterator& first, Iterator last, std::vector<unsigned>& v) {
return qi::phrase_parse(first, last,
("START" >> -qi::hex % '*'), qi::space, v);
}
int main() {
for (std::string const data : {
"START34**23*43",
"START34 * 23 * 43**",
"START*******",
"START*******1 BOGUS",
})
{
auto f = data.begin(), l = data.end();
std::vector<unsigned> v;
if (parse_numbers(f, l, v)) {
std::cout << std::quoted(data) << " Parses OK: " << std::endl;
for (auto i = 0u; i < v.size(); ++i)
std::cout << i << ": " << v[i] << std::endl;
} else {
std::cout << "Parsing failed\n";
}
if (f != l) {
std::cout << "Remaining unparsed: "
<< std::quoted(std::string(f, l)) << "\n";
}
}
}
"START34**23*43" Parses OK:
0: 52
1: 35
2: 67
"START34 * 23 * 43**" Parses OK:
0: 52
1: 35
2: 67
"START*******" Parses OK:
"START*******1 BOGUS" Parses OK:
0: 1
Remaining unparsed: "BOGUS"
In this case you might want to re-assert that all input is parsed with
>> qi::eoi
(which beats checking the iterators manually), see Live On Coliru:"START34**23*43" OK:
0: 52
1: 35
2: 67
"START34 * 23 * 43**" OK:
0: 52
1: 35
2: 67
"START*******" OK:
"START*******1 BOGUS" Failed
@sehe So someting like this: coliru.stacked-crooked.com/a/5ecc5462a8dc0081 – user3314011 19 mins ago
**
:
"START" >> (qi::hex % (qi::lit('*') - "**")) >> "**"
>
而不是
>>
):
try {
return qi::parse(first, last, "START" > (qi::hex % (qi::lit('*') - "**")) > "**" > qi::eoi, v);
} catch (qi::expectation_failure<Iterator> const& ef) {
std::ostringstream msg;
msg << "Expected " << ef.what_ << " at " << std::quoted(std::string(ef.first, ef.last), '\'');
throw ParseError(msg.str());
}
#include <boost/spirit/include/qi.hpp>
#include <iomanip>
namespace qi = boost::spirit::qi;
struct ParseError : std::runtime_error {
ParseError(std::string msg) : std::runtime_error(std::move(msg)) {}
};
template <typename Iterator>
bool parse_numbers(Iterator& first, Iterator last, std::vector<unsigned>& v) {
try {
return qi::parse(first, last, "START" > (qi::hex % (qi::lit('*') - "**")) > "**" > qi::eoi, v);
} catch (qi::expectation_failure<Iterator> const& ef) {
std::ostringstream msg;
msg << "Expected " << ef.what_ << " at " << std::quoted(std::string(ef.first, ef.last), '\'');
throw ParseError(msg.str());
}
}
int main() {
for (std::string const data : {
"START34*23*43", // Fail no EOM
"START34 * 23 * 43", // Fail spaces
"START34*23*43*", // Fail no EOM
"START34*23*43**", // OK
"START34*23*43**1", // Fail extra number
})
{
std::cout << std::quoted(data) << " -> ";
auto f = data.begin(), l = data.end();
std::vector<unsigned> v;
try {
if (parse_numbers(f, l, v)) {
std::cout << " OK:";
for (auto i : v)
std::cout << " " << i;
std::cout << "\n";
} else {
std::cout << "Not matched\n";
}
} catch(ParseError const& pe) {
std::cout << "Error: " << pe.what() << "\n";
}
}
}
"START34*23*43" -> Error: Expected "**" at ''
"START34 * 23 * 43" -> Error: Expected "**" at ' * 23 * 43'
"START34*23*43*" -> Error: Expected "**" at '*'
"START34*23*43**" -> OK: 52 35 67
"START34*23*43**1" -> Error: Expected <eoi> at '1'
关于c++ - Spirit QI解析器结束EOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61661013/
可以让x509Certificate不过期吗?如果是这样,怎么办? 最佳答案 没有。但只要您生成自己的 X509 证书,就可以使它们在未来很长一段时间内过期。 如果您购买真正的 X509 证书,则必须
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Can sizeof return 0 (zero) 在 C++ 中是否有合法的方法来定义零大小类型?
这个问题在这里已经有了答案: here-document gives 'unexpected end of file' error (10 个答案) 关闭 6 年前。 我在其中一个 linux 主机
我对 c++(11) 输入流有疑问,特别是文件流。在我用这个打开一个新流之后: ifstream stream; stream.open("C:\somefile.txt"); 当我尝试从中读取并且读
我是一名优秀的程序员,十分优秀!