8 #ifndef BOOSTER_LOCALE_GENERIC_CODECVT_HPP 9 #define BOOSTER_LOCALE_GENERIC_CODECVT_HPP 11 #include <booster/locale/utf.h> 12 #include <booster/cstdint.h> 19 #if defined(_MSC_VER) && _MSC_VER < 1700 21 #define BOOSTER_LOCALE_DO_LENGTH_MBSTATE_CONST 132 template<
typename CharType,
typename CodecvtImpl,
int CharSize=sizeof(CharType)>
143 template<
typename CharType,
typename CodecvtImpl>
148 typedef CharType uchar;
151 std::codecvt<CharType,char,std::mbstate_t>(refs)
154 CodecvtImpl
const &implementation()
const 156 return *
static_cast<CodecvtImpl
const *
>(
this);
162 virtual std::codecvt_base::result do_unshift(std::mbstate_t &s,
char *from,
char * ,
char *&next)
const 164 booster::uint16_t &state = *
reinterpret_cast<booster::uint16_t *
>(&s);
166 std::cout <<
"Entering unshift " << std::hex << state << std::dec << std::endl;
169 return std::codecvt_base::error;
171 return std::codecvt_base::ok;
173 virtual int do_encoding()
const throw()
177 virtual int do_max_length()
const throw()
179 return implementation().max_encoding_length();
181 virtual bool do_always_noconv()
const throw()
187 do_length( std::mbstate_t
188 #ifdef BOOSTER_LOCALE_DO_LENGTH_MBSTATE_CONST
193 char const *from_end,
196 #ifndef BOOSTER_LOCALE_DO_LENGTH_MBSTATE_CONST 197 char const *save_from = from;
198 booster::uint16_t &state = *
reinterpret_cast<booster::uint16_t *
>(&std_state);
200 size_t save_max = max;
201 booster::uint16_t state = *
reinterpret_cast<booster::uint16_t
const *
>(&std_state);
204 typedef typename CodecvtImpl::state_type state_type;
206 while(max > 0 && from < from_end){
207 char const *prev_from = from;
208 booster::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
224 #ifndef BOOSTER_LOCALE_DO_LENGTH_MBSTATE_CONST 225 return from - save_from;
227 return save_max - max;
232 virtual std::codecvt_base::result
233 do_in( std::mbstate_t &std_state,
235 char const *from_end,
236 char const *&from_next,
239 uchar *&to_next)
const 241 std::codecvt_base::result r=std::codecvt_base::ok;
248 booster::uint16_t &state = *
reinterpret_cast<booster::uint16_t *
>(&std_state);
249 typedef typename CodecvtImpl::state_type state_type;
251 while(to < to_end && from < from_end)
254 std::cout <<
"Entering IN--------------" << std::endl;
255 std::cout <<
"State " << std::hex << state <<std::endl;
256 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
258 char const *from_saved = from;
260 uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
264 r=std::codecvt_base::error;
269 r=std::codecvt_base::partial;
287 booster::uint16_t vh = ch >> 10;
288 booster::uint16_t vl = ch & 0x3FF;
289 booster::uint16_t w1 = vh + 0xD800;
290 booster::uint16_t w2 = vl + 0xDC00;
304 if(r == std::codecvt_base::ok && (from!=from_end || state!=0))
305 r = std::codecvt_base::partial;
307 std::cout <<
"Returning ";
309 case std::codecvt_base::ok:
310 std::cout <<
"ok" << std::endl;
312 case std::codecvt_base::partial:
313 std::cout <<
"partial" << std::endl;
315 case std::codecvt_base::error:
316 std::cout <<
"error" << std::endl;
319 std::cout <<
"other" << std::endl;
322 std::cout <<
"State " << std::hex << state <<std::endl;
323 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
328 virtual std::codecvt_base::result
329 do_out( std::mbstate_t &std_state,
331 uchar
const *from_end,
332 uchar
const *&from_next,
335 char *&to_next)
const 337 std::codecvt_base::result r=std::codecvt_base::ok;
345 booster::uint16_t &state = *
reinterpret_cast<booster::uint16_t *
>(&std_state);
346 typedef typename CodecvtImpl::state_type state_type;
348 while(to < to_end && from < from_end)
351 std::cout <<
"Entering OUT --------------" << std::endl;
352 std::cout <<
"State " << std::hex << state <<std::endl;
353 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
355 booster::uint32_t ch=0;
360 booster::uint16_t w1 = state;
361 booster::uint16_t w2 = *from;
364 if(0xDC00 <= w2 && w2<=0xDFFF) {
365 booster::uint16_t vh = w1 - 0xD800;
366 booster::uint16_t vl = w2 - 0xDC00;
367 ch=((uint32_t(vh) << 10) | vl) + 0x10000;
371 r=std::codecvt_base::error;
377 if(0xD800 <= ch && ch<=0xDBFF) {
386 else if(0xDC00 <= ch && ch<=0xDFFF) {
390 r=std::codecvt_base::error;
395 r=std::codecvt_base::error;
398 booster::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
400 r=std::codecvt_base::partial;
404 r=std::codecvt_base::error;
414 if(r==std::codecvt_base::ok && from!=from_end)
415 r = std::codecvt_base::partial;
417 std::cout <<
"Returning ";
419 case std::codecvt_base::ok:
420 std::cout <<
"ok" << std::endl;
422 case std::codecvt_base::partial:
423 std::cout <<
"partial" << std::endl;
425 case std::codecvt_base::error:
426 std::cout <<
"error" << std::endl;
429 std::cout <<
"other" << std::endl;
432 std::cout <<
"State " << std::hex << state <<std::endl;
433 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
446 template<
typename CharType,
typename CodecvtImpl>
450 typedef CharType uchar;
453 std::codecvt<CharType,char,std::mbstate_t>(refs)
457 CodecvtImpl
const &implementation()
const 459 return *
static_cast<CodecvtImpl
const *
>(
this);
464 virtual std::codecvt_base::result do_unshift(std::mbstate_t &,
char *from,
char * ,
char *&next)
const 467 return std::codecvt_base::ok;
469 virtual int do_encoding()
const throw()
473 virtual int do_max_length()
const throw()
475 return implementation().max_encoding_length();
477 virtual bool do_always_noconv()
const throw()
483 do_length( std::mbstate_t
484 #ifdef BOOSTER_LOCALE_DO_LENGTH_MBSTATE_CONST
489 char const *from_end,
492 #ifndef BOOSTER_LOCALE_DO_LENGTH_MBSTATE_CONST 493 char const *start_from = from;
495 size_t save_max = max;
497 typedef typename CodecvtImpl::state_type state_type;
499 while(max > 0 && from < from_end){
500 char const *save_from = from;
501 booster::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
508 #ifndef BOOSTER_LOCALE_DO_LENGTH_MBSTATE_CONST 509 return from - start_from;
511 return save_max - max;
516 virtual std::codecvt_base::result
517 do_in( std::mbstate_t &,
519 char const *from_end,
520 char const *&from_next,
523 uchar *&to_next)
const 525 std::codecvt_base::result r=std::codecvt_base::ok;
532 typedef typename CodecvtImpl::state_type state_type;
534 while(to < to_end && from < from_end)
537 std::cout <<
"Entering IN--------------" << std::endl;
538 std::cout <<
"State " << std::hex << state <<std::endl;
539 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
541 char const *from_saved = from;
543 uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
546 r=std::codecvt_base::error;
551 r=std::codecvt_base::partial;
559 if(r == std::codecvt_base::ok && from!=from_end)
560 r = std::codecvt_base::partial;
562 std::cout <<
"Returning ";
564 case std::codecvt_base::ok:
565 std::cout <<
"ok" << std::endl;
567 case std::codecvt_base::partial:
568 std::cout <<
"partial" << std::endl;
570 case std::codecvt_base::error:
571 std::cout <<
"error" << std::endl;
574 std::cout <<
"other" << std::endl;
577 std::cout <<
"State " << std::hex << state <<std::endl;
578 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
583 virtual std::codecvt_base::result
584 do_out( std::mbstate_t &,
586 uchar
const *from_end,
587 uchar
const *&from_next,
590 char *&to_next)
const 592 std::codecvt_base::result r=std::codecvt_base::ok;
593 typedef typename CodecvtImpl::state_type state_type;
595 while(to < to_end && from < from_end)
598 std::cout <<
"Entering OUT --------------" << std::endl;
599 std::cout <<
"State " << std::hex << state <<std::endl;
600 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
602 booster::uint32_t ch=0;
605 r=std::codecvt_base::error;
608 booster::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
610 r=std::codecvt_base::partial;
614 r=std::codecvt_base::error;
622 if(r==std::codecvt_base::ok && from!=from_end)
623 r = std::codecvt_base::partial;
625 std::cout <<
"Returning ";
627 case std::codecvt_base::ok:
628 std::cout <<
"ok" << std::endl;
630 case std::codecvt_base::partial:
631 std::cout <<
"partial" << std::endl;
633 case std::codecvt_base::error:
634 std::cout <<
"error" << std::endl;
637 std::cout <<
"other" << std::endl;
640 std::cout <<
"State " << std::hex << state <<std::endl;
641 std::cout <<
"Left in " << std::dec << from_end - from <<
" out " << to_end -to << std::endl;
648 template<
typename CharType,
typename CodecvtImpl>
652 typedef CharType uchar;
654 CodecvtImpl
const &implementation()
const 656 return *
static_cast<CodecvtImpl
const *
>(
this);
659 generic_codecvt(
size_t refs = 0) : std::codecvt<char,char,std::mbstate_t>(refs)
initial_convertion_state
Definition: generic_codecvt.h:32
Geneneric generic codecvt facet, various stateless encodings to UTF-16 and UTF-32 using wchar_t...
Definition: generic_codecvt.h:133
static const code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.h:44
static const code_point illegal
Special constant that defines illegal code point.
Definition: utf.h:39
bool is_valid_codepoint(code_point v)
the function checks if v is a valid code point
Definition: utf.h:49
A base class that used to define constants for generic_codecvt.
Definition: generic_codecvt.h:27
The state would be used by from_unicode functions.
Definition: generic_codecvt.h:34
This is the main namespace that encloses all localization classes.
Definition: locale_fwd.h:14
The state would be used by to_unicode functions.
Definition: generic_codecvt.h:33
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23