unchecked.h 7.5 KB

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