2012-12-03 28 views
5
#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)? का अध्ययन किया और गाइड का पालन करें, लेकिन शब्दों "你好"यूटीएफ -8 को पार्स करने के लिए boost :: spirit का उपयोग कैसे करें?

पार्स करने में विफल अपेक्षित परिणाम होना चाहिए

12,3 अब, सीडी जी, जी \ "जीजी केकेके 10, \" 0 99,987 पीपीपी 你好

लेकिन वास्तविक परिणाम ०१२३५१६४१०६१ हैं12,3 अब, सीडी जी, जी \ "जीजी केकेके 10, \" 0 99,987 पीपीपी

चीनी शब्द पार्स करने में विफल "你好"

ओएस Win7 64bits, है मेरी संपादक यूटीएफ -8

+1

मैं उलझन में हूँ। आप ... यूटीएफ 8 का उपयोग कर रहे हैं? तब wstring क्यों? (यूटीएफ 8 एक एन्कोडिंग सिंगल/डबल/ट्रिपल बाइट-अनुक्रम है, दाएं)। मैं बेहतर व्याख्या करने के लिए योग्य महसूस नहीं करता हूं, लेकिन यह मेरी धारणा – sehe

+0

1-4 बाइट्स में एक मेल नहीं है। लेकिन हाँ, यह काफी चमकदार मेल नहीं है। जब तक 'char8_t' पेश नहीं किया जाता है,' char' अधिकांश के लिए यूटीएफ -8 प्रकार का विकल्प होता है। – Puppy

+5

क्या हर कोई ने कहा। यूटीएफ -8 का उपयोग करते समय 'wstring' गलत है। यदि आप उचित रूप से यूटीएफ -8 अक्षरों को एन्कोड करना चाहते हैं, * विशेष रूप से * विंडोज़ पर, सबसे सुरक्षित तरीका सी ++ 11 अक्षर 'u8 "blah" '(जो अभी तक विजुअल स्टूडियो में नहीं है) या बाइट से बाएं से बचें "你好" के बजाय सीधे एन्कोडिंग, यानी "\ xE4 \ xBD \ xA0 \ xE5 \ xA5 \ xBD"। –

उत्तर

8

यदि आपके पास इनपुट पर यूटीएफ -8 है, तो आप Boost.Regex से Unicode Iterators का उपयोग करने का प्रयास कर सकते हैं।

उदाहरण के लिए, उपयोग को बढ़ावा देने :: u8_to_u32_iterator:

द्विदिश इटरेटर एडाप्टर करता है कि UTF8 वर्ण का एक अंतर्निहित अनुक्रम एक (केवल पढ़ने के लिए) UTF32 पात्रों के अनुक्रम की तरह लग रहे।

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; 
+0

+1 अच्छा काम सर – sehe