Chris@16
|
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
2 // utf8_codecvt_facet.ipp
|
Chris@16
|
3
|
Chris@16
|
4 // Copyright (c) 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
|
Chris@16
|
5 // Andrew Lumsdaine, Indiana University (lums@osl.iu.edu).
|
Chris@16
|
6 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
7 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9
|
Chris@16
|
10 // Please see the comments in <boost/detail/utf8_codecvt_facet.hpp> to
|
Chris@16
|
11 // learn how this file should be used.
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/detail/utf8_codecvt_facet.hpp>
|
Chris@16
|
14
|
Chris@16
|
15 #include <cstdlib> // for multi-byte converson routines
|
Chris@16
|
16 #include <cassert>
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/limits.hpp>
|
Chris@16
|
19 #include <boost/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 // If we don't have wstring, then Unicode support
|
Chris@16
|
22 // is not available anyway, so we don't need to even
|
Chris@16
|
23 // compiler this file. This also fixes the problem
|
Chris@16
|
24 // with mingw, which can compile this file, but will
|
Chris@16
|
25 // generate link error when building DLL.
|
Chris@16
|
26 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
27
|
Chris@16
|
28 BOOST_UTF8_BEGIN_NAMESPACE
|
Chris@16
|
29
|
Chris@16
|
30 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
31 // implementation for wchar_t
|
Chris@16
|
32
|
Chris@16
|
33 // Translate incoming UTF-8 into UCS-4
|
Chris@16
|
34 std::codecvt_base::result utf8_codecvt_facet::do_in(
|
Chris@16
|
35 std::mbstate_t& /*state*/,
|
Chris@16
|
36 const char * from,
|
Chris@16
|
37 const char * from_end,
|
Chris@16
|
38 const char * & from_next,
|
Chris@16
|
39 wchar_t * to,
|
Chris@16
|
40 wchar_t * to_end,
|
Chris@16
|
41 wchar_t * & to_next
|
Chris@16
|
42 ) const {
|
Chris@16
|
43 // Basic algorithm: The first octet determines how many
|
Chris@16
|
44 // octets total make up the UCS-4 character. The remaining
|
Chris@16
|
45 // "continuing octets" all begin with "10". To convert, subtract
|
Chris@16
|
46 // the amount that specifies the number of octets from the first
|
Chris@16
|
47 // octet. Subtract 0x80 (1000 0000) from each continuing octet,
|
Chris@16
|
48 // then mash the whole lot together. Note that each continuing
|
Chris@16
|
49 // octet only uses 6 bits as unique values, so only shift by
|
Chris@16
|
50 // multiples of 6 to combine.
|
Chris@16
|
51 while (from != from_end && to != to_end) {
|
Chris@16
|
52
|
Chris@16
|
53 // Error checking on the first octet
|
Chris@16
|
54 if (invalid_leading_octet(*from)){
|
Chris@16
|
55 from_next = from;
|
Chris@16
|
56 to_next = to;
|
Chris@16
|
57 return std::codecvt_base::error;
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@16
|
60 // The first octet is adjusted by a value dependent upon
|
Chris@16
|
61 // the number of "continuing octets" encoding the character
|
Chris@16
|
62 const int cont_octet_count = get_cont_octet_count(*from);
|
Chris@16
|
63 const wchar_t octet1_modifier_table[] = {
|
Chris@16
|
64 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 // The unsigned char conversion is necessary in case char is
|
Chris@16
|
68 // signed (I learned this the hard way)
|
Chris@16
|
69 wchar_t ucs_result =
|
Chris@16
|
70 (unsigned char)(*from++) - octet1_modifier_table[cont_octet_count];
|
Chris@16
|
71
|
Chris@16
|
72 // Invariants :
|
Chris@16
|
73 // 1) At the start of the loop, 'i' continuing characters have been
|
Chris@16
|
74 // processed
|
Chris@16
|
75 // 2) *from points to the next continuing character to be processed.
|
Chris@16
|
76 int i = 0;
|
Chris@16
|
77 while(i != cont_octet_count && from != from_end) {
|
Chris@16
|
78
|
Chris@16
|
79 // Error checking on continuing characters
|
Chris@16
|
80 if (invalid_continuing_octet(*from)) {
|
Chris@16
|
81 from_next = from;
|
Chris@16
|
82 to_next = to;
|
Chris@16
|
83 return std::codecvt_base::error;
|
Chris@16
|
84 }
|
Chris@16
|
85
|
Chris@16
|
86 ucs_result *= (1 << 6);
|
Chris@16
|
87
|
Chris@16
|
88 // each continuing character has an extra (10xxxxxx)b attached to
|
Chris@16
|
89 // it that must be removed.
|
Chris@16
|
90 ucs_result += (unsigned char)(*from++) - 0x80;
|
Chris@16
|
91 ++i;
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 // If the buffer ends with an incomplete unicode character...
|
Chris@16
|
95 if (from == from_end && i != cont_octet_count) {
|
Chris@16
|
96 // rewind "from" to before the current character translation
|
Chris@16
|
97 from_next = from - (i+1);
|
Chris@16
|
98 to_next = to;
|
Chris@16
|
99 return std::codecvt_base::partial;
|
Chris@16
|
100 }
|
Chris@16
|
101 *to++ = ucs_result;
|
Chris@16
|
102 }
|
Chris@16
|
103 from_next = from;
|
Chris@16
|
104 to_next = to;
|
Chris@16
|
105
|
Chris@16
|
106 // Were we done converting or did we run out of destination space?
|
Chris@16
|
107 if(from == from_end) return std::codecvt_base::ok;
|
Chris@16
|
108 else return std::codecvt_base::partial;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 std::codecvt_base::result utf8_codecvt_facet::do_out(
|
Chris@16
|
112 std::mbstate_t& /*state*/,
|
Chris@16
|
113 const wchar_t * from,
|
Chris@16
|
114 const wchar_t * from_end,
|
Chris@16
|
115 const wchar_t * & from_next,
|
Chris@16
|
116 char * to,
|
Chris@16
|
117 char * to_end,
|
Chris@16
|
118 char * & to_next
|
Chris@16
|
119 ) const
|
Chris@16
|
120 {
|
Chris@16
|
121 // RG - consider merging this table with the other one
|
Chris@16
|
122 const wchar_t octet1_modifier_table[] = {
|
Chris@16
|
123 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
|
Chris@16
|
124 };
|
Chris@16
|
125
|
Chris@16
|
126 wchar_t max_wchar = (std::numeric_limits<wchar_t>::max)();
|
Chris@16
|
127 while (from != from_end && to != to_end) {
|
Chris@16
|
128
|
Chris@16
|
129 // Check for invalid UCS-4 character
|
Chris@16
|
130 if (*from > max_wchar) {
|
Chris@16
|
131 from_next = from;
|
Chris@16
|
132 to_next = to;
|
Chris@16
|
133 return std::codecvt_base::error;
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 int cont_octet_count = get_cont_octet_out_count(*from);
|
Chris@16
|
137
|
Chris@16
|
138 // RG - comment this formula better
|
Chris@16
|
139 int shift_exponent = (cont_octet_count) * 6;
|
Chris@16
|
140
|
Chris@16
|
141 // Process the first character
|
Chris@16
|
142 *to++ = static_cast<char>(octet1_modifier_table[cont_octet_count] +
|
Chris@16
|
143 (unsigned char)(*from / (1 << shift_exponent)));
|
Chris@16
|
144
|
Chris@16
|
145 // Process the continuation characters
|
Chris@16
|
146 // Invariants: At the start of the loop:
|
Chris@16
|
147 // 1) 'i' continuing octets have been generated
|
Chris@16
|
148 // 2) '*to' points to the next location to place an octet
|
Chris@16
|
149 // 3) shift_exponent is 6 more than needed for the next octet
|
Chris@16
|
150 int i = 0;
|
Chris@16
|
151 while (i != cont_octet_count && to != to_end) {
|
Chris@16
|
152 shift_exponent -= 6;
|
Chris@16
|
153 *to++ = static_cast<char>(0x80 + ((*from / (1 << shift_exponent)) % (1 << 6)));
|
Chris@16
|
154 ++i;
|
Chris@16
|
155 }
|
Chris@16
|
156 // If we filled up the out buffer before encoding the character
|
Chris@16
|
157 if(to == to_end && i != cont_octet_count) {
|
Chris@16
|
158 from_next = from;
|
Chris@16
|
159 to_next = to - (i+1);
|
Chris@16
|
160 return std::codecvt_base::partial;
|
Chris@16
|
161 }
|
Chris@16
|
162 ++from;
|
Chris@16
|
163 }
|
Chris@16
|
164 from_next = from;
|
Chris@16
|
165 to_next = to;
|
Chris@16
|
166 // Were we done or did we run out of destination space
|
Chris@16
|
167 if(from == from_end) return std::codecvt_base::ok;
|
Chris@16
|
168 else return std::codecvt_base::partial;
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 // How many char objects can I process to get <= max_limit
|
Chris@16
|
172 // wchar_t objects?
|
Chris@16
|
173 int utf8_codecvt_facet::do_length(
|
Chris@101
|
174 const std::mbstate_t &,
|
Chris@16
|
175 const char * from,
|
Chris@16
|
176 const char * from_end,
|
Chris@16
|
177 std::size_t max_limit
|
Chris@101
|
178 ) const
|
Chris@16
|
179 #if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
|
Chris@101
|
180 throw()
|
Chris@16
|
181 #endif
|
Chris@16
|
182 {
|
Chris@16
|
183 // RG - this code is confusing! I need a better way to express it.
|
Chris@16
|
184 // and test cases.
|
Chris@16
|
185
|
Chris@16
|
186 // Invariants:
|
Chris@16
|
187 // 1) last_octet_count has the size of the last measured character
|
Chris@16
|
188 // 2) char_count holds the number of characters shown to fit
|
Chris@16
|
189 // within the bounds so far (no greater than max_limit)
|
Chris@16
|
190 // 3) from_next points to the octet 'last_octet_count' before the
|
Chris@16
|
191 // last measured character.
|
Chris@16
|
192 int last_octet_count=0;
|
Chris@16
|
193 std::size_t char_count = 0;
|
Chris@16
|
194 const char* from_next = from;
|
Chris@16
|
195 // Use "<" because the buffer may represent incomplete characters
|
Chris@16
|
196 while (from_next+last_octet_count <= from_end && char_count <= max_limit) {
|
Chris@16
|
197 from_next += last_octet_count;
|
Chris@16
|
198 last_octet_count = (get_octet_count(*from_next));
|
Chris@16
|
199 ++char_count;
|
Chris@16
|
200 }
|
Chris@16
|
201 return static_cast<int>(from_next-from_end);
|
Chris@16
|
202 }
|
Chris@16
|
203
|
Chris@16
|
204 unsigned int utf8_codecvt_facet::get_octet_count(
|
Chris@16
|
205 unsigned char lead_octet
|
Chris@16
|
206 ){
|
Chris@16
|
207 // if the 0-bit (MSB) is 0, then 1 character
|
Chris@16
|
208 if (lead_octet <= 0x7f) return 1;
|
Chris@16
|
209
|
Chris@16
|
210 // Otherwise the count number of consecutive 1 bits starting at MSB
|
Chris@16
|
211 // assert(0xc0 <= lead_octet && lead_octet <= 0xfd);
|
Chris@16
|
212
|
Chris@16
|
213 if (0xc0 <= lead_octet && lead_octet <= 0xdf) return 2;
|
Chris@16
|
214 else if (0xe0 <= lead_octet && lead_octet <= 0xef) return 3;
|
Chris@16
|
215 else if (0xf0 <= lead_octet && lead_octet <= 0xf7) return 4;
|
Chris@16
|
216 else if (0xf8 <= lead_octet && lead_octet <= 0xfb) return 5;
|
Chris@16
|
217 else return 6;
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@101
|
220 namespace detail {
|
Chris@101
|
221
|
Chris@16
|
222 template<std::size_t s>
|
Chris@16
|
223 int get_cont_octet_out_count_impl(wchar_t word){
|
Chris@16
|
224 if (word < 0x80) {
|
Chris@16
|
225 return 0;
|
Chris@16
|
226 }
|
Chris@16
|
227 if (word < 0x800) {
|
Chris@16
|
228 return 1;
|
Chris@16
|
229 }
|
Chris@16
|
230 return 2;
|
Chris@16
|
231 }
|
Chris@16
|
232
|
Chris@16
|
233 template<>
|
Chris@16
|
234 int get_cont_octet_out_count_impl<4>(wchar_t word){
|
Chris@16
|
235 if (word < 0x80) {
|
Chris@16
|
236 return 0;
|
Chris@16
|
237 }
|
Chris@16
|
238 if (word < 0x800) {
|
Chris@16
|
239 return 1;
|
Chris@16
|
240 }
|
Chris@16
|
241
|
Chris@16
|
242 // Note that the following code will generate warnings on some platforms
|
Chris@16
|
243 // where wchar_t is defined as UCS2. The warnings are superfluous as the
|
Chris@16
|
244 // specialization is never instantitiated with such compilers, but this
|
Chris@16
|
245 // can cause problems if warnings are being treated as errors, so we guard
|
Chris@16
|
246 // against that. Including <boost/detail/utf8_codecvt_facet.hpp> as we do
|
Chris@16
|
247 // should be enough to get WCHAR_MAX defined.
|
Chris@16
|
248 #if !defined(WCHAR_MAX)
|
Chris@16
|
249 # error WCHAR_MAX not defined!
|
Chris@16
|
250 #endif
|
Chris@16
|
251 // cope with VC++ 7.1 or earlier having invalid WCHAR_MAX
|
Chris@16
|
252 #if defined(_MSC_VER) && _MSC_VER <= 1310 // 7.1 or earlier
|
Chris@16
|
253 return 2;
|
Chris@16
|
254 #elif WCHAR_MAX > 0x10000
|
Chris@16
|
255
|
Chris@16
|
256 if (word < 0x10000) {
|
Chris@16
|
257 return 2;
|
Chris@16
|
258 }
|
Chris@16
|
259 if (word < 0x200000) {
|
Chris@16
|
260 return 3;
|
Chris@16
|
261 }
|
Chris@16
|
262 if (word < 0x4000000) {
|
Chris@16
|
263 return 4;
|
Chris@16
|
264 }
|
Chris@16
|
265 return 5;
|
Chris@16
|
266
|
Chris@16
|
267 #else
|
Chris@16
|
268 return 2;
|
Chris@16
|
269 #endif
|
Chris@16
|
270 }
|
Chris@16
|
271
|
Chris@101
|
272 } // namespace detail
|
Chris@16
|
273
|
Chris@16
|
274 // How many "continuing octets" will be needed for this word
|
Chris@16
|
275 // == total octets - 1.
|
Chris@16
|
276 int utf8_codecvt_facet::get_cont_octet_out_count(
|
Chris@16
|
277 wchar_t word
|
Chris@16
|
278 ) const {
|
Chris@101
|
279 return detail::get_cont_octet_out_count_impl<sizeof(wchar_t)>(word);
|
Chris@16
|
280 }
|
Chris@16
|
281 BOOST_UTF8_END_NAMESPACE
|
Chris@16
|
282
|
Chris@16
|
283 #endif
|