unicode_iterator.hpp 23 KB

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