unchecked.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. // Copyright 2006 Nemanja Trifunovic
  18. /*
  19. Permission is hereby granted, free of charge, to any person or organization
  20. obtaining a copy of the software and accompanying documentation covered by
  21. this license (the "Software") to use, reproduce, display, distribute,
  22. execute, and transmit the Software, and to prepare derivative works of the
  23. Software, and to permit third-parties to whom the Software is furnished to
  24. do so, all subject to the following:
  25. The copyright notices in the Software and this entire statement, including
  26. the above license grant, this restriction and the following disclaimer,
  27. must be included in all copies of the Software, in whole or in part, and
  28. all derivative works of the Software, unless such copies or derivative
  29. works are solely in the form of machine-executable object code generated by
  30. a source language processor.
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  34. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  35. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  36. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  37. DEALINGS IN THE SOFTWARE.
  38. */
  39. #ifndef UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  40. #define UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  41. #include "core.h"
  42. namespace KC {
  43. namespace utf8 {
  44. namespace unchecked {
  45. template <typename octet_iterator>
  46. octet_iterator append(uint32_t cp, octet_iterator result)
  47. {
  48. if (cp < 0x80) // one octet
  49. *(result++) = static_cast<uint8_t>(cp);
  50. else if (cp < 0x800) { // two octets
  51. *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
  52. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  53. }
  54. else if (cp < 0x10000) { // three octets
  55. *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
  56. *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
  57. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  58. }
  59. else { // four octets
  60. *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
  61. *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f)| 0x80);
  62. *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
  63. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  64. }
  65. return result;
  66. }
  67. template <typename octet_iterator>
  68. uint32_t next(octet_iterator& it)
  69. {
  70. uint32_t cp = internal::mask8(*it);
  71. typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
  72. switch (length) {
  73. case 1:
  74. break;
  75. case 2:
  76. ++it;
  77. cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
  78. break;
  79. case 3:
  80. ++it;
  81. cp = ((cp << 12) & 0xffff) + ((internal::mask8(*it) << 6) & 0xfff);
  82. ++it;
  83. cp += (*it) & 0x3f;
  84. break;
  85. case 4:
  86. ++it;
  87. cp = ((cp << 18) & 0x1fffff) + ((internal::mask8(*it) << 12) & 0x3ffff);
  88. ++it;
  89. cp += (internal::mask8(*it) << 6) & 0xfff;
  90. ++it;
  91. cp += (*it) & 0x3f;
  92. break;
  93. }
  94. ++it;
  95. return cp;
  96. }
  97. template <typename octet_iterator>
  98. uint32_t peek_next(octet_iterator it)
  99. {
  100. return next(it);
  101. }
  102. template <typename octet_iterator>
  103. uint32_t prior(octet_iterator& it)
  104. {
  105. while (internal::is_trail(*(--it))) ;
  106. octet_iterator temp = it;
  107. return next(temp);
  108. }
  109. // Deprecated in versions that include prior, but only for the sake of consistency (see utf8::previous)
  110. template <typename octet_iterator>
  111. inline uint32_t previous(octet_iterator& it)
  112. {
  113. return prior(it);
  114. }
  115. template <typename octet_iterator, typename distance_type>
  116. void advance (octet_iterator& it, distance_type n)
  117. {
  118. for (distance_type i = 0; i < n; ++i)
  119. next(it);
  120. }
  121. template <typename octet_iterator>
  122. typename std::iterator_traits<octet_iterator>::difference_type
  123. distance (octet_iterator first, octet_iterator last)
  124. {
  125. typename std::iterator_traits<octet_iterator>::difference_type dist;
  126. for (dist = 0; first < last; ++dist)
  127. next(first);
  128. return dist;
  129. }
  130. template <typename u16bit_iterator, typename octet_iterator>
  131. octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
  132. {
  133. while (start != end) {
  134. uint32_t cp = internal::mask16(*start++);
  135. // Take care of surrogate pairs first
  136. if (internal::is_surrogate(cp)) {
  137. uint32_t trail_surrogate = internal::mask16(*start++);
  138. cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
  139. }
  140. result = append(cp, result);
  141. }
  142. return result;
  143. }
  144. template <typename u16bit_iterator, typename octet_iterator>
  145. u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
  146. {
  147. while (start != end) {
  148. uint32_t cp = next(start);
  149. if (cp > 0xffff) { //make a surrogate pair
  150. *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
  151. *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
  152. }
  153. else
  154. *result++ = static_cast<uint16_t>(cp);
  155. }
  156. return result;
  157. }
  158. template <typename octet_iterator, typename u32bit_iterator>
  159. octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
  160. {
  161. while (start != end)
  162. result = append(*(start++), result);
  163. return result;
  164. }
  165. template <typename octet_iterator, typename u32bit_iterator>
  166. u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
  167. {
  168. while (start < end)
  169. (*result++) = next(start);
  170. return result;
  171. }
  172. // The iterator class
  173. template <typename octet_iterator>
  174. class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
  175. octet_iterator it;
  176. public:
  177. iterator(void) = default;
  178. explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
  179. // the default "big three" are OK
  180. octet_iterator base () const { return it; }
  181. uint32_t operator * () const
  182. {
  183. octet_iterator temp = it;
  184. return next(temp);
  185. }
  186. bool operator == (const iterator& rhs) const
  187. {
  188. return (it == rhs.it);
  189. }
  190. bool operator != (const iterator& rhs) const
  191. {
  192. return !(operator == (rhs));
  193. }
  194. iterator& operator ++ ()
  195. {
  196. std::advance(it, internal::sequence_length(it));
  197. return *this;
  198. }
  199. iterator operator++(int) const
  200. {
  201. iterator temp = *this;
  202. std::advance(it, internal::sequence_length(it));
  203. return temp;
  204. }
  205. iterator& operator -- ()
  206. {
  207. prior(it);
  208. return *this;
  209. }
  210. iterator operator--(int) const
  211. {
  212. iterator temp = *this;
  213. prior(it);
  214. return temp;
  215. }
  216. }; // class iterator
  217. } // namespace utf8::unchecked
  218. } // namespace utf8
  219. } /* namespace */
  220. #endif // header guard