How to use Boost Spirit to parse Chinese(unicode utf-16)?

对着背影说爱祢 提交于 2019-11-29 11:30:56

You should use parsers/skippers from another namespace, not from ascii. I guess, in you case it should be standard_wide .

this code can parse chinese.

  #define BOOST_TEST_DYN_LINK 
  #define BOOST_SPIRIT_USE_PHOENIX_V3 
  #define BOOST_SPIRIT_UNICODE 
  #include <boost/config/warning_disable.hpp>
  #include <boost/spirit/include/qi.hpp>
  #include <boost/spirit/include/support_standard_wide.hpp>
  #include <boost/spirit/include/karma.hpp>
  #include <boost/spirit/include/qi_parse.hpp>
  #include <boost/phoenix.hpp>
  #include <boost/fusion/include/std_pair.hpp>
  #define BOOST_TEST_MODULE MyTest
  #include <boost/test/unit_test.hpp>
  using namespace std;

BOOST_AUTO_TEST_CASE( parse_chinese)
  {
          namespace qi        = boost::spirit::qi  ;
          namespace ascii     = boost::spirit::ascii ;
          namespace encoding  = boost::spirit::unicode;
          namespace px        = boost::phoenix ;
          using namespace qi::labels;
          std::wstring test=L"中国" ;
          std::wstring found ;
          qi::rule<wstring::iterator,wstring(),encoding::space_type> unicode_string;
          unicode_string = * qi::unicode::char_ [_val += _1 ]  ;   
          if( qi::phrase_parse(test.begin(),test.end(),
                      unicode_string,
                      encoding::space,
                      found)
            )   
          {   
              BOOST_CHECK(true);
              //std::cout << "OK:" << utf16to8(found) << std::endl  ;
          }
          else
          {
              BOOST_CHECK(false);
          }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!