Use angle brackets for system headers
[TortoiseGit.git] / src / ResText / codecvt.cpp
blobd911d21497946348748ed4994a773fa00c3b7d0e
1 #include "codecvt.h"
3 using namespace std;
5 ucs2_conversion::result
6 ucs2_conversion::do_in(mbstate_t&,
7 const char* from, const char* from_end, const char*& from_next,
8 wchar_t* to, wchar_t* to_limit, wchar_t*& to_next) const
10 size_t max_input = (from_end - from) & ~1;
11 size_t max_output = (to_limit - to);
12 size_t count = min(max_input/2, max_output);
14 result res = ok;
16 from_next = from;
17 to_next = to;
18 for (;count--; from_next += 2, ++to_next) {
19 unsigned char c1 = *from_next, c2 = *(from_next + 1);
20 *to_next = c1 | c2 << 8;
22 if (to_next == to && from_next == from_end - 1) res = partial;
23 return res;
26 ucs2_conversion::result
27 ucs2_conversion::do_out(mbstate_t&,
28 const wchar_t* from, const wchar_t* from_end, const wchar_t*& from_next,
29 char* to, char* to_limit, char*& to_next) const
31 size_t max_input = (from_end - from);
32 size_t max_output = (to_limit - to) & ~1;
33 size_t count = min(max_input, max_output/2);
35 from_next = from;
36 to_next = to;
37 for (;count--; ++from_next, to_next += 2) {
38 *(to_next + 0) = (char)(*from_next & 0xFF);
39 *(to_next + 1) = (char)(*from_next >> 8 & 0xFF);
41 return ok;
44 typedef unsigned char uchar;
46 inline unsigned char
47 take_6_bits(unsigned value, size_t right_position)
49 return uchar((value >> right_position) & 63);
54 inline size_t
55 most_signifant_bit_position(unsigned value)
57 size_t result(0);
59 size_t half = 16;
60 for(; half > 0; half >>= 1) {
61 if (1u << (result + half) <= value ) result += half;
63 return result + 1;
64 //return *lower_bound(range(0u, 31u), \x -> (1 << x <= value));
69 utf8_conversion::result
70 utf8_conversion::do_in(mbstate_t&,
71 const char* from, const char* from_end, const char*& from_next,
72 wchar_t* to, wchar_t* to_limit, wchar_t*& to_next) const
74 from_next = from;
75 to_next = to;
77 for(; to_next < to_limit && from_next < from_end; ++to_next) {
79 if (uchar(*from_next) < 0x80) *to_next = uchar(*from_next++);
80 else {
82 // 111zxxxx : z = 0 xxxx are data bits
83 size_t zero_bit_pos = most_signifant_bit_position(~*from_next);
84 size_t extra_bytes = 7 - zero_bit_pos;
86 if (size_t(from_end - from_next) < extra_bytes + 1)
87 return partial;
89 *to_next = uchar(*from_next++) & (wchar_t)((1 << (zero_bit_pos - 1)) - 1);
91 for (;extra_bytes--; ++from_next) {
92 *to_next = *to_next << 6 | uchar(*from_next) & 63;
97 return ok;
101 utf8_conversion::result
102 utf8_conversion::do_out(mbstate_t&,
103 const wchar_t* from, const wchar_t* from_end, const wchar_t*& from_next,
104 char* to, char* to_limit, char*& to_next) const
106 from_next = from;
107 to_next = to;
109 for (;from_next < from_end; ++from_next) {
111 unsigned symbol = *from_next;
113 if (symbol < 0x7F) {
114 if (to_next < to_limit)
115 *to_next++ = (unsigned char)symbol;
116 else
117 return ok;
118 } else {
120 size_t msb_pos = most_signifant_bit_position(symbol);
121 size_t extra_bytes = msb_pos / 6;
123 if (size_t(to_limit - to_next) >= extra_bytes + 1) {
125 *to_next = uchar(0xFF80 >> extra_bytes);
126 *to_next++ |= take_6_bits(symbol, extra_bytes*6);
128 for(;extra_bytes--;)
129 *to_next++ = 0x80 | take_6_bits(symbol, extra_bytes*6);
131 else
132 return ok;
135 return ok;