2 // Copyright (c) 2015 Artyom Beilis (Tonkikh)
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_LOCALE_GENERIC_CODECVT_HPP
9 #define BOOST_LOCALE_GENERIC_CODECVT_HPP
11 #include <boost/locale/utf.hpp>
12 #include <boost/cstdint.hpp>
13 #include <boost/static_assert.hpp>
19 #ifndef BOOST_LOCALE_DOXYGEN
21 // Make sure that mbstate can keep 16 bit of UTF-16 sequence
23 BOOST_STATIC_ASSERT(sizeof(std::mbstate_t)>=2);
26 #if defined(_MSC_VER) && _MSC_VER < 1700
27 // up to MSVC 11 (2012) do_length is non-standard it counts wide characters instead of narrow and does not change mbstate
28 #define BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
32 /// \brief A base class that used to define constants for generic_codecvt
34 class generic_codecvt_base {
37 /// Initail state for converting to or from unicode code points, used by initial_state in derived classes
39 enum initial_convertion_state {
40 to_unicode_state, ///< The state would be used by to_unicode functions
41 from_unicode_state ///< The state would be used by from_unicode functions
46 /// \brief Geneneric generic codecvt facet, various stateless encodings to UTF-16 and UTF-32 using wchar_t, char32_t and char16_t
48 /// Implementations should dervide from this class defining itself as CodecvtImpl and provide following members
50 /// - `state_type` - a type of special object that allows to store intermediate cached data, for example `iconv_t` descriptor
51 /// - `state_type initial_state(generic_codecvt_base::initial_convertion_state direction) const` - member function that creates initial state
52 /// - `int max_encoding_length() const` - a maximal length that one Unicode code point is represented, for UTF-8 for example it is 4 from ISO-8859-1 it is 1
53 /// - `utf::code_point to_unicode(state_type &state,char const *&begin,char const *end)` - extract first code point from the text in range [begin,end), in case of success begin would point to the next character sequence to be encoded to next code point, in case of incomplete sequence - utf::incomplete shell be returned, and in case of invalid input sequence utf::illegal shell be returned and begin would remain unmodified
54 /// - `utf::code_point from_unicode(state_type &state,utf::code_point u,char *begin,char const *end)` - convert a unicode code point `u` into a character seqnece at [begin,end). Return the length of the sequence in case of success, utf::incomplete in case of not enough room to encode the code point of utf::illegal in case conversion can not be performed
57 /// For example implementaion of codecvt for latin1/ISO-8859-1 character set
61 /// template<typename CharType>
62 /// class latin1_codecvt :boost::locale::generic_codecvt<CharType,latin1_codecvt<CharType> >
66 /// /* Standard codecvt constructor */
67 /// latin1_codecvt(size_t refs = 0) : boost::locale::generic_codecvt<CharType,latin1_codecvt<CharType> >(refs)
71 /// /* State is unused but required by generic_codecvt */
72 /// struct state_type {};
74 /// state_type initial_state(generic_codecvt_base::initial_convertion_state /*unused*/) const
76 /// return state_type();
79 /// int max_encoding_length() const
84 /// boost::locale::utf::code_point to_unicode(state_type &,char const *&begin,char const *end) const
87 /// return boost::locale::utf::incomplete;
91 /// boost::locale::utf::code_point from_unicode(state_type &,boost::locale::utf::code_point u,char *begin,char const *end) const
94 /// return boost::locale::utf::illegal;
96 /// return boost::locale::utf::incomplete;
104 /// When external tools used for encoding conversion, the `state_type` is useful to save objects used for conversions. For example,
105 /// icu::UConverter can be saved in such a state for an efficient use:
108 /// template<typename CharType>
109 /// class icu_codecvt :boost::locale::generic_codecvt<CharType,icu_codecvt<CharType> >
113 /// /* Standard codecvt constructor */
114 /// icu_codecvt(std::string const &name,refs = 0) :
115 /// boost::locale::generic_codecvt<CharType,latin1_codecvt<CharType> >(refs)
118 /// /* State is unused but required by generic_codecvt */
119 /// struct std::unique_ptr<UConverter,void (*)(UConverter*)> state_type;
121 /// state_type &&initial_state(generic_codecvt_base::initial_convertion_state /*unused*/) const
123 /// UErrorCode err = U_ZERO_ERROR;
124 /// state_type ptr(ucnv_safeClone(converter_,0,0,&err,ucnv_close);
125 /// return std::move(ptr);
128 /// boost::locale::utf::code_point to_unicode(state_type &ptr,char const *&begin,char const *end) const
130 /// UErrorCode err = U_ZERO_ERROR;
131 /// boost::locale::utf::code_point cp = ucnv_getNextUChar(ptr.get(),&begin,end,&err);
139 template<typename CharType,typename CodecvtImpl,int CharSize=sizeof(CharType)>
140 class generic_codecvt;
143 /// \brief UTF-16 to/from UTF-8 codecvt facet to use with char16_t or wchar_t on Windows
145 /// Note in order to fit the requirements of usability by std::wfstream it uses mbstate_t
146 /// to handle intermediate states in handling of variable length UTF-16 sequences
148 /// Its member functions implement standard virtual functions of basic codecvt
150 template<typename CharType,typename CodecvtImpl>
151 class generic_codecvt<CharType,CodecvtImpl,2> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
155 typedef CharType uchar;
157 generic_codecvt(size_t refs = 0) :
158 std::codecvt<CharType,char,std::mbstate_t>(refs)
161 CodecvtImpl const &implementation() const
163 return *static_cast<CodecvtImpl const *>(this);
169 virtual std::codecvt_base::result do_unshift(std::mbstate_t &s,char *from,char * /*to*/,char *&next) const
171 boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&s);
173 std::cout << "Entering unshift " << std::hex << state << std::dec << std::endl;
176 return std::codecvt_base::error;
178 return std::codecvt_base::ok;
180 virtual int do_encoding() const throw()
184 virtual int do_max_length() const throw()
186 return implementation().max_encoding_length();
188 virtual bool do_always_noconv() const throw()
194 do_length( std::mbstate_t
195 #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
200 char const *from_end,
203 #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
204 char const *save_from = from;
205 boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
207 size_t save_max = max;
208 boost::uint16_t state = *reinterpret_cast<boost::uint16_t const *>(&std_state);
211 typedef typename CodecvtImpl::state_type state_type;
212 state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
213 while(max > 0 && from < from_end){
214 char const *prev_from = from;
215 boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
216 if(ch==boost::locale::utf::incomplete || ch==boost::locale::utf::illegal) {
231 #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
232 return from - save_from;
234 return save_max - max;
239 virtual std::codecvt_base::result
240 do_in( std::mbstate_t &std_state,
242 char const *from_end,
243 char const *&from_next,
246 uchar *&to_next) const
248 std::codecvt_base::result r=std::codecvt_base::ok;
250 // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
251 // according to standard. We use it to keep a flag 0/1 for surrogate pair writing
253 // if 0 no code above >0xFFFF observed, of 1 a code above 0xFFFF observerd
254 // and first pair is written, but no input consumed
255 boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
256 typedef typename CodecvtImpl::state_type state_type;
257 state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
258 while(to < to_end && from < from_end)
261 std::cout << "Entering IN--------------" << std::endl;
262 std::cout << "State " << std::hex << state <<std::endl;
263 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
265 char const *from_saved = from;
267 uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
269 if(ch==boost::locale::utf::illegal) {
271 r=std::codecvt_base::error;
274 if(ch==boost::locale::utf::incomplete) {
276 r=std::codecvt_base::partial;
279 // Normal codepoints go direcly to stream
284 // for other codepoints we do following
286 // 1. We can't consume our input as we may find ourselfs
287 // in state where all input consumed but not all output written,i.e. only
288 // 1st pair is written
289 // 2. We only write first pair and mark this in the state, we also revert back
290 // the from pointer in order to make sure this codepoint would be read
291 // once again and then we would consume our input together with writing
292 // second surrogate pair
294 boost::uint16_t vh = ch >> 10;
295 boost::uint16_t vl = ch & 0x3FF;
296 boost::uint16_t w1 = vh + 0xD800;
297 boost::uint16_t w2 = vl + 0xDC00;
311 if(r == std::codecvt_base::ok && (from!=from_end || state!=0))
312 r = std::codecvt_base::partial;
314 std::cout << "Returning ";
316 case std::codecvt_base::ok:
317 std::cout << "ok" << std::endl;
319 case std::codecvt_base::partial:
320 std::cout << "partial" << std::endl;
322 case std::codecvt_base::error:
323 std::cout << "error" << std::endl;
326 std::cout << "other" << std::endl;
329 std::cout << "State " << std::hex << state <<std::endl;
330 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
335 virtual std::codecvt_base::result
336 do_out( std::mbstate_t &std_state,
338 uchar const *from_end,
339 uchar const *&from_next,
342 char *&to_next) const
344 std::codecvt_base::result r=std::codecvt_base::ok;
345 // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
346 // according to standard. We assume that sizeof(mbstate_t) >=2 in order
347 // to be able to store first observerd surrogate pair
349 // State: state!=0 - a first surrogate pair was observerd (state = first pair),
350 // we expect the second one to come and then zero the state
352 boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
353 typedef typename CodecvtImpl::state_type state_type;
354 state_type cvt_state = implementation().initial_state(generic_codecvt_base::from_unicode_state);
355 while(to < to_end && from < from_end)
358 std::cout << "Entering OUT --------------" << std::endl;
359 std::cout << "State " << std::hex << state <<std::endl;
360 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
362 boost::uint32_t ch=0;
364 // if the state idecates that 1st surrogate pair was written
365 // we should make sure that the second one that comes is actually
367 boost::uint16_t w1 = state;
368 boost::uint16_t w2 = *from;
369 // we don't forward from as writing may fail to incomplete or
370 // partial conversion
371 if(0xDC00 <= w2 && w2<=0xDFFF) {
372 boost::uint16_t vh = w1 - 0xD800;
373 boost::uint16_t vl = w2 - 0xDC00;
374 ch=((uint32_t(vh) << 10) | vl) + 0x10000;
378 r=std::codecvt_base::error;
384 if(0xD800 <= ch && ch<=0xDBFF) {
385 // if this is a first surrogate pair we put
386 // it into the state and consume it, note we don't
387 // go forward as it should be illegal so we increase
388 // the from pointer manually
393 else if(0xDC00 <= ch && ch<=0xDFFF) {
394 // if we observe second surrogate pair and
395 // first only may be expected we should break from the loop with error
396 // as it is illegal input
397 r=std::codecvt_base::error;
401 if(!boost::locale::utf::is_valid_codepoint(ch)) {
402 r=std::codecvt_base::error;
405 boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
406 if(len == boost::locale::utf::incomplete) {
407 r=std::codecvt_base::partial;
410 else if(len == boost::locale::utf::illegal) {
411 r=std::codecvt_base::error;
421 if(r==std::codecvt_base::ok && from!=from_end)
422 r = std::codecvt_base::partial;
424 std::cout << "Returning ";
426 case std::codecvt_base::ok:
427 std::cout << "ok" << std::endl;
429 case std::codecvt_base::partial:
430 std::cout << "partial" << std::endl;
432 case std::codecvt_base::error:
433 std::cout << "error" << std::endl;
436 std::cout << "other" << std::endl;
439 std::cout << "State " << std::hex << state <<std::endl;
440 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
448 /// \brief UTF-32 to/from UTF-8 codecvt facet to use with char32_t or wchar_t on POSIX platforms
450 /// Its member functions implement standard virtual functions of basic codecvt.
451 /// mbstate_t is not used for UTF-32 handling due to fixed length encoding
453 template<typename CharType,typename CodecvtImpl>
454 class generic_codecvt<CharType,CodecvtImpl,4> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
457 typedef CharType uchar;
459 generic_codecvt(size_t refs = 0) :
460 std::codecvt<CharType,char,std::mbstate_t>(refs)
464 CodecvtImpl const &implementation() const
466 return *static_cast<CodecvtImpl const *>(this);
471 virtual std::codecvt_base::result do_unshift(std::mbstate_t &/*s*/,char *from,char * /*to*/,char *&next) const
474 return std::codecvt_base::ok;
476 virtual int do_encoding() const throw()
480 virtual int do_max_length() const throw()
482 return implementation().max_encoding_length();
484 virtual bool do_always_noconv() const throw()
490 do_length( std::mbstate_t
491 #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
496 char const *from_end,
499 #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
500 char const *start_from = from;
502 size_t save_max = max;
504 typedef typename CodecvtImpl::state_type state_type;
505 state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
506 while(max > 0 && from < from_end){
507 char const *save_from = from;
508 boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
509 if(ch==boost::locale::utf::incomplete || ch==boost::locale::utf::illegal) {
515 #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
516 return from - start_from;
518 return save_max - max;
523 virtual std::codecvt_base::result
524 do_in( std::mbstate_t &/*state*/,
526 char const *from_end,
527 char const *&from_next,
530 uchar *&to_next) const
532 std::codecvt_base::result r=std::codecvt_base::ok;
534 // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
535 // according to standard. We use it to keep a flag 0/1 for surrogate pair writing
537 // if 0 no code above >0xFFFF observed, of 1 a code above 0xFFFF observerd
538 // and first pair is written, but no input consumed
539 typedef typename CodecvtImpl::state_type state_type;
540 state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
541 while(to < to_end && from < from_end)
544 std::cout << "Entering IN--------------" << std::endl;
545 std::cout << "State " << std::hex << state <<std::endl;
546 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
548 char const *from_saved = from;
550 uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
552 if(ch==boost::locale::utf::illegal) {
553 r=std::codecvt_base::error;
557 if(ch==boost::locale::utf::incomplete) {
558 r=std::codecvt_base::partial;
566 if(r == std::codecvt_base::ok && from!=from_end)
567 r = std::codecvt_base::partial;
569 std::cout << "Returning ";
571 case std::codecvt_base::ok:
572 std::cout << "ok" << std::endl;
574 case std::codecvt_base::partial:
575 std::cout << "partial" << std::endl;
577 case std::codecvt_base::error:
578 std::cout << "error" << std::endl;
581 std::cout << "other" << std::endl;
584 std::cout << "State " << std::hex << state <<std::endl;
585 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
590 virtual std::codecvt_base::result
591 do_out( std::mbstate_t &/*std_state*/,
593 uchar const *from_end,
594 uchar const *&from_next,
597 char *&to_next) const
599 std::codecvt_base::result r=std::codecvt_base::ok;
600 typedef typename CodecvtImpl::state_type state_type;
601 state_type cvt_state = implementation().initial_state(generic_codecvt_base::from_unicode_state);
602 while(to < to_end && from < from_end)
605 std::cout << "Entering OUT --------------" << std::endl;
606 std::cout << "State " << std::hex << state <<std::endl;
607 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
609 boost::uint32_t ch=0;
611 if(!boost::locale::utf::is_valid_codepoint(ch)) {
612 r=std::codecvt_base::error;
615 boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
616 if(len == boost::locale::utf::incomplete) {
617 r=std::codecvt_base::partial;
620 else if(len == boost::locale::utf::illegal) {
621 r=std::codecvt_base::error;
629 if(r==std::codecvt_base::ok && from!=from_end)
630 r = std::codecvt_base::partial;
632 std::cout << "Returning ";
634 case std::codecvt_base::ok:
635 std::cout << "ok" << std::endl;
637 case std::codecvt_base::partial:
638 std::cout << "partial" << std::endl;
640 case std::codecvt_base::error:
641 std::cout << "error" << std::endl;
644 std::cout << "other" << std::endl;
647 std::cout << "State " << std::hex << state <<std::endl;
648 std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
655 template<typename CharType,typename CodecvtImpl>
656 class generic_codecvt<CharType,CodecvtImpl,1> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
659 typedef CharType uchar;
661 CodecvtImpl const &implementation() const
663 return *static_cast<CodecvtImpl const *>(this);
666 generic_codecvt(size_t refs = 0) : std::codecvt<char,char,std::mbstate_t>(refs)
676 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4