c – 在Boost Spirit中解码char UTF8转义
发布时间:2020-12-16 07:27:43 所属栏目:百科 来源:网络整理
导读:提问: Spirit-general list 大家好, 我不确定我的主题是否正确,但测试代码可能会显示 我想要实现的目标. 我正在尝试解析以下内容: ’@’到’@’ ’'到'' 我在下面有一个最小的测试用例.我不明白为什么 这不起作用.这可能是我犯了一个错误,但我没有看到它.
提问:
Spirit-general list
大家好, 我不确定我的主题是否正确,但测试代码可能会显示 我正在尝试解析以下内容: >’@’到’@’ 使用: 我使用以下编译行: g++ -o main -L/usr/src/boost-trunk/stage/lib -I/usr/src/boost-trunk -g -Werror -Wall -std=c++0x -DBOOST_SPIRIT_USE_PHOENIX_V3 main.cpp #include <iostream> #include <string> #define BOOST_SPIRIT_UNICODE #include <boost/cstdint.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/phoenix/phoenix.hpp> typedef boost::uint32_t uchar; // Unicode codepoint namespace qi = boost::spirit::qi; int main(int argc,char **argv) { // Input std::string input = "%3C"; std::string::const_iterator begin = input.begin(); std::string::const_iterator end = input.end(); using qi::xdigit; using qi::_1; using qi::_2; using qi::_val; qi::rule<std::string::const_iterator,uchar()> pchar = ('%' > xdigit > xdigit) [_val = (_1 << 4) + _2]; std::string result; bool r = qi::parse(begin,end,pchar,result); if (r && begin == end) { std::cout << "Output: " << result << std::endl; std::cout << "Expected: < (LESS-THAN SIGN)" << std::endl; } else { std::cerr << "Error" << std::endl; return 1; } return 0; } 问候, MatthijsM?hlmann 解决方法
qi :: xdigit没有做你认为它做的事情:它返回原始字符(即’0′,而不是0x00).
你可以利用 typedef qi::uint_parser<uchar,16,2,2> xuchar; >不需要依赖凤凰(使其适用于较旧版本的Boost) 这是一个固定的样本: #include <iostream> #include <string> #define BOOST_SPIRIT_UNICODE #include <boost/cstdint.hpp> #include <boost/spirit/include/qi.hpp> typedef boost::uint32_t uchar; // Unicode codepoint namespace qi = boost::spirit::qi; typedef qi::uint_parser<uchar,2> xuchar; const static xuchar xuchar_ = xuchar(); int main(int argc,char **argv) { // Input std::string input = "%3C"; std::string::const_iterator begin = input.begin(); std::string::const_iterator end = input.end(); qi::rule<std::string::const_iterator,uchar()> pchar = '%' > xuchar_; uchar result; bool r = qi::parse(begin,result); if (r && begin == end) { std::cout << "Output: " << result << std::endl; std::cout << "Expected: < (LESS-THAN SIGN)" << std::endl; } else { std::cerr << "Error" << std::endl; return 1; } return 0; } 输出: Output: 60 Expected: < (LESS-THAN SIGN) ‘<’确实是ASCII 60 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |