c++ - 如何使用boost::spirit解析UTF-8?

标签 c++ boost unicode utf-8 boost-spirit

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define BOOST_SPIRIT_UNICODE // We'll use unicode (UTF8) all throughout

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/support_standard_wide.hpp>

void parse_simple_string()
{
    namespace qi = boost::spirit::qi;    
    namespace encoding  = boost::spirit::unicode;
    //namespace stw = boost::spirit::standard_wide;

    typedef std::wstring::const_iterator iterator_type;

    std::vector<std::wstring> result;
    std::wstring const input = LR"(12,3","ab,cd","G,G\"GG","kkk","10,\"0","99987","PPP","你好)";

    qi::rule<iterator_type, std::wstring()> key = +(qi::unicode::char_ - qi::lit(L"\",\""));
    qi::phrase_parse(input.begin(), input.end(),
                     key % qi::lit(L"\",\""),
                     encoding::space,
                     result);

    //std::copy(result.rbegin(), result.rend(), std::ostream_iterator<std::wstring, wchar_t>  (std::wcout, L"\n"));
    for(auto const &data : result) std::wcout<<data<<std::endl;
}

我研究了这篇文章How to use Boost Spirit to parse Chinese(unicode utf-16)? 并按照指南进行操作,但无法解析“你好”这个词

预期的结果应该是

12,3 A B C D G,G\"格格 kkk 10,\"0 99987 购买力平价 你好

但实际结果是 12,3 A B C D G,G\"格格 kkk 10,\"0 99987 购买力平价

中文单词“你好”解析失败

操作系统是win7 64bits,我的编辑器将文字保存为UTF-8

最佳答案

如果您输入的是 UTF-8,那么您可以尝试使用 Unicode Iterators来自 Boost.Regex .

例如,使用 boost::u8_to_u32_iterator:

A Bidirectional iterator adapter that makes an underlying sequence of UTF8 characters look like a (read-only) sequence of UTF32 characters.

live demo

#include <boost/regex/pending/unicode_iterator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/range.hpp>
#include <iterator>
#include <iostream>
#include <ostream>
#include <cstdint>
#include <vector>

int main()
{
    using namespace boost;
    using namespace spirit::qi;
    using namespace std;

    auto &&utf8_text=u8"你好,世界!";
    u8_to_u32_iterator<const char*>
        tbegin(begin(utf8_text)), tend(end(utf8_text));

    vector<uint32_t> result;
    parse(tbegin, tend, *standard_wide::char_, result);
    for(auto &&code_point : result)
        cout << "&#" << code_point << ";";
    cout << endl;
}

输出是:

&#20320;&#22909;&#65292;&#19990;&#30028;&#65281;&#0;

关于c++ - 如何使用boost::spirit解析UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13679669/

相关文章:

c++ - 音频混合算法改变音量

c++ - NSTask 的碳当量?

c++ - boost asio取消读取而不取消写入

windows - 如何在 cmd.exe 默认情况下制作 Unicode 字符集?

c++ - 如何使用 Alchemy 将 C++ 移植到 swf?

c++ - 如何在qt中使用QRegularExpression匹配字符串开头的空格?

c++ - FILE_NOTIFY_INFORMATION 不支持 Utf-8 文件名

android - 如何从 json 读取\uxxxx

c++ - 当前是否有任何包含众所周知的压缩算法的 C/C++ 库?

c++ - 为什么这个 boost 变换操作会把第二个元素弄错?