checked.h 12 KB

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