]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * | |
3 | * Copyright (c) 2004 | |
4 | * John Maddock | |
5 | * | |
6 | * Use, modification and distribution are subject to the | |
7 | * Boost Software License, Version 1.0. (See accompanying file | |
8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | * | |
10 | */ | |
11 | ||
12 | /* | |
13 | * LOCATION: see http://www.boost.org for most recent version. | |
14 | * FILE unicode_iterator.hpp | |
15 | * VERSION see <boost/version.hpp> | |
16 | * DESCRIPTION: Iterator adapters for converting between different Unicode encodings. | |
17 | */ | |
18 | ||
19 | /**************************************************************************** | |
20 | ||
21 | Contents: | |
22 | ~~~~~~~~~ | |
23 | ||
24 | 1) Read Only, Input Adapters: | |
25 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
26 | ||
27 | template <class BaseIterator, class U8Type = ::boost::uint8_t> | |
28 | class u32_to_u8_iterator; | |
29 | ||
30 | Adapts sequence of UTF-32 code points to "look like" a sequence of UTF-8. | |
31 | ||
32 | template <class BaseIterator, class U32Type = ::boost::uint32_t> | |
33 | class u8_to_u32_iterator; | |
34 | ||
35 | Adapts sequence of UTF-8 code points to "look like" a sequence of UTF-32. | |
36 | ||
37 | template <class BaseIterator, class U16Type = ::boost::uint16_t> | |
38 | class u32_to_u16_iterator; | |
39 | ||
40 | Adapts sequence of UTF-32 code points to "look like" a sequence of UTF-16. | |
41 | ||
42 | template <class BaseIterator, class U32Type = ::boost::uint32_t> | |
43 | class u16_to_u32_iterator; | |
44 | ||
45 | Adapts sequence of UTF-16 code points to "look like" a sequence of UTF-32. | |
46 | ||
47 | 2) Single pass output iterator adapters: | |
48 | ||
49 | template <class BaseIterator> | |
50 | class utf8_output_iterator; | |
51 | ||
52 | Accepts UTF-32 code points and forwards them on as UTF-8 code points. | |
53 | ||
54 | template <class BaseIterator> | |
55 | class utf16_output_iterator; | |
56 | ||
57 | Accepts UTF-32 code points and forwards them on as UTF-16 code points. | |
58 | ||
59 | ****************************************************************************/ | |
60 | ||
61 | #ifndef BOOST_REGEX_UNICODE_ITERATOR_HPP | |
62 | #define BOOST_REGEX_UNICODE_ITERATOR_HPP | |
63 | #include <boost/cstdint.hpp> | |
64 | #include <boost/assert.hpp> | |
65 | #include <boost/iterator/iterator_facade.hpp> | |
66 | #include <boost/static_assert.hpp> | |
67 | #include <boost/throw_exception.hpp> | |
68 | #include <stdexcept> | |
69 | #ifndef BOOST_NO_STD_LOCALE | |
70 | #include <sstream> | |
71 | #include <ios> | |
72 | #endif | |
73 | #include <limits.h> // CHAR_BIT | |
74 | ||
75 | namespace boost{ | |
76 | ||
77 | namespace detail{ | |
78 | ||
79 | static const ::boost::uint16_t high_surrogate_base = 0xD7C0u; | |
80 | static const ::boost::uint16_t low_surrogate_base = 0xDC00u; | |
81 | static const ::boost::uint32_t ten_bit_mask = 0x3FFu; | |
82 | ||
83 | inline bool is_high_surrogate(::boost::uint16_t v) | |
84 | { | |
85 | return (v & 0xFFFFFC00u) == 0xd800u; | |
86 | } | |
87 | inline bool is_low_surrogate(::boost::uint16_t v) | |
88 | { | |
89 | return (v & 0xFFFFFC00u) == 0xdc00u; | |
90 | } | |
91 | template <class T> | |
92 | inline bool is_surrogate(T v) | |
93 | { | |
94 | return (v & 0xFFFFF800u) == 0xd800; | |
95 | } | |
96 | ||
97 | inline unsigned utf8_byte_count(boost::uint8_t c) | |
98 | { | |
99 | // if the most significant bit with a zero in it is in position | |
100 | // 8-N then there are N bytes in this UTF-8 sequence: | |
101 | boost::uint8_t mask = 0x80u; | |
102 | unsigned result = 0; | |
103 | while(c & mask) | |
104 | { | |
105 | ++result; | |
106 | mask >>= 1; | |
107 | } | |
108 | return (result == 0) ? 1 : ((result > 4) ? 4 : result); | |
109 | } | |
110 | ||
111 | inline unsigned utf8_trailing_byte_count(boost::uint8_t c) | |
112 | { | |
113 | return utf8_byte_count(c) - 1; | |
114 | } | |
115 | ||
116 | #ifdef BOOST_MSVC | |
117 | #pragma warning(push) | |
118 | #pragma warning(disable:4100) | |
119 | #endif | |
120 | BOOST_NORETURN inline void invalid_utf32_code_point(::boost::uint32_t val) | |
121 | { | |
122 | #ifndef BOOST_NO_STD_LOCALE | |
123 | std::stringstream ss; | |
124 | ss << "Invalid UTF-32 code point U+" << std::showbase << std::hex << val << " encountered while trying to encode UTF-16 sequence"; | |
125 | std::out_of_range e(ss.str()); | |
126 | #else | |
127 | std::out_of_range e("Invalid UTF-32 code point encountered while trying to encode UTF-16 sequence"); | |
128 | #endif | |
129 | boost::throw_exception(e); | |
130 | } | |
131 | #ifdef BOOST_MSVC | |
132 | #pragma warning(pop) | |
133 | #endif | |
134 | ||
135 | ||
136 | } // namespace detail | |
137 | ||
138 | template <class BaseIterator, class U16Type = ::boost::uint16_t> | |
139 | class u32_to_u16_iterator | |
140 | : public boost::iterator_facade<u32_to_u16_iterator<BaseIterator, U16Type>, U16Type, std::bidirectional_iterator_tag, const U16Type> | |
141 | { | |
142 | typedef boost::iterator_facade<u32_to_u16_iterator<BaseIterator, U16Type>, U16Type, std::bidirectional_iterator_tag, const U16Type> base_type; | |
143 | ||
144 | #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) | |
145 | typedef typename std::iterator_traits<BaseIterator>::value_type base_value_type; | |
146 | ||
147 | BOOST_STATIC_ASSERT(sizeof(base_value_type)*CHAR_BIT == 32); | |
148 | BOOST_STATIC_ASSERT(sizeof(U16Type)*CHAR_BIT == 16); | |
149 | #endif | |
150 | ||
151 | public: | |
152 | typename base_type::reference | |
153 | dereference()const | |
154 | { | |
155 | if(m_current == 2) | |
156 | extract_current(); | |
157 | return m_values[m_current]; | |
158 | } | |
159 | bool equal(const u32_to_u16_iterator& that)const | |
160 | { | |
161 | if(m_position == that.m_position) | |
162 | { | |
163 | // Both m_currents must be equal, or both even | |
164 | // this is the same as saying their sum must be even: | |
165 | return (m_current + that.m_current) & 1u ? false : true; | |
166 | } | |
167 | return false; | |
168 | } | |
169 | void increment() | |
170 | { | |
171 | // if we have a pending read then read now, so that we know whether | |
172 | // to skip a position, or move to a low-surrogate: | |
173 | if(m_current == 2) | |
174 | { | |
175 | // pending read: | |
176 | extract_current(); | |
177 | } | |
178 | // move to the next surrogate position: | |
179 | ++m_current; | |
180 | // if we've reached the end skip a position: | |
181 | if(m_values[m_current] == 0) | |
182 | { | |
183 | m_current = 2; | |
184 | ++m_position; | |
185 | } | |
186 | } | |
187 | void decrement() | |
188 | { | |
189 | if(m_current != 1) | |
190 | { | |
191 | // decrementing an iterator always leads to a valid position: | |
192 | --m_position; | |
193 | extract_current(); | |
194 | m_current = m_values[1] ? 1 : 0; | |
195 | } | |
196 | else | |
197 | { | |
198 | m_current = 0; | |
199 | } | |
200 | } | |
201 | BaseIterator base()const | |
202 | { | |
203 | return m_position; | |
204 | } | |
205 | // construct: | |
206 | u32_to_u16_iterator() : m_position(), m_current(0) | |
207 | { | |
208 | m_values[0] = 0; | |
209 | m_values[1] = 0; | |
210 | m_values[2] = 0; | |
211 | } | |
212 | u32_to_u16_iterator(BaseIterator b) : m_position(b), m_current(2) | |
213 | { | |
214 | m_values[0] = 0; | |
215 | m_values[1] = 0; | |
216 | m_values[2] = 0; | |
217 | } | |
218 | private: | |
219 | ||
220 | void extract_current()const | |
221 | { | |
222 | // begin by checking for a code point out of range: | |
223 | ::boost::uint32_t v = *m_position; | |
224 | if(v >= 0x10000u) | |
225 | { | |
226 | if(v > 0x10FFFFu) | |
227 | detail::invalid_utf32_code_point(*m_position); | |
228 | // split into two surrogates: | |
229 | m_values[0] = static_cast<U16Type>(v >> 10) + detail::high_surrogate_base; | |
230 | m_values[1] = static_cast<U16Type>(v & detail::ten_bit_mask) + detail::low_surrogate_base; | |
231 | m_current = 0; | |
232 | BOOST_ASSERT(detail::is_high_surrogate(m_values[0])); | |
233 | BOOST_ASSERT(detail::is_low_surrogate(m_values[1])); | |
234 | } | |
235 | else | |
236 | { | |
237 | // 16-bit code point: | |
238 | m_values[0] = static_cast<U16Type>(*m_position); | |
239 | m_values[1] = 0; | |
240 | m_current = 0; | |
241 | // value must not be a surrogate: | |
242 | if(detail::is_surrogate(m_values[0])) | |
243 | detail::invalid_utf32_code_point(*m_position); | |
244 | } | |
245 | } | |
246 | BaseIterator m_position; | |
247 | mutable U16Type m_values[3]; | |
248 | mutable unsigned m_current; | |
249 | }; | |
250 | ||
251 | template <class BaseIterator, class U32Type = ::boost::uint32_t> | |
252 | class u16_to_u32_iterator | |
253 | : public boost::iterator_facade<u16_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type> | |
254 | { | |
255 | typedef boost::iterator_facade<u16_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type> base_type; | |
256 | // special values for pending iterator reads: | |
257 | BOOST_STATIC_CONSTANT(U32Type, pending_read = 0xffffffffu); | |
258 | ||
259 | #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) | |
260 | typedef typename std::iterator_traits<BaseIterator>::value_type base_value_type; | |
261 | ||
262 | BOOST_STATIC_ASSERT(sizeof(base_value_type)*CHAR_BIT == 16); | |
263 | BOOST_STATIC_ASSERT(sizeof(U32Type)*CHAR_BIT == 32); | |
264 | #endif | |
265 | ||
266 | public: | |
267 | typename base_type::reference | |
268 | dereference()const | |
269 | { | |
270 | if(m_value == pending_read) | |
271 | extract_current(); | |
272 | return m_value; | |
273 | } | |
274 | bool equal(const u16_to_u32_iterator& that)const | |
275 | { | |
276 | return m_position == that.m_position; | |
277 | } | |
278 | void increment() | |
279 | { | |
280 | // skip high surrogate first if there is one: | |
281 | if(detail::is_high_surrogate(*m_position)) ++m_position; | |
282 | ++m_position; | |
283 | m_value = pending_read; | |
284 | } | |
285 | void decrement() | |
286 | { | |
287 | --m_position; | |
288 | // if we have a low surrogate then go back one more: | |
289 | if(detail::is_low_surrogate(*m_position)) | |
290 | --m_position; | |
291 | m_value = pending_read; | |
292 | } | |
293 | BaseIterator base()const | |
294 | { | |
295 | return m_position; | |
296 | } | |
297 | // construct: | |
298 | u16_to_u32_iterator() : m_position() | |
299 | { | |
300 | m_value = pending_read; | |
301 | } | |
302 | u16_to_u32_iterator(BaseIterator b) : m_position(b) | |
303 | { | |
304 | m_value = pending_read; | |
305 | } | |
306 | // | |
307 | // Range checked version: | |
308 | // | |
309 | u16_to_u32_iterator(BaseIterator b, BaseIterator start, BaseIterator end) : m_position(b) | |
310 | { | |
311 | m_value = pending_read; | |
312 | // | |
313 | // The range must not start with a low surrogate, or end in a high surrogate, | |
314 | // otherwise we run the risk of running outside the underlying input range. | |
315 | // Likewise b must not be located at a low surrogate. | |
316 | // | |
317 | boost::uint16_t val; | |
318 | if(start != end) | |
319 | { | |
320 | if((b != start) && (b != end)) | |
321 | { | |
322 | val = *b; | |
323 | if(detail::is_surrogate(val) && ((val & 0xFC00u) == 0xDC00u)) | |
324 | invalid_code_point(val); | |
325 | } | |
326 | val = *start; | |
327 | if(detail::is_surrogate(val) && ((val & 0xFC00u) == 0xDC00u)) | |
328 | invalid_code_point(val); | |
329 | val = *--end; | |
330 | if(detail::is_high_surrogate(val)) | |
331 | invalid_code_point(val); | |
332 | } | |
333 | } | |
334 | private: | |
335 | static void invalid_code_point(::boost::uint16_t val) | |
336 | { | |
337 | #ifndef BOOST_NO_STD_LOCALE | |
338 | std::stringstream ss; | |
339 | ss << "Misplaced UTF-16 surrogate U+" << std::showbase << std::hex << val << " encountered while trying to encode UTF-32 sequence"; | |
340 | std::out_of_range e(ss.str()); | |
341 | #else | |
342 | std::out_of_range e("Misplaced UTF-16 surrogate encountered while trying to encode UTF-32 sequence"); | |
343 | #endif | |
344 | boost::throw_exception(e); | |
345 | } | |
346 | void extract_current()const | |
347 | { | |
348 | m_value = static_cast<U32Type>(static_cast< ::boost::uint16_t>(*m_position)); | |
349 | // if the last value is a high surrogate then adjust m_position and m_value as needed: | |
350 | if(detail::is_high_surrogate(*m_position)) | |
351 | { | |
352 | // precondition; next value must have be a low-surrogate: | |
353 | BaseIterator next(m_position); | |
354 | ::boost::uint16_t t = *++next; | |
355 | if((t & 0xFC00u) != 0xDC00u) | |
356 | invalid_code_point(t); | |
357 | m_value = (m_value - detail::high_surrogate_base) << 10; | |
358 | m_value |= (static_cast<U32Type>(static_cast< ::boost::uint16_t>(t)) & detail::ten_bit_mask); | |
359 | } | |
360 | // postcondition; result must not be a surrogate: | |
361 | if(detail::is_surrogate(m_value)) | |
362 | invalid_code_point(static_cast< ::boost::uint16_t>(m_value)); | |
363 | } | |
364 | BaseIterator m_position; | |
365 | mutable U32Type m_value; | |
366 | }; | |
367 | ||
368 | template <class BaseIterator, class U8Type = ::boost::uint8_t> | |
369 | class u32_to_u8_iterator | |
370 | : public boost::iterator_facade<u32_to_u8_iterator<BaseIterator, U8Type>, U8Type, std::bidirectional_iterator_tag, const U8Type> | |
371 | { | |
372 | typedef boost::iterator_facade<u32_to_u8_iterator<BaseIterator, U8Type>, U8Type, std::bidirectional_iterator_tag, const U8Type> base_type; | |
373 | ||
374 | #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) | |
375 | typedef typename std::iterator_traits<BaseIterator>::value_type base_value_type; | |
376 | ||
377 | BOOST_STATIC_ASSERT(sizeof(base_value_type)*CHAR_BIT == 32); | |
378 | BOOST_STATIC_ASSERT(sizeof(U8Type)*CHAR_BIT == 8); | |
379 | #endif | |
380 | ||
381 | public: | |
382 | typename base_type::reference | |
383 | dereference()const | |
384 | { | |
385 | if(m_current == 4) | |
386 | extract_current(); | |
387 | return m_values[m_current]; | |
388 | } | |
389 | bool equal(const u32_to_u8_iterator& that)const | |
390 | { | |
391 | if(m_position == that.m_position) | |
392 | { | |
393 | // either the m_current's must be equal, or one must be 0 and | |
394 | // the other 4: which means neither must have bits 1 or 2 set: | |
395 | return (m_current == that.m_current) | |
396 | || (((m_current | that.m_current) & 3) == 0); | |
397 | } | |
398 | return false; | |
399 | } | |
400 | void increment() | |
401 | { | |
402 | // if we have a pending read then read now, so that we know whether | |
403 | // to skip a position, or move to a low-surrogate: | |
404 | if(m_current == 4) | |
405 | { | |
406 | // pending read: | |
407 | extract_current(); | |
408 | } | |
409 | // move to the next surrogate position: | |
410 | ++m_current; | |
411 | // if we've reached the end skip a position: | |
412 | if(m_values[m_current] == 0) | |
413 | { | |
414 | m_current = 4; | |
415 | ++m_position; | |
416 | } | |
417 | } | |
418 | void decrement() | |
419 | { | |
420 | if((m_current & 3) == 0) | |
421 | { | |
422 | --m_position; | |
423 | extract_current(); | |
424 | m_current = 3; | |
425 | while(m_current && (m_values[m_current] == 0)) | |
426 | --m_current; | |
427 | } | |
428 | else | |
429 | --m_current; | |
430 | } | |
431 | BaseIterator base()const | |
432 | { | |
433 | return m_position; | |
434 | } | |
435 | // construct: | |
436 | u32_to_u8_iterator() : m_position(), m_current(0) | |
437 | { | |
438 | m_values[0] = 0; | |
439 | m_values[1] = 0; | |
440 | m_values[2] = 0; | |
441 | m_values[3] = 0; | |
442 | m_values[4] = 0; | |
443 | } | |
444 | u32_to_u8_iterator(BaseIterator b) : m_position(b), m_current(4) | |
445 | { | |
446 | m_values[0] = 0; | |
447 | m_values[1] = 0; | |
448 | m_values[2] = 0; | |
449 | m_values[3] = 0; | |
450 | m_values[4] = 0; | |
451 | } | |
452 | private: | |
453 | ||
454 | void extract_current()const | |
455 | { | |
456 | boost::uint32_t c = *m_position; | |
457 | if(c > 0x10FFFFu) | |
458 | detail::invalid_utf32_code_point(c); | |
459 | if(c < 0x80u) | |
460 | { | |
461 | m_values[0] = static_cast<unsigned char>(c); | |
462 | m_values[1] = static_cast<unsigned char>(0u); | |
463 | m_values[2] = static_cast<unsigned char>(0u); | |
464 | m_values[3] = static_cast<unsigned char>(0u); | |
465 | } | |
466 | else if(c < 0x800u) | |
467 | { | |
468 | m_values[0] = static_cast<unsigned char>(0xC0u + (c >> 6)); | |
469 | m_values[1] = static_cast<unsigned char>(0x80u + (c & 0x3Fu)); | |
470 | m_values[2] = static_cast<unsigned char>(0u); | |
471 | m_values[3] = static_cast<unsigned char>(0u); | |
472 | } | |
473 | else if(c < 0x10000u) | |
474 | { | |
475 | m_values[0] = static_cast<unsigned char>(0xE0u + (c >> 12)); | |
476 | m_values[1] = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu)); | |
477 | m_values[2] = static_cast<unsigned char>(0x80u + (c & 0x3Fu)); | |
478 | m_values[3] = static_cast<unsigned char>(0u); | |
479 | } | |
480 | else | |
481 | { | |
482 | m_values[0] = static_cast<unsigned char>(0xF0u + (c >> 18)); | |
483 | m_values[1] = static_cast<unsigned char>(0x80u + ((c >> 12) & 0x3Fu)); | |
484 | m_values[2] = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu)); | |
485 | m_values[3] = static_cast<unsigned char>(0x80u + (c & 0x3Fu)); | |
486 | } | |
487 | m_current= 0; | |
488 | } | |
489 | BaseIterator m_position; | |
490 | mutable U8Type m_values[5]; | |
491 | mutable unsigned m_current; | |
492 | }; | |
493 | ||
494 | template <class BaseIterator, class U32Type = ::boost::uint32_t> | |
495 | class u8_to_u32_iterator | |
496 | : public boost::iterator_facade<u8_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type> | |
497 | { | |
498 | typedef boost::iterator_facade<u8_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type> base_type; | |
499 | // special values for pending iterator reads: | |
500 | BOOST_STATIC_CONSTANT(U32Type, pending_read = 0xffffffffu); | |
501 | ||
502 | #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) | |
503 | typedef typename std::iterator_traits<BaseIterator>::value_type base_value_type; | |
504 | ||
505 | BOOST_STATIC_ASSERT(sizeof(base_value_type)*CHAR_BIT == 8); | |
506 | BOOST_STATIC_ASSERT(sizeof(U32Type)*CHAR_BIT == 32); | |
507 | #endif | |
508 | ||
509 | public: | |
510 | typename base_type::reference | |
511 | dereference()const | |
512 | { | |
513 | if(m_value == pending_read) | |
514 | extract_current(); | |
515 | return m_value; | |
516 | } | |
517 | bool equal(const u8_to_u32_iterator& that)const | |
518 | { | |
519 | return m_position == that.m_position; | |
520 | } | |
521 | void increment() | |
522 | { | |
523 | // We must not start with a continuation character: | |
524 | if((static_cast<boost::uint8_t>(*m_position) & 0xC0) == 0x80) | |
525 | invalid_sequence(); | |
526 | // skip high surrogate first if there is one: | |
527 | unsigned c = detail::utf8_byte_count(*m_position); | |
528 | if(m_value == pending_read) | |
529 | { | |
530 | // Since we haven't read in a value, we need to validate the code points: | |
531 | for(unsigned i = 0; i < c; ++i) | |
532 | { | |
533 | ++m_position; | |
534 | // We must have a continuation byte: | |
535 | if((i != c - 1) && ((static_cast<boost::uint8_t>(*m_position) & 0xC0) != 0x80)) | |
536 | invalid_sequence(); | |
537 | } | |
538 | } | |
539 | else | |
540 | { | |
541 | std::advance(m_position, c); | |
542 | } | |
543 | m_value = pending_read; | |
544 | } | |
545 | void decrement() | |
546 | { | |
547 | // Keep backtracking until we don't have a trailing character: | |
548 | unsigned count = 0; | |
549 | while((*--m_position & 0xC0u) == 0x80u) ++count; | |
550 | // now check that the sequence was valid: | |
551 | if(count != detail::utf8_trailing_byte_count(*m_position)) | |
552 | invalid_sequence(); | |
553 | m_value = pending_read; | |
554 | } | |
555 | BaseIterator base()const | |
556 | { | |
557 | return m_position; | |
558 | } | |
559 | // construct: | |
560 | u8_to_u32_iterator() : m_position() | |
561 | { | |
562 | m_value = pending_read; | |
563 | } | |
564 | u8_to_u32_iterator(BaseIterator b) : m_position(b) | |
565 | { | |
566 | m_value = pending_read; | |
567 | } | |
568 | // | |
569 | // Checked constructor: | |
570 | // | |
571 | u8_to_u32_iterator(BaseIterator b, BaseIterator start, BaseIterator end) : m_position(b) | |
572 | { | |
573 | m_value = pending_read; | |
574 | // | |
575 | // We must not start with a continuation character, or end with a | |
576 | // truncated UTF-8 sequence otherwise we run the risk of going past | |
577 | // the start/end of the underlying sequence: | |
578 | // | |
579 | if(start != end) | |
580 | { | |
581 | unsigned char v = *start; | |
582 | if((v & 0xC0u) == 0x80u) | |
583 | invalid_sequence(); | |
584 | if((b != start) && (b != end) && ((*b & 0xC0u) == 0x80u)) | |
585 | invalid_sequence(); | |
586 | BaseIterator pos = end; | |
587 | do | |
588 | { | |
589 | v = *--pos; | |
590 | } | |
591 | while((start != pos) && ((v & 0xC0u) == 0x80u)); | |
592 | std::ptrdiff_t extra = detail::utf8_byte_count(v); | |
593 | if(std::distance(pos, end) < extra) | |
594 | invalid_sequence(); | |
595 | } | |
596 | } | |
597 | private: | |
598 | static void invalid_sequence() | |
599 | { | |
600 | std::out_of_range e("Invalid UTF-8 sequence encountered while trying to encode UTF-32 character"); | |
601 | boost::throw_exception(e); | |
602 | } | |
603 | void extract_current()const | |
604 | { | |
605 | m_value = static_cast<U32Type>(static_cast< ::boost::uint8_t>(*m_position)); | |
606 | // we must not have a continuation character: | |
607 | if((m_value & 0xC0u) == 0x80u) | |
608 | invalid_sequence(); | |
609 | // see how many extra bytes we have: | |
610 | unsigned extra = detail::utf8_trailing_byte_count(*m_position); | |
611 | // extract the extra bits, 6 from each extra byte: | |
612 | BaseIterator next(m_position); | |
613 | for(unsigned c = 0; c < extra; ++c) | |
614 | { | |
615 | ++next; | |
616 | m_value <<= 6; | |
617 | // We must have a continuation byte: | |
618 | if((static_cast<boost::uint8_t>(*next) & 0xC0) != 0x80) | |
619 | invalid_sequence(); | |
620 | m_value += static_cast<boost::uint8_t>(*next) & 0x3Fu; | |
621 | } | |
622 | // we now need to remove a few of the leftmost bits, but how many depends | |
623 | // upon how many extra bytes we've extracted: | |
624 | static const boost::uint32_t masks[4] = | |
625 | { | |
626 | 0x7Fu, | |
627 | 0x7FFu, | |
628 | 0xFFFFu, | |
629 | 0x1FFFFFu, | |
630 | }; | |
631 | m_value &= masks[extra]; | |
632 | // check the result is in range: | |
633 | if(m_value > static_cast<U32Type>(0x10FFFFu)) | |
634 | invalid_sequence(); | |
635 | // The result must not be a surrogate: | |
636 | if((m_value >= static_cast<U32Type>(0xD800)) && (m_value <= static_cast<U32Type>(0xDFFF))) | |
637 | invalid_sequence(); | |
638 | // We should not have had an invalidly encoded UTF8 sequence: | |
639 | if((extra > 0) && (m_value <= static_cast<U32Type>(masks[extra - 1]))) | |
640 | invalid_sequence(); | |
641 | } | |
642 | BaseIterator m_position; | |
643 | mutable U32Type m_value; | |
644 | }; | |
645 | ||
646 | template <class BaseIterator> | |
647 | class utf16_output_iterator | |
648 | { | |
649 | public: | |
650 | typedef void difference_type; | |
651 | typedef void value_type; | |
652 | typedef boost::uint32_t* pointer; | |
653 | typedef boost::uint32_t& reference; | |
654 | typedef std::output_iterator_tag iterator_category; | |
655 | ||
656 | utf16_output_iterator(const BaseIterator& b) | |
657 | : m_position(b){} | |
658 | utf16_output_iterator(const utf16_output_iterator& that) | |
659 | : m_position(that.m_position){} | |
660 | utf16_output_iterator& operator=(const utf16_output_iterator& that) | |
661 | { | |
662 | m_position = that.m_position; | |
663 | return *this; | |
664 | } | |
665 | const utf16_output_iterator& operator*()const | |
666 | { | |
667 | return *this; | |
668 | } | |
669 | void operator=(boost::uint32_t val)const | |
670 | { | |
671 | push(val); | |
672 | } | |
673 | utf16_output_iterator& operator++() | |
674 | { | |
675 | return *this; | |
676 | } | |
677 | utf16_output_iterator& operator++(int) | |
678 | { | |
679 | return *this; | |
680 | } | |
681 | BaseIterator base()const | |
682 | { | |
683 | return m_position; | |
684 | } | |
685 | private: | |
686 | void push(boost::uint32_t v)const | |
687 | { | |
688 | if(v >= 0x10000u) | |
689 | { | |
690 | // begin by checking for a code point out of range: | |
691 | if(v > 0x10FFFFu) | |
692 | detail::invalid_utf32_code_point(v); | |
693 | // split into two surrogates: | |
694 | *m_position++ = static_cast<boost::uint16_t>(v >> 10) + detail::high_surrogate_base; | |
695 | *m_position++ = static_cast<boost::uint16_t>(v & detail::ten_bit_mask) + detail::low_surrogate_base; | |
696 | } | |
697 | else | |
698 | { | |
699 | // 16-bit code point: | |
700 | // value must not be a surrogate: | |
701 | if(detail::is_surrogate(v)) | |
702 | detail::invalid_utf32_code_point(v); | |
703 | *m_position++ = static_cast<boost::uint16_t>(v); | |
704 | } | |
705 | } | |
706 | mutable BaseIterator m_position; | |
707 | }; | |
708 | ||
709 | template <class BaseIterator> | |
710 | class utf8_output_iterator | |
711 | { | |
712 | public: | |
713 | typedef void difference_type; | |
714 | typedef void value_type; | |
715 | typedef boost::uint32_t* pointer; | |
716 | typedef boost::uint32_t& reference; | |
717 | typedef std::output_iterator_tag iterator_category; | |
718 | ||
719 | utf8_output_iterator(const BaseIterator& b) | |
720 | : m_position(b){} | |
721 | utf8_output_iterator(const utf8_output_iterator& that) | |
722 | : m_position(that.m_position){} | |
723 | utf8_output_iterator& operator=(const utf8_output_iterator& that) | |
724 | { | |
725 | m_position = that.m_position; | |
726 | return *this; | |
727 | } | |
728 | const utf8_output_iterator& operator*()const | |
729 | { | |
730 | return *this; | |
731 | } | |
732 | void operator=(boost::uint32_t val)const | |
733 | { | |
734 | push(val); | |
735 | } | |
736 | utf8_output_iterator& operator++() | |
737 | { | |
738 | return *this; | |
739 | } | |
740 | utf8_output_iterator& operator++(int) | |
741 | { | |
742 | return *this; | |
743 | } | |
744 | BaseIterator base()const | |
745 | { | |
746 | return m_position; | |
747 | } | |
748 | private: | |
749 | void push(boost::uint32_t c)const | |
750 | { | |
751 | if(c > 0x10FFFFu) | |
752 | detail::invalid_utf32_code_point(c); | |
753 | if(c < 0x80u) | |
754 | { | |
755 | *m_position++ = static_cast<unsigned char>(c); | |
756 | } | |
757 | else if(c < 0x800u) | |
758 | { | |
759 | *m_position++ = static_cast<unsigned char>(0xC0u + (c >> 6)); | |
760 | *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu)); | |
761 | } | |
762 | else if(c < 0x10000u) | |
763 | { | |
764 | *m_position++ = static_cast<unsigned char>(0xE0u + (c >> 12)); | |
765 | *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu)); | |
766 | *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu)); | |
767 | } | |
768 | else | |
769 | { | |
770 | *m_position++ = static_cast<unsigned char>(0xF0u + (c >> 18)); | |
771 | *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 12) & 0x3Fu)); | |
772 | *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu)); | |
773 | *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu)); | |
774 | } | |
775 | } | |
776 | mutable BaseIterator m_position; | |
777 | }; | |
778 | ||
779 | } // namespace boost | |
780 | ||
781 | #endif // BOOST_REGEX_UNICODE_ITERATOR_HPP | |
782 |