123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- #ifndef BOOST_LOCALE_GENERIC_CODECVT_HPP
- #define BOOST_LOCALE_GENERIC_CODECVT_HPP
- #include <boost/locale/utf.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/static_assert.hpp>
- #include <locale>
- namespace boost {
- namespace locale {
- #ifndef BOOST_LOCALE_DOXYGEN
- BOOST_STATIC_ASSERT(sizeof(std::mbstate_t)>=2);
- #endif
- #if defined(_MSC_VER) && _MSC_VER < 1700
- #define BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
- #endif
- class generic_codecvt_base {
- public:
-
-
-
- enum initial_convertion_state {
- to_unicode_state,
- from_unicode_state
- };
- };
- template<typename CharType,typename CodecvtImpl,int CharSize=sizeof(CharType)>
- class generic_codecvt;
- template<typename CharType,typename CodecvtImpl>
- class generic_codecvt<CharType,CodecvtImpl,2> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
- {
- public:
- typedef CharType uchar;
- generic_codecvt(size_t refs = 0) :
- std::codecvt<CharType,char,std::mbstate_t>(refs)
- {
- }
- CodecvtImpl const &implementation() const
- {
- return *static_cast<CodecvtImpl const *>(this);
- }
- protected:
- virtual std::codecvt_base::result do_unshift(std::mbstate_t &s,char *from,char * ,char *&next) const
- {
- boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&s);
- #ifdef DEBUG_CODECVT
- std::cout << "Entering unshift " << std::hex << state << std::dec << std::endl;
- #endif
- if(state != 0)
- return std::codecvt_base::error;
- next=from;
- return std::codecvt_base::ok;
- }
- virtual int do_encoding() const throw()
- {
- return 0;
- }
- virtual int do_max_length() const throw()
- {
- return implementation().max_encoding_length();
- }
- virtual bool do_always_noconv() const throw()
- {
- return false;
- }
- virtual int
- do_length( std::mbstate_t
- #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
- const
- #endif
- &std_state,
- char const *from,
- char const *from_end,
- size_t max) const
- {
- #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
- char const *save_from = from;
- boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
- #else
- size_t save_max = max;
- boost::uint16_t state = *reinterpret_cast<boost::uint16_t const *>(&std_state);
- #endif
- typedef typename CodecvtImpl::state_type state_type;
- state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
- while(max > 0 && from < from_end){
- char const *prev_from = from;
- boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
- if(ch==boost::locale::utf::incomplete || ch==boost::locale::utf::illegal) {
- from = prev_from;
- break;
- }
- max --;
- if(ch > 0xFFFF) {
- if(state == 0) {
- from = prev_from;
- state = 1;
- }
- else {
- state = 0;
- }
- }
- }
- #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
- return from - save_from;
- #else
- return save_max - max;
- #endif
- }
-
- virtual std::codecvt_base::result
- do_in( std::mbstate_t &std_state,
- char const *from,
- char const *from_end,
- char const *&from_next,
- uchar *to,
- uchar *to_end,
- uchar *&to_next) const
- {
- std::codecvt_base::result r=std::codecvt_base::ok;
-
-
-
-
-
-
- boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
- typedef typename CodecvtImpl::state_type state_type;
- state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
- while(to < to_end && from < from_end)
- {
- #ifdef DEBUG_CODECVT
- std::cout << "Entering IN--------------" << std::endl;
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- char const *from_saved = from;
-
- uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
-
- if(ch==boost::locale::utf::illegal) {
- from = from_saved;
- r=std::codecvt_base::error;
- break;
- }
- if(ch==boost::locale::utf::incomplete) {
- from = from_saved;
- r=std::codecvt_base::partial;
- break;
- }
-
- if(ch <= 0xFFFF) {
- *to++=ch;
- }
- else {
-
-
-
-
-
-
-
-
-
- ch-=0x10000;
- boost::uint16_t vh = ch >> 10;
- boost::uint16_t vl = ch & 0x3FF;
- boost::uint16_t w1 = vh + 0xD800;
- boost::uint16_t w2 = vl + 0xDC00;
- if(state == 0) {
- from = from_saved;
- *to++ = w1;
- state = 1;
- }
- else {
- *to++ = w2;
- state = 0;
- }
- }
- }
- from_next=from;
- to_next=to;
- if(r == std::codecvt_base::ok && (from!=from_end || state!=0))
- r = std::codecvt_base::partial;
- #ifdef DEBUG_CODECVT
- std::cout << "Returning ";
- switch(r) {
- case std::codecvt_base::ok:
- std::cout << "ok" << std::endl;
- break;
- case std::codecvt_base::partial:
- std::cout << "partial" << std::endl;
- break;
- case std::codecvt_base::error:
- std::cout << "error" << std::endl;
- break;
- default:
- std::cout << "other" << std::endl;
- break;
- }
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- return r;
- }
-
- virtual std::codecvt_base::result
- do_out( std::mbstate_t &std_state,
- uchar const *from,
- uchar const *from_end,
- uchar const *&from_next,
- char *to,
- char *to_end,
- char *&to_next) const
- {
- std::codecvt_base::result r=std::codecvt_base::ok;
-
-
-
-
-
-
-
- boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
- typedef typename CodecvtImpl::state_type state_type;
- state_type cvt_state = implementation().initial_state(generic_codecvt_base::from_unicode_state);
- while(to < to_end && from < from_end)
- {
- #ifdef DEBUG_CODECVT
- std::cout << "Entering OUT --------------" << std::endl;
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- boost::uint32_t ch=0;
- if(state != 0) {
-
-
-
- boost::uint16_t w1 = state;
- boost::uint16_t w2 = *from;
-
-
- if(0xDC00 <= w2 && w2<=0xDFFF) {
- boost::uint16_t vh = w1 - 0xD800;
- boost::uint16_t vl = w2 - 0xDC00;
- ch=((uint32_t(vh) << 10) | vl) + 0x10000;
- }
- else {
-
- r=std::codecvt_base::error;
- break;
- }
- }
- else {
- ch = *from;
- if(0xD800 <= ch && ch<=0xDBFF) {
-
-
-
-
- state = ch;
- from++;
- continue;
- }
- else if(0xDC00 <= ch && ch<=0xDFFF) {
-
-
-
- r=std::codecvt_base::error;
- break;
- }
- }
- if(!boost::locale::utf::is_valid_codepoint(ch)) {
- r=std::codecvt_base::error;
- break;
- }
- boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
- if(len == boost::locale::utf::incomplete) {
- r=std::codecvt_base::partial;
- break;
- }
- else if(len == boost::locale::utf::illegal) {
- r=std::codecvt_base::error;
- break;
- }
- else
- to+= len;
- state = 0;
- from++;
- }
- from_next=from;
- to_next=to;
- if(r==std::codecvt_base::ok && from!=from_end)
- r = std::codecvt_base::partial;
- #ifdef DEBUG_CODECVT
- std::cout << "Returning ";
- switch(r) {
- case std::codecvt_base::ok:
- std::cout << "ok" << std::endl;
- break;
- case std::codecvt_base::partial:
- std::cout << "partial" << std::endl;
- break;
- case std::codecvt_base::error:
- std::cout << "error" << std::endl;
- break;
- default:
- std::cout << "other" << std::endl;
- break;
- }
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- return r;
- }
-
- };
- template<typename CharType,typename CodecvtImpl>
- class generic_codecvt<CharType,CodecvtImpl,4> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
- {
- public:
- typedef CharType uchar;
- generic_codecvt(size_t refs = 0) :
- std::codecvt<CharType,char,std::mbstate_t>(refs)
- {
- }
-
- CodecvtImpl const &implementation() const
- {
- return *static_cast<CodecvtImpl const *>(this);
- }
-
- protected:
- virtual std::codecvt_base::result do_unshift(std::mbstate_t &,char *from,char * ,char *&next) const
- {
- next=from;
- return std::codecvt_base::ok;
- }
- virtual int do_encoding() const throw()
- {
- return 0;
- }
- virtual int do_max_length() const throw()
- {
- return implementation().max_encoding_length();
- }
- virtual bool do_always_noconv() const throw()
- {
- return false;
- }
- virtual int
- do_length( std::mbstate_t
- #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
- const
- #endif
- &,
- char const *from,
- char const *from_end,
- size_t max) const
- {
- #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
- char const *start_from = from;
- #else
- size_t save_max = max;
- #endif
- typedef typename CodecvtImpl::state_type state_type;
- state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
- while(max > 0 && from < from_end){
- char const *save_from = from;
- boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
- if(ch==boost::locale::utf::incomplete || ch==boost::locale::utf::illegal) {
- from = save_from;
- break;
- }
- max--;
- }
- #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
- return from - start_from;
- #else
- return save_max - max;
- #endif
- }
-
- virtual std::codecvt_base::result
- do_in( std::mbstate_t &,
- char const *from,
- char const *from_end,
- char const *&from_next,
- uchar *to,
- uchar *to_end,
- uchar *&to_next) const
- {
- std::codecvt_base::result r=std::codecvt_base::ok;
-
-
-
-
-
-
- typedef typename CodecvtImpl::state_type state_type;
- state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
- while(to < to_end && from < from_end)
- {
- #ifdef DEBUG_CODECVT
- std::cout << "Entering IN--------------" << std::endl;
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- char const *from_saved = from;
-
- uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
-
- if(ch==boost::locale::utf::illegal) {
- r=std::codecvt_base::error;
- from = from_saved;
- break;
- }
- if(ch==boost::locale::utf::incomplete) {
- r=std::codecvt_base::partial;
- from=from_saved;
- break;
- }
- *to++=ch;
- }
- from_next=from;
- to_next=to;
- if(r == std::codecvt_base::ok && from!=from_end)
- r = std::codecvt_base::partial;
- #ifdef DEBUG_CODECVT
- std::cout << "Returning ";
- switch(r) {
- case std::codecvt_base::ok:
- std::cout << "ok" << std::endl;
- break;
- case std::codecvt_base::partial:
- std::cout << "partial" << std::endl;
- break;
- case std::codecvt_base::error:
- std::cout << "error" << std::endl;
- break;
- default:
- std::cout << "other" << std::endl;
- break;
- }
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- return r;
- }
-
- virtual std::codecvt_base::result
- do_out( std::mbstate_t &,
- uchar const *from,
- uchar const *from_end,
- uchar const *&from_next,
- char *to,
- char *to_end,
- char *&to_next) const
- {
- std::codecvt_base::result r=std::codecvt_base::ok;
- typedef typename CodecvtImpl::state_type state_type;
- state_type cvt_state = implementation().initial_state(generic_codecvt_base::from_unicode_state);
- while(to < to_end && from < from_end)
- {
- #ifdef DEBUG_CODECVT
- std::cout << "Entering OUT --------------" << std::endl;
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- boost::uint32_t ch=0;
- ch = *from;
- if(!boost::locale::utf::is_valid_codepoint(ch)) {
- r=std::codecvt_base::error;
- break;
- }
- boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
- if(len == boost::locale::utf::incomplete) {
- r=std::codecvt_base::partial;
- break;
- }
- else if(len == boost::locale::utf::illegal) {
- r=std::codecvt_base::error;
- break;
- }
- to+=len;
- from++;
- }
- from_next=from;
- to_next=to;
- if(r==std::codecvt_base::ok && from!=from_end)
- r = std::codecvt_base::partial;
- #ifdef DEBUG_CODECVT
- std::cout << "Returning ";
- switch(r) {
- case std::codecvt_base::ok:
- std::cout << "ok" << std::endl;
- break;
- case std::codecvt_base::partial:
- std::cout << "partial" << std::endl;
- break;
- case std::codecvt_base::error:
- std::cout << "error" << std::endl;
- break;
- default:
- std::cout << "other" << std::endl;
- break;
- }
- std::cout << "State " << std::hex << state <<std::endl;
- std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
- #endif
- return r;
- }
- };
- template<typename CharType,typename CodecvtImpl>
- class generic_codecvt<CharType,CodecvtImpl,1> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
- {
- public:
- typedef CharType uchar;
- CodecvtImpl const &implementation() const
- {
- return *static_cast<CodecvtImpl const *>(this);
- }
- generic_codecvt(size_t refs = 0) : std::codecvt<char,char,std::mbstate_t>(refs)
- {
- }
- };
- }
- }
- #endif
|