5 ucs2_conversion::result
\r
6 ucs2_conversion::do_in(mbstate_t&,
\r
7 const char* from, const char* from_end, const char*& from_next,
\r
8 wchar_t* to, wchar_t* to_limit, wchar_t*& to_next) const
\r
10 size_t max_input = (from_end - from) & ~1;
\r
11 size_t max_output = (to_limit - to);
\r
12 size_t count = min(max_input/2, max_output);
\r
18 for (;count--; from_next += 2, ++to_next) {
\r
19 unsigned char c1 = *from_next, c2 = *(from_next + 1);
\r
20 *to_next = c1 | c2 << 8;
\r
22 if (to_next == to && from_next == from_end - 1) res = partial;
\r
26 ucs2_conversion::result
\r
27 ucs2_conversion::do_out(mbstate_t&,
\r
28 const wchar_t* from, const wchar_t* from_end, const wchar_t*& from_next,
\r
29 char* to, char* to_limit, char*& to_next) const
\r
31 size_t max_input = (from_end - from);
\r
32 size_t max_output = (to_limit - to) & ~1;
\r
33 size_t count = min(max_input, max_output/2);
\r
37 for (;count--; ++from_next, to_next += 2) {
\r
38 *(to_next + 0) = (char)(*from_next & 0xFF);
\r
39 *(to_next + 1) = (char)(*from_next >> 8 & 0xFF);
\r
44 typedef unsigned char uchar;
\r
46 inline unsigned char
\r
47 take_6_bits(unsigned value, size_t right_position)
\r
49 return uchar((value >> right_position) & 63);
\r
55 most_signifant_bit_position(unsigned value)
\r
60 for(; half > 0; half >>= 1) {
\r
61 if (1u << (result + half) <= value ) result += half;
\r
64 //return *lower_bound(range(0u, 31u), \x -> (1 << x <= value));
\r
69 utf8_conversion::result
\r
70 utf8_conversion::do_in(mbstate_t&,
\r
71 const char* from, const char* from_end, const char*& from_next,
\r
72 wchar_t* to, wchar_t* to_limit, wchar_t*& to_next) const
\r
77 for(; to_next < to_limit && from_next < from_end; ++to_next) {
\r
79 if (uchar(*from_next) < 0x80) *to_next = uchar(*from_next++);
\r
82 // 111zxxxx : z = 0 xxxx are data bits
\r
83 size_t zero_bit_pos = most_signifant_bit_position(~*from_next);
\r
84 size_t extra_bytes = 7 - zero_bit_pos;
\r
86 if (size_t(from_end - from_next) < extra_bytes + 1)
\r
89 *to_next = uchar(*from_next++) & (wchar_t)((1 << (zero_bit_pos - 1)) - 1);
\r
91 for (;extra_bytes--; ++from_next) {
\r
92 *to_next = *to_next << 6 | uchar(*from_next) & 63;
\r
101 utf8_conversion::result
\r
102 utf8_conversion::do_out(mbstate_t&,
\r
103 const wchar_t* from, const wchar_t* from_end, const wchar_t*& from_next,
\r
104 char* to, char* to_limit, char*& to_next) const
\r
109 for (;from_next < from_end; ++from_next) {
\r
111 unsigned symbol = *from_next;
\r
113 if (symbol < 0x7F) {
\r
114 if (to_next < to_limit)
\r
115 *to_next++ = (unsigned char)symbol;
\r
120 size_t msb_pos = most_signifant_bit_position(symbol);
\r
121 size_t extra_bytes = msb_pos / 6;
\r
123 if (size_t(to_limit - to_next) >= extra_bytes + 1) {
\r
125 *to_next = uchar(0xFF80 >> extra_bytes);
\r
126 *to_next++ |= take_6_bits(symbol, extra_bytes*6);
\r
128 for(;extra_bytes--;)
\r
129 *to_next++ = 0x80 | take_6_bits(symbol, extra_bytes*6);
\r