unicode_iterator.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 693 行 · 第 1/2 页

HPP
693
字号
      dereference()const   {      if(m_current == 4)         extract_current();      return m_values[m_current];   }   bool equal(const u32_to_u8_iterator& that)const   {      if(m_position == that.m_position)      {         // either the m_current's must be equal, or one must be 0 and          // the other 4: which means neither must have bits 1 or 2 set:         return (m_current == that.m_current)            || (((m_current | that.m_current) & 3) == 0);      }      return false;   }   void increment()   {      // if we have a pending read then read now, so that we know whether      // to skip a position, or move to a low-surrogate:      if(m_current == 4)      {         // pending read:         extract_current();      }      // move to the next surrogate position:      ++m_current;      // if we've reached the end skip a position:      if(m_values[m_current] == 0)      {         m_current = 4;         ++m_position;      }   }   void decrement()   {      if((m_current & 3) == 0)      {         --m_position;         extract_current();         m_current = 3;         while(m_current && (m_values[m_current] == 0))            --m_current;      }      else         --m_current;   }   BaseIterator base()const   {      return m_position;   }   // construct:   u32_to_u8_iterator() : m_position(), m_current(0)   {      m_values[0] = 0;      m_values[1] = 0;      m_values[2] = 0;      m_values[3] = 0;      m_values[4] = 0;   }   u32_to_u8_iterator(BaseIterator b) : m_position(b), m_current(4)   {      m_values[0] = 0;      m_values[1] = 0;      m_values[2] = 0;      m_values[3] = 0;      m_values[4] = 0;   }private:   void extract_current()const   {      boost::uint32_t c = *m_position;      if(c > 0x10FFFFu)         detail::invalid_utf32_code_point(c);      if(c < 0x80u)      {         m_values[0] = static_cast<unsigned char>(c);         m_values[1] = static_cast<unsigned char>(0u);         m_values[2] = static_cast<unsigned char>(0u);         m_values[3] = static_cast<unsigned char>(0u);      }      else if(c < 0x800u)      {         m_values[0] = static_cast<unsigned char>(0xC0u + (c >> 6));         m_values[1] = static_cast<unsigned char>(0x80u + (c & 0x3Fu));         m_values[2] = static_cast<unsigned char>(0u);         m_values[3] = static_cast<unsigned char>(0u);      }      else if(c < 0x10000u)      {         m_values[0] = static_cast<unsigned char>(0xE0u + (c >> 12));         m_values[1] = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));         m_values[2] = static_cast<unsigned char>(0x80u + (c & 0x3Fu));         m_values[3] = static_cast<unsigned char>(0u);      }      else      {         m_values[0] = static_cast<unsigned char>(0xF0u + (c >> 18));         m_values[1] = static_cast<unsigned char>(0x80u + ((c >> 12) & 0x3Fu));         m_values[2] = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));         m_values[3] = static_cast<unsigned char>(0x80u + (c & 0x3Fu));      }      m_current= 0;   }   BaseIterator m_position;   mutable U8Type m_values[5];   mutable unsigned m_current;};template <class BaseIterator, class U32Type = ::boost::uint32_t>class u8_to_u32_iterator   : public boost::iterator_facade<u8_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type>{   typedef boost::iterator_facade<u8_to_u32_iterator<BaseIterator, U32Type>, U32Type, std::bidirectional_iterator_tag, const U32Type> base_type;   // special values for pending iterator reads:   BOOST_STATIC_CONSTANT(U32Type, pending_read = 0xffffffffu);#if !defined(BOOST_NO_STD_ITERATOR_TRAITS) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)   typedef typename std::iterator_traits<BaseIterator>::value_type base_value_type;   BOOST_STATIC_ASSERT(sizeof(base_value_type)*CHAR_BIT == 8);   BOOST_STATIC_ASSERT(sizeof(U32Type)*CHAR_BIT == 32);#endifpublic:   typename base_type::reference      dereference()const   {      if(m_value == pending_read)         extract_current();      return m_value;   }   bool equal(const u8_to_u32_iterator& that)const   {      return m_position == that.m_position;   }   void increment()   {      // skip high surrogate first if there is one:      unsigned c = detail::utf8_byte_count(*m_position);      std::advance(m_position, c);      m_value = pending_read;   }   void decrement()   {      // Keep backtracking until we don't have a trailing character:      unsigned count = 0;      while((*--m_position & 0xC0u) == 0x80u) ++count;      // now check that the sequence was valid:      if(count != detail::utf8_trailing_byte_count(*m_position))         invalid_sequnce();      m_value = pending_read;   }   BaseIterator base()const   {      return m_position;   }   // construct:   u8_to_u32_iterator() : m_position()   {      m_value = pending_read;   }   u8_to_u32_iterator(BaseIterator b) : m_position(b)   {      m_value = pending_read;   }private:   static void invalid_sequnce()   {      std::out_of_range e("Invalid UTF-8 sequence encountered while trying to encode UTF-32 character");      boost::throw_exception(e);   }   void extract_current()const   {      m_value = static_cast<U32Type>(static_cast< ::boost::uint8_t>(*m_position));      // we must not have a continuation character:      if((m_value & 0xC0u) == 0x80u)         invalid_sequnce();      // see how many extra byts we have:      unsigned extra = detail::utf8_trailing_byte_count(*m_position);      // extract the extra bits, 6 from each extra byte:      BaseIterator next(m_position);      for(unsigned c = 0; c < extra; ++c)      {         ++next;         m_value <<= 6;         m_value += static_cast<boost::uint8_t>(*next) & 0x3Fu;      }      // we now need to remove a few of the leftmost bits, but how many depends      // upon how many extra bytes we've extracted:      static const boost::uint32_t masks[4] =       {         0x7Fu,         0x7FFu,         0xFFFFu,         0x1FFFFFu,      };      m_value &= masks[extra];      // check the result:      if(m_value > static_cast<U32Type>(0x10FFFFu))         invalid_sequnce();   }   BaseIterator m_position;   mutable U32Type m_value;};template <class BaseIterator>class utf16_output_iterator{public:   typedef void                                   difference_type;   typedef void                                   value_type;   typedef boost::uint32_t*                       pointer;   typedef boost::uint32_t&                       reference;   typedef std::output_iterator_tag               iterator_category;   utf16_output_iterator(const BaseIterator& b)      : m_position(b){}   utf16_output_iterator(const utf16_output_iterator& that)      : m_position(that.m_position){}   utf16_output_iterator& operator=(const utf16_output_iterator& that)   {      m_position = that.m_position;      return *this;   }   const utf16_output_iterator& operator*()const   {      return *this;   }   void operator=(boost::uint32_t val)const   {      push(val);   }   utf16_output_iterator& operator++()   {      return *this;   }   utf16_output_iterator& operator++(int)   {      return *this;   }   BaseIterator base()const   {      return m_position;   }private:   void push(boost::uint32_t v)const   {      if(v >= 0x10000u)      {         // begin by checking for a code point out of range:         if(v > 0x10FFFFu)            detail::invalid_utf32_code_point(v);         // split into two surrogates:         *m_position++ = static_cast<boost::uint16_t>(v >> 10) + detail::high_surrogate_base;         *m_position++ = static_cast<boost::uint16_t>(v & detail::ten_bit_mask) + detail::low_surrogate_base;      }      else      {         // 16-bit code point:         // value must not be a surrogate:         if(detail::is_surrogate(v))            detail::invalid_utf32_code_point(v);         *m_position++ = static_cast<boost::uint16_t>(v);      }   }   mutable BaseIterator m_position;};template <class BaseIterator>class utf8_output_iterator{public:   typedef void                                   difference_type;   typedef void                                   value_type;   typedef boost::uint32_t*                       pointer;   typedef boost::uint32_t&                       reference;   typedef std::output_iterator_tag               iterator_category;   utf8_output_iterator(const BaseIterator& b)      : m_position(b){}   utf8_output_iterator(const utf8_output_iterator& that)      : m_position(that.m_position){}   utf8_output_iterator& operator=(const utf8_output_iterator& that)   {      m_position = that.m_position;      return *this;   }   const utf8_output_iterator& operator*()const   {      return *this;   }   void operator=(boost::uint32_t val)const   {      push(val);   }   utf8_output_iterator& operator++()   {      return *this;   }   utf8_output_iterator& operator++(int)   {      return *this;   }   BaseIterator base()const   {      return m_position;   }private:   void push(boost::uint32_t c)const   {      if(c > 0x10FFFFu)         detail::invalid_utf32_code_point(c);      if(c < 0x80u)      {         *m_position++ = static_cast<unsigned char>(c);      }      else if(c < 0x800u)      {         *m_position++ = static_cast<unsigned char>(0xC0u + (c >> 6));         *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu));      }      else if(c < 0x10000u)      {         *m_position++ = static_cast<unsigned char>(0xE0u + (c >> 12));         *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));         *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu));      }      else      {         *m_position++ = static_cast<unsigned char>(0xF0u + (c >> 18));         *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 12) & 0x3Fu));         *m_position++ = static_cast<unsigned char>(0x80u + ((c >> 6) & 0x3Fu));         *m_position++ = static_cast<unsigned char>(0x80u + (c & 0x3Fu));      }   }   mutable BaseIterator m_position;};} // namespace boost#endif // BOOST_REGEX_UNICODE_ITERATOR_HPP

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?