我有一个类似的问题,比如如何使用boost :: spirit来解析UTF-8?以及如何使用boost :: spirit匹配unicode字符?但这些都没有解决我面临的问题.我有一个std::stringUTF-8字符,我用它u8_to_u32_iterator来包装std::string和使用这样的unicode终端:
BOOST_NETWORK_INLINE void parse_headers(std::string const & input, std::vector<request_header_narrow> & container) {
using namespace boost::spirit::qi;
u8_to_u32_iterator<std::string::const_iterator> begin(input.begin()), end(input.end());
std::vector<request_header_narrow_utf8_wrapper> wrapper_container;
parse(
begin, end,
*(
+(alnum|(punct-':'))
>> lit(": ")
>> +((unicode::alnum|space|punct) - '\r' - '\n')
>> lit("\r\n")
)
>> lit("\r\n")
, wrapper_container
);
BOOST_FOREACH(request_header_narrow_utf8_wrapper header_wrapper, wrapper_container)
{
request_header_narrow header;
u32_to_u8_iterator<request_header_narrow_utf8_wrapper::string_type::iterator> name_begin(header_wrapper.name.begin()),
name_end(header_wrapper.name.end()),
value_begin(header_wrapper.value.begin()),
value_end(header_wrapper.value.end());
for(; name_begin != name_end; ++name_begin)
header.name += *name_begin;
for(; value_begin != value_end; …Run Code Online (Sandbox Code Playgroud) 我的节目不承认中文.如何使用精神来认识中国人?我使用wstring并将其转换为utf-16.
这是我的头文件:
#pragma once
#define BOOST_SPIRIT_UNICODE
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <vector>
#include <map>
using namespace std;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
typedef pair<wstring,wstring> WordMeaningType;
typedef vector<WordMeaningType> WordMeaningsType;
typedef pair<wstring,WordMeaningsType> WordType;
typedef vector<WordType> WordListType;
struct WordPaser
:qi::grammar<wstring::iterator,WordListType(),ascii::space_type >
{
public:
qi::rule<wstring::iterator, wstring(),ascii::space_type> mRuleWordPart;
qi::rule<wstring::iterator, wstring(),ascii::space_type> mRuleWordMeaning;
qi::rule<wstring::iterator, wstring(),ascii::space_type> mRuleWord;
qi::rule<wstring::iterator, WordMeaningType(),ascii::space_type> mRulePM;
qi::rule<wstring::iterator, WordMeaningsType(),ascii::space_type> mRulePMs;
qi::rule<wstring::iterator, WordType(),ascii::space_type> mRuleCurWPM;
qi::rule<wstring::iterator, WordListType(),ascii::space_type> mRuleEntrence;
wstring mCurWord;
wstring mCurWordPart;
wstring mCurWordMeaning;
WordMeaningType mCurPM;
WordMeaningsType mCurPMs;
WordType mCurWPM;
WordPaser();
}; …Run Code Online (Sandbox Code Playgroud)