Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checked.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Copyright 2006 Nemanja Trifunovic
  2. /*
  3. Permission is hereby granted, free of charge, to any person or organization
  4. obtaining a copy of the software and accompanying documentation covered by
  5. this license (the "Software") to use, reproduce, display, distribute,
  6. execute, and transmit the Software, and to prepare derivative works of the
  7. Software, and to permit third-parties to whom the Software is furnished to
  8. do so, all subject to the following:
  9. The copyright notices in the Software and this entire statement, including
  10. the above license grant, this restriction and the following disclaimer,
  11. must be included in all copies of the Software, in whole or in part, and
  12. all derivative works of the Software, unless such copies or derivative
  13. works are solely in the form of machine-executable object code generated by
  14. a source language processor.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  18. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  19. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  24. #define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  25. #include "core.h"
  26. #include <stdexcept>
  27. namespace utf8
  28. {
  29. // Exceptions that may be thrown from the library functions.
  30. class invalid_code_point : public ::std::exception {
  31. uint32_t cp;
  32. public:
  33. invalid_code_point(uint32_t _cp) : cp(_cp) {}
  34. virtual const char* what() const noexcept { return "Invalid code point"; }
  35. uint32_t code_point() const {return cp;}
  36. };
  37. class invalid_utf8 : public ::std::exception {
  38. uint8_t u8;
  39. public:
  40. invalid_utf8 (uint8_t u) : u8(u) {}
  41. virtual const char* what() const noexcept { return "Invalid UTF-8"; }
  42. uint8_t utf8_octet() const {return u8;}
  43. };
  44. class invalid_utf16 : public ::std::exception {
  45. uint16_t u16;
  46. public:
  47. invalid_utf16 (uint16_t u) : u16(u) {}
  48. virtual const char* what() const noexcept { return "Invalid UTF-16"; }
  49. uint16_t utf16_word() const {return u16;}
  50. };
  51. class not_enough_room : public ::std::exception {
  52. public:
  53. virtual const char* what() const noexcept { return "Not enough space"; }
  54. };
  55. /// The library API - functions intended to be called by the users
  56. template <typename octet_iterator>
  57. octet_iterator append(uint32_t cp, octet_iterator result)
  58. {
  59. if (!utf8::internal::is_code_point_valid(cp))
  60. throw invalid_code_point(cp);
  61. if (cp < 0x80) // one octet
  62. *(result++) = static_cast<uint8_t>(cp);
  63. else if (cp < 0x800) { // two octets
  64. *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
  65. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  66. }
  67. else if (cp < 0x10000) { // three octets
  68. *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
  69. *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
  70. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  71. }
  72. else { // four octets
  73. *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
  74. *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
  75. *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
  76. *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
  77. }
  78. return result;
  79. }
  80. template <typename octet_iterator, typename output_iterator>
  81. output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
  82. {
  83. while (start != end) {
  84. octet_iterator sequence_start = start;
  85. internal::utf_error err_code = utf8::internal::validate_next(start, end);
  86. switch (err_code) {
  87. case internal::UTF8_OK :
  88. for (octet_iterator it = sequence_start; it != start; ++it)
  89. *out++ = *it;
  90. break;
  91. case internal::NOT_ENOUGH_ROOM:
  92. throw not_enough_room();
  93. case internal::INVALID_LEAD:
  94. out = utf8::append (replacement, out);
  95. ++start;
  96. break;
  97. case internal::INCOMPLETE_SEQUENCE:
  98. case internal::OVERLONG_SEQUENCE:
  99. case internal::INVALID_CODE_POINT:
  100. out = utf8::append (replacement, out);
  101. ++start;
  102. // just one replacement mark for the sequence
  103. while (start != end && utf8::internal::is_trail(*start))
  104. ++start;
  105. break;
  106. }
  107. }
  108. return out;
  109. }
  110. template <typename octet_iterator, typename output_iterator>
  111. inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
  112. {
  113. static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
  114. return utf8::replace_invalid(start, end, out, replacement_marker);
  115. }
  116. template <typename octet_iterator>
  117. uint32_t next(octet_iterator& it, octet_iterator end)
  118. {
  119. uint32_t cp = 0;
  120. internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
  121. switch (err_code) {
  122. case internal::UTF8_OK :
  123. break;
  124. case internal::NOT_ENOUGH_ROOM :
  125. throw not_enough_room();
  126. case internal::INVALID_LEAD :
  127. case internal::INCOMPLETE_SEQUENCE :
  128. case internal::OVERLONG_SEQUENCE :
  129. throw invalid_utf8(*it);
  130. case internal::INVALID_CODE_POINT :
  131. throw invalid_code_point(cp);
  132. }
  133. return cp;
  134. }
  135. template <typename octet_iterator>
  136. uint32_t peek_next(octet_iterator it, octet_iterator end)
  137. {
  138. return utf8::next(it, end);
  139. }
  140. template <typename octet_iterator>
  141. uint32_t prior(octet_iterator& it, octet_iterator start)
  142. {
  143. // can't do much if it == start
  144. if (it == start)
  145. throw not_enough_room();
  146. octet_iterator end = it;
  147. // Go back until we hit either a lead octet or start
  148. while (utf8::internal::is_trail(*(--it)))
  149. if (it == start)
  150. throw invalid_utf8(*it); // error - no lead byte in the sequence
  151. return utf8::peek_next(it, end);
  152. }
  153. /// Deprecated in versions that include "prior"
  154. template <typename octet_iterator>
  155. uint32_t previous(octet_iterator& it, octet_iterator pass_start)
  156. {
  157. octet_iterator end = it;
  158. while (utf8::internal::is_trail(*(--it)))
  159. if (it == pass_start)
  160. throw invalid_utf8(*it); // error - no lead byte in the sequence
  161. octet_iterator temp = it;
  162. return utf8::next(temp, end);
  163. }
  164. template <typename octet_iterator, typename distance_type>
  165. void advance (octet_iterator& it, distance_type n, octet_iterator end)
  166. {
  167. for (distance_type i = 0; i < n; ++i)
  168. utf8::next(it, end);
  169. }
  170. template <typename octet_iterator>
  171. typename std::iterator_traits<octet_iterator>::difference_type
  172. distance (octet_iterator first, octet_iterator last)
  173. {
  174. typename std::iterator_traits<octet_iterator>::difference_type dist;
  175. for (dist = 0; first < last; ++dist)
  176. utf8::next(first, last);
  177. return dist;
  178. }
  179. template <typename u16bit_iterator, typename octet_iterator>
  180. octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
  181. {
  182. while (start != end) {
  183. uint32_t cp = utf8::internal::mask16(*start++);
  184. // Take care of surrogate pairs first
  185. if (utf8::internal::is_lead_surrogate(cp)) {
  186. if (start != end) {
  187. uint32_t trail_surrogate = utf8::internal::mask16(*start++);
  188. if (utf8::internal::is_trail_surrogate(trail_surrogate))
  189. cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
  190. else
  191. throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
  192. }
  193. else
  194. throw invalid_utf16(static_cast<uint16_t>(cp));
  195. }
  196. // Lone trail surrogate
  197. else if (utf8::internal::is_trail_surrogate(cp))
  198. throw invalid_utf16(static_cast<uint16_t>(cp));
  199. result = utf8::append(cp, result);
  200. }
  201. return result;
  202. }
  203. template <typename u16bit_iterator, typename octet_iterator>
  204. u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
  205. {
  206. while (start != end) {
  207. uint32_t cp = utf8::next(start, end);
  208. if (cp > 0xffff) { //make a surrogate pair
  209. *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
  210. *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
  211. }
  212. else
  213. *result++ = static_cast<uint16_t>(cp);
  214. }
  215. return result;
  216. }
  217. template <typename octet_iterator, typename u32bit_iterator>
  218. octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
  219. {
  220. while (start != end)
  221. result = utf8::append(*(start++), result);
  222. return result;
  223. }
  224. template <typename octet_iterator, typename u32bit_iterator>
  225. u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
  226. {
  227. while (start != end)
  228. (*result++) = utf8::next(start, end);
  229. return result;
  230. }
  231. // The iterator class
  232. template <typename octet_iterator>
  233. class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
  234. octet_iterator it;
  235. octet_iterator range_start;
  236. octet_iterator range_end;
  237. public:
  238. iterator () {}
  239. explicit iterator (const octet_iterator& octet_it,
  240. const octet_iterator& _range_start,
  241. const octet_iterator& _range_end) :
  242. it(octet_it), range_start(_range_start), range_end(_range_end)
  243. {
  244. if (it < range_start || it > range_end)
  245. throw std::out_of_range("Invalid utf-8 iterator position");
  246. }
  247. // the default "big three" are OK
  248. octet_iterator base () const { return it; }
  249. uint32_t operator * () const
  250. {
  251. octet_iterator temp = it;
  252. return utf8::next(temp, range_end);
  253. }
  254. bool operator == (const iterator& rhs) const
  255. {
  256. if (range_start != rhs.range_start || range_end != rhs.range_end)
  257. throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
  258. return (it == rhs.it);
  259. }
  260. bool operator != (const iterator& rhs) const
  261. {
  262. return !(operator == (rhs));
  263. }
  264. iterator& operator ++ ()
  265. {
  266. utf8::next(it, range_end);
  267. return *this;
  268. }
  269. iterator operator ++ (int)
  270. {
  271. iterator temp = *this;
  272. utf8::next(it, range_end);
  273. return temp;
  274. }
  275. iterator& operator -- ()
  276. {
  277. utf8::prior(it, range_start);
  278. return *this;
  279. }
  280. iterator operator -- (int)
  281. {
  282. iterator temp = *this;
  283. utf8::prior(it, range_start);
  284. return temp;
  285. }
  286. }; // class iterator
  287. } // namespace utf8
  288. #endif //header guard