unicode_iterator.hpp 20 KB

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