]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/pending/unicode_iterator.hpp
boost: update to 1.47.0
[lyx.git] / boost / boost / regex / pending / unicode_iterator.hpp
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 & 0xFC00u) == 0xd800u;
86 }
87 inline bool is_low_surrogate(::boost::uint16_t v)
88 {
89    return (v & 0xFC00u) == 0xdc00u;
90 }
91 template <class T>
92 inline bool is_surrogate(T v)
93 {
94    return (v & 0xF800u) == 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 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) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
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) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
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 private:
307    static void invalid_code_point(::boost::uint16_t val)
308    {
309 #ifndef BOOST_NO_STD_LOCALE
310       std::stringstream ss;
311       ss << "Misplaced UTF-16 surrogate U+" << std::showbase << std::hex << val << " encountered while trying to encode UTF-32 sequence";
312       std::out_of_range e(ss.str());
313 #else
314       std::out_of_range e("Misplaced UTF-16 surrogate encountered while trying to encode UTF-32 sequence");
315 #endif
316       boost::throw_exception(e);
317    }
318    void extract_current()const
319    {
320       m_value = static_cast<U32Type>(static_cast< ::boost::uint16_t>(*m_position));
321       // if the last value is a high surrogate then adjust m_position and m_value as needed:
322       if(detail::is_high_surrogate(*m_position))
323       {
324          // precondition; next value must have be a low-surrogate:
325          BaseIterator next(m_position);
326          ::boost::uint16_t t = *++next;
327          if((t & 0xFC00u) != 0xDC00u)
328             invalid_code_point(t);
329          m_value = (m_value - detail::high_surrogate_base) << 10;
330          m_value |= (static_cast<U32Type>(static_cast< ::boost::uint16_t>(t)) & detail::ten_bit_mask);
331       }
332       // postcondition; result must not be a surrogate:
333       if(detail::is_surrogate(m_value))
334          invalid_code_point(static_cast< ::boost::uint16_t>(m_value));
335    }
336    BaseIterator m_position;
337    mutable U32Type m_value;
338 };
339
340 template <class BaseIterator, class U8Type = ::boost::uint8_t>
341 class u32_to_u8_iterator
342    : public boost::iterator_facade<u32_to_u8_iterator<BaseIterator, U8Type>, U8Type, std::bidirectional_iterator_tag, const U8Type>
343 {
344    typedef boost::iterator_facade<u32_to_u8_iterator<BaseIterator, U8Type>, U8Type, std::bidirectional_iterator_tag, const U8Type> base_type;
345    
346 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
347    typedef typename std::iterator_traits<BaseIterator>::value_type base_value_type;
348
349    BOOST_STATIC_ASSERT(sizeof(base_value_type)*CHAR_BIT == 32);
350    BOOST_STATIC_ASSERT(sizeof(U8Type)*CHAR_BIT == 8);
351 #endif
352
353 public:
354    typename base_type::reference
355       dereference()const
356    {
357       if(m_current == 4)
358          extract_current();
359       return m_values[m_current];
360    }
361    bool equal(const u32_to_u8_iterator& that)const
362    {
363       if(m_position == that.m_position)
364       {
365          // either the m_current's must be equal, or one must be 0 and 
366          // the other 4: which means neither must have bits 1 or 2 set:
367          return (m_current == that.m_current)
368             || (((m_current | that.m_current) & 3) == 0);
369       }
370       return false;
371    }
372    void increment()
373    {
374       // if we have a pending read then read now, so that we know whether
375       // to skip a position, or move to a low-surrogate:
376       if(m_current == 4)
377       {
378          // pending read:
379          extract_current();
380       }
381       // move to the next surrogate position:
382       ++m_current;
383       // if we've reached the end skip a position:
384       if(m_values[m_current] == 0)
385       {
386          m_current = 4;
387          ++m_position;
388       }
389    }
390    void decrement()
391    {
392       if((m_current & 3) == 0)
393       {
394          --m_position;
395          extract_current();
396          m_current = 3;
397          while(m_current && (m_values[m_current] == 0))
398             --m_current;
399       }
400       else
401          --m_current;
402    }
403    BaseIterator base()const
404    {
405       return m_position;
406    }
407    // construct:
408    u32_to_u8_iterator() : m_position(), m_current(0)
409    {
410       m_values[0] = 0;
411       m_values[1] = 0;
412       m_values[2] = 0;
413       m_values[3] = 0;
414       m_values[4] = 0;
415    }
416    u32_to_u8_iterator(BaseIterator b) : m_position(b), m_current(4)
417    {
418       m_values[0] = 0;
419       m_values[1] = 0;
420       m_values[2] = 0;
421       m_values[3] = 0;
422       m_values[4] = 0;
423    }
424 private:
425
426    void extract_current()const
427    {
428       boost::uint32_t c = *m_position;
429       if(c > 0x10FFFFu)
430          detail::invalid_utf32_code_point(c);
431       if(c < 0x80u)
432       {
433          m_values[0] = static_cast<unsigned char>(c);
434          m_values[1] = static_cast<unsigned char>(0u);
435          m_values[2] = static_cast<unsigned char>(0u);
436          m_values[3] = static_cast<unsigned char>(0u);
437       }
438       else if(c < 0x800u)
439       {
440          m_values[0] = static_cast<unsigned char>(0xC0u + (c >> 6));
441          m_values[1] = static_cast<unsigned char>(0x80u + (c & 0x3Fu));
442          m_values[2] = static_cast<unsigned char>(0u);
443          m_values[3] = static_cast<unsigned char>(0u);
444       }
445       else if(c < 0x10000u)
446       {
447          m_values[0] = static_cast<unsigned char>(0xE0u + (c >> 12));
448          m_values[1] = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));
449          m_values[2] = static_cast<unsigned char>(0x80u + (c & 0x3Fu));
450          m_values[3] = static_cast<unsigned char>(0u);
451       }
452       else
453       {
454          m_values[0] = static_cast<unsigned char>(0xF0u + (c >> 18));
455          m_values[1] = static_cast<unsigned char>(0x80u + ((c >> 12) & 0x3Fu));
456          m_values[2] = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));
457          m_values[3] = static_cast<unsigned char>(0x80u + (c & 0x3Fu));
458       }
459       m_current= 0;
460    }
461    BaseIterator m_position;
462    mutable U8Type m_values[5];
463    mutable unsigned m_current;
464 };
465
466 template <class BaseIterator, class U32Type = ::boost::uint32_t>
467 class u8_to_u32_iterator
468    : public boost::iterator_facade<u8_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type>
469 {
470    typedef boost::iterator_facade<u8_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type> base_type;
471    // special values for pending iterator reads:
472    BOOST_STATIC_CONSTANT(U32Type, pending_read = 0xffffffffu);
473
474 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
475    typedef typename std::iterator_traits<BaseIterator>::value_type base_value_type;
476
477    BOOST_STATIC_ASSERT(sizeof(base_value_type)*CHAR_BIT == 8);
478    BOOST_STATIC_ASSERT(sizeof(U32Type)*CHAR_BIT == 32);
479 #endif
480
481 public:
482    typename base_type::reference
483       dereference()const
484    {
485       if(m_value == pending_read)
486          extract_current();
487       return m_value;
488    }
489    bool equal(const u8_to_u32_iterator& that)const
490    {
491       return m_position == that.m_position;
492    }
493    void increment()
494    {
495       // skip high surrogate first if there is one:
496       unsigned c = detail::utf8_byte_count(*m_position);
497       std::advance(m_position, c);
498       m_value = pending_read;
499    }
500    void decrement()
501    {
502       // Keep backtracking until we don't have a trailing character:
503       unsigned count = 0;
504       while((*--m_position & 0xC0u) == 0x80u) ++count;
505       // now check that the sequence was valid:
506       if(count != detail::utf8_trailing_byte_count(*m_position))
507          invalid_sequnce();
508       m_value = pending_read;
509    }
510    BaseIterator base()const
511    {
512       return m_position;
513    }
514    // construct:
515    u8_to_u32_iterator() : m_position()
516    {
517       m_value = pending_read;
518    }
519    u8_to_u32_iterator(BaseIterator b) : m_position(b)
520    {
521       m_value = pending_read;
522    }
523 private:
524    static void invalid_sequnce()
525    {
526       std::out_of_range e("Invalid UTF-8 sequence encountered while trying to encode UTF-32 character");
527       boost::throw_exception(e);
528    }
529    void extract_current()const
530    {
531       m_value = static_cast<U32Type>(static_cast< ::boost::uint8_t>(*m_position));
532       // we must not have a continuation character:
533       if((m_value & 0xC0u) == 0x80u)
534          invalid_sequnce();
535       // see how many extra byts we have:
536       unsigned extra = detail::utf8_trailing_byte_count(*m_position);
537       // extract the extra bits, 6 from each extra byte:
538       BaseIterator next(m_position);
539       for(unsigned c = 0; c < extra; ++c)
540       {
541          ++next;
542          m_value <<= 6;
543          m_value += static_cast<boost::uint8_t>(*next) & 0x3Fu;
544       }
545       // we now need to remove a few of the leftmost bits, but how many depends
546       // upon how many extra bytes we've extracted:
547       static const boost::uint32_t masks[4] = 
548       {
549          0x7Fu,
550          0x7FFu,
551          0xFFFFu,
552          0x1FFFFFu,
553       };
554       m_value &= masks[extra];
555       // check the result:
556       if(m_value > static_cast<U32Type>(0x10FFFFu))
557          invalid_sequnce();
558    }
559    BaseIterator m_position;
560    mutable U32Type m_value;
561 };
562
563 template <class BaseIterator>
564 class utf16_output_iterator
565 {
566 public:
567    typedef void                                   difference_type;
568    typedef void                                   value_type;
569    typedef boost::uint32_t*                       pointer;
570    typedef boost::uint32_t&                       reference;
571    typedef std::output_iterator_tag               iterator_category;
572
573    utf16_output_iterator(const BaseIterator& b)
574       : m_position(b){}
575    utf16_output_iterator(const utf16_output_iterator& that)
576       : m_position(that.m_position){}
577    utf16_output_iterator& operator=(const utf16_output_iterator& that)
578    {
579       m_position = that.m_position;
580       return *this;
581    }
582    const utf16_output_iterator& operator*()const
583    {
584       return *this;
585    }
586    void operator=(boost::uint32_t val)const
587    {
588       push(val);
589    }
590    utf16_output_iterator& operator++()
591    {
592       return *this;
593    }
594    utf16_output_iterator& operator++(int)
595    {
596       return *this;
597    }
598    BaseIterator base()const
599    {
600       return m_position;
601    }
602 private:
603    void push(boost::uint32_t v)const
604    {
605       if(v >= 0x10000u)
606       {
607          // begin by checking for a code point out of range:
608          if(v > 0x10FFFFu)
609             detail::invalid_utf32_code_point(v);
610          // split into two surrogates:
611          *m_position++ = static_cast<boost::uint16_t>(v >> 10) + detail::high_surrogate_base;
612          *m_position++ = static_cast<boost::uint16_t>(v & detail::ten_bit_mask) + detail::low_surrogate_base;
613       }
614       else
615       {
616          // 16-bit code point:
617          // value must not be a surrogate:
618          if(detail::is_surrogate(v))
619             detail::invalid_utf32_code_point(v);
620          *m_position++ = static_cast<boost::uint16_t>(v);
621       }
622    }
623    mutable BaseIterator m_position;
624 };
625
626 template <class BaseIterator>
627 class utf8_output_iterator
628 {
629 public:
630    typedef void                                   difference_type;
631    typedef void                                   value_type;
632    typedef boost::uint32_t*                       pointer;
633    typedef boost::uint32_t&                       reference;
634    typedef std::output_iterator_tag               iterator_category;
635
636    utf8_output_iterator(const BaseIterator& b)
637       : m_position(b){}
638    utf8_output_iterator(const utf8_output_iterator& that)
639       : m_position(that.m_position){}
640    utf8_output_iterator& operator=(const utf8_output_iterator& that)
641    {
642       m_position = that.m_position;
643       return *this;
644    }
645    const utf8_output_iterator& operator*()const
646    {
647       return *this;
648    }
649    void operator=(boost::uint32_t val)const
650    {
651       push(val);
652    }
653    utf8_output_iterator& operator++()
654    {
655       return *this;
656    }
657    utf8_output_iterator& operator++(int)
658    {
659       return *this;
660    }
661    BaseIterator base()const
662    {
663       return m_position;
664    }
665 private:
666    void push(boost::uint32_t c)const
667    {
668       if(c > 0x10FFFFu)
669          detail::invalid_utf32_code_point(c);
670       if(c < 0x80u)
671       {
672          *m_position++ = static_cast<unsigned char>(c);
673       }
674       else if(c < 0x800u)
675       {
676          *m_position++ = static_cast<unsigned char>(0xC0u + (c >> 6));
677          *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu));
678       }
679       else if(c < 0x10000u)
680       {
681          *m_position++ = static_cast<unsigned char>(0xE0u + (c >> 12));
682          *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));
683          *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu));
684       }
685       else
686       {
687          *m_position++ = static_cast<unsigned char>(0xF0u + (c >> 18));
688          *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 12) & 0x3Fu));
689          *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));
690          *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu));
691       }
692    }
693    mutable BaseIterator m_position;
694 };
695
696 } // namespace boost
697
698 #endif // BOOST_REGEX_UNICODE_ITERATOR_HPP
699