1
0

checked.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  41. #define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  42. #include <stdexcept>
  43. #include "core.h"
  44. namespace utf8 {
  45. // Base for the exceptions that may be thrown from the library
  46. class exception : public ::std::exception {};
  47. // Exceptions that may be thrown from the library functions.
  48. class invalid_code_point : public exception {
  49. uint32_t cp;
  50. public:
  51. invalid_code_point(uint32_t cp) : cp(cp) {}
  52. virtual const char* what() const throw() { return "Invalid code point"; }
  53. uint32_t code_point() const { return cp; }
  54. };
  55. class invalid_utf8 : public exception {
  56. uint8_t u8;
  57. public:
  58. invalid_utf8(uint8_t u) : u8(u) {}
  59. virtual const char* what() const throw() { return "Invalid UTF-8"; }
  60. uint8_t utf8_octet() const { return u8; }
  61. };
  62. class invalid_utf16 : public exception {
  63. uint16_t u16;
  64. public:
  65. invalid_utf16(uint16_t u) : u16(u) {}
  66. virtual const char* what() const throw() { return "Invalid UTF-16"; }
  67. uint16_t utf16_word() const { return u16; }
  68. };
  69. class not_enough_room : public exception {
  70. public:
  71. virtual const char* what() const throw() { return "Not enough space"; }
  72. };
  73. /// The library API - functions intended to be called by the users
  74. template <typename octet_iterator>
  75. octet_iterator append(uint32_t cp, octet_iterator result) {
  76. if (!utf8::internal::is_code_point_valid(cp)) throw invalid_code_point(cp);
  77. if (cp < 0x80) // one octet
  78. *(result++) = static_cast<uint8_t>(cp);
  79. else if (cp < 0x800) { // two octets
  80. *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
  81. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  82. } else if (cp < 0x10000) { // three octets
  83. *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
  84. *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
  85. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  86. } else { // four octets
  87. *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
  88. *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
  89. *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
  90. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  91. }
  92. return result;
  93. }
  94. template <typename octet_iterator, typename output_iterator>
  95. output_iterator replace_invalid(octet_iterator start, octet_iterator end,
  96. output_iterator out, uint32_t replacement) {
  97. while (start != end) {
  98. octet_iterator sequence_start = start;
  99. internal::utf_error err_code = utf8::internal::validate_next(start, end);
  100. switch (err_code) {
  101. case internal::UTF8_OK:
  102. for (octet_iterator it = sequence_start; it != start; ++it)
  103. *out++ = *it;
  104. break;
  105. case internal::NOT_ENOUGH_ROOM:
  106. throw not_enough_room();
  107. case internal::INVALID_LEAD:
  108. out = utf8::append(replacement, out);
  109. ++start;
  110. break;
  111. case internal::INCOMPLETE_SEQUENCE:
  112. case internal::OVERLONG_SEQUENCE:
  113. case internal::INVALID_CODE_POINT:
  114. out = utf8::append(replacement, out);
  115. ++start;
  116. // just one replacement mark for the sequence
  117. while (start != end && utf8::internal::is_trail(*start)) ++start;
  118. break;
  119. }
  120. }
  121. return out;
  122. }
  123. template <typename octet_iterator, typename output_iterator>
  124. inline output_iterator replace_invalid(octet_iterator start, octet_iterator end,
  125. output_iterator out) {
  126. static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
  127. return utf8::replace_invalid(start, end, out, replacement_marker);
  128. }
  129. template <typename octet_iterator>
  130. uint32_t next(octet_iterator& it, octet_iterator end) {
  131. uint32_t cp = 0;
  132. internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
  133. switch (err_code) {
  134. case internal::UTF8_OK:
  135. break;
  136. case internal::NOT_ENOUGH_ROOM:
  137. throw not_enough_room();
  138. case internal::INVALID_LEAD:
  139. case internal::INCOMPLETE_SEQUENCE:
  140. case internal::OVERLONG_SEQUENCE:
  141. throw invalid_utf8(*it);
  142. case internal::INVALID_CODE_POINT:
  143. throw invalid_code_point(cp);
  144. }
  145. return cp;
  146. }
  147. template <typename octet_iterator>
  148. uint32_t peek_next(octet_iterator it, octet_iterator end) {
  149. return utf8::next(it, end);
  150. }
  151. template <typename octet_iterator>
  152. uint32_t prior(octet_iterator& it, octet_iterator start) {
  153. // can't do much if it == start
  154. if (it == start) throw not_enough_room();
  155. octet_iterator end = it;
  156. // Go back until we hit either a lead octet or start
  157. while (utf8::internal::is_trail(*(--it)))
  158. if (it == start)
  159. throw invalid_utf8(*it); // error - no lead byte in the sequence
  160. return utf8::peek_next(it, end);
  161. }
  162. /// Deprecated in versions that include "prior"
  163. template <typename octet_iterator>
  164. uint32_t previous(octet_iterator& it, octet_iterator pass_start) {
  165. octet_iterator end = it;
  166. while (utf8::internal::is_trail(*(--it)))
  167. if (it == pass_start)
  168. throw invalid_utf8(*it); // error - no lead byte in the sequence
  169. octet_iterator temp = it;
  170. return utf8::next(temp, end);
  171. }
  172. template <typename octet_iterator, typename distance_type>
  173. void advance(octet_iterator& it, distance_type n, octet_iterator end) {
  174. for (distance_type i = 0; i < n; ++i) utf8::next(it, end);
  175. }
  176. template <typename octet_iterator>
  177. typename std::iterator_traits<octet_iterator>::difference_type distance(
  178. octet_iterator first, octet_iterator last) {
  179. typename std::iterator_traits<octet_iterator>::difference_type dist;
  180. for (dist = 0; first < last; ++dist) utf8::next(first, last);
  181. return dist;
  182. }
  183. template <typename u16bit_iterator, typename octet_iterator>
  184. octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end,
  185. octet_iterator result) {
  186. while (start != end) {
  187. uint32_t cp = utf8::internal::mask16(*start++);
  188. // Take care of surrogate pairs first
  189. if (utf8::internal::is_lead_surrogate(cp)) {
  190. if (start != end) {
  191. uint32_t trail_surrogate = utf8::internal::mask16(*start++);
  192. if (utf8::internal::is_trail_surrogate(trail_surrogate))
  193. cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
  194. else
  195. throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
  196. } else
  197. throw invalid_utf16(static_cast<uint16_t>(cp));
  198. }
  199. // Lone trail surrogate
  200. else if (utf8::internal::is_trail_surrogate(cp))
  201. throw invalid_utf16(static_cast<uint16_t>(cp));
  202. result = utf8::append(cp, result);
  203. }
  204. return result;
  205. }
  206. template <typename u16bit_iterator, typename octet_iterator>
  207. u16bit_iterator utf8to16(octet_iterator start, octet_iterator end,
  208. u16bit_iterator result) {
  209. while (start != end) {
  210. uint32_t cp = utf8::next(start, end);
  211. if (cp > 0xffff) { // make a surrogate pair
  212. *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
  213. *result++ =
  214. static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
  215. } else
  216. *result++ = static_cast<uint16_t>(cp);
  217. }
  218. return result;
  219. }
  220. template <typename octet_iterator, typename u32bit_iterator>
  221. octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end,
  222. octet_iterator result) {
  223. while (start != end) result = utf8::append(*(start++), result);
  224. return result;
  225. }
  226. template <typename octet_iterator, typename u32bit_iterator>
  227. u32bit_iterator utf8to32(octet_iterator start, octet_iterator end,
  228. u32bit_iterator result) {
  229. while (start != end) (*result++) = utf8::next(start, end);
  230. return result;
  231. }
  232. // The iterator class
  233. template <typename octet_iterator>
  234. class iterator
  235. : public std::iterator<std::bidirectional_iterator_tag, uint32_t> {
  236. octet_iterator it;
  237. octet_iterator range_start;
  238. octet_iterator range_end;
  239. public:
  240. iterator() {}
  241. explicit iterator(const octet_iterator& octet_it,
  242. const octet_iterator& range_start,
  243. const octet_iterator& range_end)
  244. : it(octet_it), range_start(range_start), range_end(range_end) {
  245. if (it < range_start || it > range_end)
  246. throw std::out_of_range("Invalid utf-8 iterator position");
  247. }
  248. // the default "big three" are OK
  249. octet_iterator base() const { return it; }
  250. uint32_t operator*() const {
  251. octet_iterator temp = it;
  252. return utf8::next(temp, range_end);
  253. }
  254. bool operator==(const iterator& rhs) const {
  255. if (range_start != rhs.range_start || range_end != rhs.range_end)
  256. throw std::logic_error(
  257. "Comparing utf-8 iterators defined with different ranges");
  258. return (it == rhs.it);
  259. }
  260. bool operator!=(const iterator& rhs) const { return !(operator==(rhs)); }
  261. iterator& operator++() {
  262. utf8::next(it, range_end);
  263. return *this;
  264. }
  265. iterator operator++(int) {
  266. iterator temp = *this;
  267. utf8::next(it, range_end);
  268. return temp;
  269. }
  270. iterator& operator--() {
  271. utf8::prior(it, range_start);
  272. return *this;
  273. }
  274. iterator operator--(int) {
  275. iterator temp = *this;
  276. utf8::prior(it, range_start);
  277. return temp;
  278. }
  279. }; // class iterator
  280. } // namespace utf8
  281. #endif // header guard