utf.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_LOCALE_UTF_HPP_INCLUDED
  7. #define BOOST_LOCALE_UTF_HPP_INCLUDED
  8. #include <boost/locale/config.hpp>
  9. #include <boost/cstdint.hpp>
  10. namespace boost { namespace locale {
  11. /// \brief Namespace that holds basic operations on UTF encoded sequences
  12. ///
  13. /// All functions defined in this namespace do not require linking with Boost.Locale library
  14. namespace utf {
  15. /// \brief The integral type that can hold a Unicode code point
  16. typedef uint32_t code_point;
  17. /// \brief Special constant that defines illegal code point
  18. constexpr code_point illegal = 0xFFFFFFFFu;
  19. /// \brief Special constant that defines incomplete code point
  20. constexpr code_point incomplete = 0xFFFFFFFEu;
  21. /// \brief the function checks if \a v is a valid code point
  22. inline bool is_valid_codepoint(code_point v)
  23. {
  24. if(v > 0x10FFFF)
  25. return false;
  26. if(0xD800 <= v && v <= 0xDFFF) // surrogates
  27. return false;
  28. return true;
  29. }
  30. #ifdef BOOST_LOCALE_DOXYGEN
  31. /// \brief UTF Traits class - functions to convert UTF sequences to and from Unicode code points
  32. template<typename CharType, int size = sizeof(CharType)>
  33. struct utf_traits {
  34. /// The type of the character
  35. typedef CharType char_type;
  36. /// Read one code point from the range [p,e) and return it.
  37. ///
  38. /// - If the sequence that was read is incomplete sequence returns \ref incomplete,
  39. /// - If illegal sequence detected returns \ref illegal
  40. ///
  41. /// Requirements
  42. ///
  43. /// - Iterator is valid input iterator
  44. ///
  45. /// Postconditions
  46. ///
  47. /// - p points to the last consumed character
  48. template<typename Iterator>
  49. static code_point decode(Iterator& p, Iterator e);
  50. /// Maximal width of valid sequence in the code units:
  51. ///
  52. /// - UTF-8 - 4
  53. /// - UTF-16 - 2
  54. /// - UTF-32 - 1
  55. static constexpr int max_width;
  56. /// The width of specific code point in the code units.
  57. ///
  58. /// Requirement: value is a valid Unicode code point
  59. /// Returns value in range [1..max_width]
  60. static int width(code_point value);
  61. /// Get the size of the trail part of variable length encoded sequence.
  62. ///
  63. /// Returns -1 if C is not valid lead character
  64. static int trail_length(char_type c);
  65. /// Returns true if c is trail code unit, always false for UTF-32
  66. static bool is_trail(char_type c);
  67. /// Returns true if c is lead code unit, always true of UTF-32
  68. static bool is_lead(char_type c);
  69. /// Convert valid Unicode code point \a value to the UTF sequence.
  70. ///
  71. /// Requirements:
  72. ///
  73. /// - \a value is valid code point
  74. /// - \a out is an output iterator should be able to accept at least width(value) units
  75. ///
  76. /// Returns the iterator past the last written code unit.
  77. template<typename Iterator>
  78. static Iterator encode(code_point value, Iterator out);
  79. /// Decodes valid UTF sequence that is pointed by p into code point.
  80. ///
  81. /// If the sequence is invalid or points to end the behavior is undefined
  82. template<typename Iterator>
  83. static code_point decode_valid(Iterator& p);
  84. };
  85. #else
  86. template<typename CharType, int size = sizeof(CharType)>
  87. struct utf_traits;
  88. template<typename CharType>
  89. struct utf_traits<CharType, 1> {
  90. typedef CharType char_type;
  91. static int trail_length(char_type ci)
  92. {
  93. unsigned char c = ci;
  94. if(c < 128)
  95. return 0;
  96. if(BOOST_UNLIKELY(c < 194))
  97. return -1;
  98. if(c < 224)
  99. return 1;
  100. if(c < 240)
  101. return 2;
  102. if(BOOST_LIKELY(c <= 244))
  103. return 3;
  104. return -1;
  105. }
  106. static constexpr int max_width = 4;
  107. static int width(code_point value)
  108. {
  109. if(value <= 0x7F) {
  110. return 1;
  111. } else if(value <= 0x7FF) {
  112. return 2;
  113. } else if(BOOST_LIKELY(value <= 0xFFFF)) {
  114. return 3;
  115. } else {
  116. return 4;
  117. }
  118. }
  119. static bool is_trail(char_type ci)
  120. {
  121. unsigned char c = ci;
  122. return (c & 0xC0) == 0x80;
  123. }
  124. static bool is_lead(char_type ci) { return !is_trail(ci); }
  125. template<typename Iterator>
  126. static code_point decode(Iterator& p, Iterator e)
  127. {
  128. if(BOOST_UNLIKELY(p == e))
  129. return incomplete;
  130. unsigned char lead = *p++;
  131. // First byte is fully validated here
  132. int trail_size = trail_length(lead);
  133. if(BOOST_UNLIKELY(trail_size < 0))
  134. return illegal;
  135. // Ok as only ASCII may be of size = 0
  136. // also optimize for ASCII text
  137. if(trail_size == 0)
  138. return lead;
  139. code_point c = lead & ((1 << (6 - trail_size)) - 1);
  140. // Read the rest
  141. unsigned char tmp;
  142. switch(trail_size) {
  143. case 3:
  144. if(BOOST_UNLIKELY(p == e))
  145. return incomplete;
  146. tmp = *p++;
  147. if(!is_trail(tmp))
  148. return illegal;
  149. c = (c << 6) | (tmp & 0x3F);
  150. BOOST_FALLTHROUGH;
  151. case 2:
  152. if(BOOST_UNLIKELY(p == e))
  153. return incomplete;
  154. tmp = *p++;
  155. if(!is_trail(tmp))
  156. return illegal;
  157. c = (c << 6) | (tmp & 0x3F);
  158. BOOST_FALLTHROUGH;
  159. case 1:
  160. if(BOOST_UNLIKELY(p == e))
  161. return incomplete;
  162. tmp = *p++;
  163. if(!is_trail(tmp))
  164. return illegal;
  165. c = (c << 6) | (tmp & 0x3F);
  166. }
  167. // Check code point validity: no surrogates and
  168. // valid range
  169. if(BOOST_UNLIKELY(!is_valid_codepoint(c)))
  170. return illegal;
  171. // make sure it is the most compact representation
  172. if(BOOST_UNLIKELY(width(c) != trail_size + 1))
  173. return illegal;
  174. return c;
  175. }
  176. template<typename Iterator>
  177. static code_point decode_valid(Iterator& p)
  178. {
  179. unsigned char lead = *p++;
  180. if(lead < 192)
  181. return lead;
  182. int trail_size;
  183. if(lead < 224)
  184. trail_size = 1;
  185. else if(BOOST_LIKELY(lead < 240)) // non-BMP rare
  186. trail_size = 2;
  187. else
  188. trail_size = 3;
  189. code_point c = lead & ((1 << (6 - trail_size)) - 1);
  190. switch(trail_size) {
  191. case 3: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F); BOOST_FALLTHROUGH;
  192. case 2: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F); BOOST_FALLTHROUGH;
  193. case 1: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F);
  194. }
  195. return c;
  196. }
  197. template<typename Iterator>
  198. static Iterator encode(code_point value, Iterator out)
  199. {
  200. if(value <= 0x7F) {
  201. *out++ = static_cast<char_type>(value);
  202. } else if(value <= 0x7FF) {
  203. *out++ = static_cast<char_type>((value >> 6) | 0xC0);
  204. *out++ = static_cast<char_type>((value & 0x3F) | 0x80);
  205. } else if(BOOST_LIKELY(value <= 0xFFFF)) {
  206. *out++ = static_cast<char_type>((value >> 12) | 0xE0);
  207. *out++ = static_cast<char_type>(((value >> 6) & 0x3F) | 0x80);
  208. *out++ = static_cast<char_type>((value & 0x3F) | 0x80);
  209. } else {
  210. *out++ = static_cast<char_type>((value >> 18) | 0xF0);
  211. *out++ = static_cast<char_type>(((value >> 12) & 0x3F) | 0x80);
  212. *out++ = static_cast<char_type>(((value >> 6) & 0x3F) | 0x80);
  213. *out++ = static_cast<char_type>((value & 0x3F) | 0x80);
  214. }
  215. return out;
  216. }
  217. }; // utf8
  218. template<typename CharType>
  219. struct utf_traits<CharType, 2> {
  220. typedef CharType char_type;
  221. // See RFC 2781
  222. static bool is_first_surrogate(uint16_t x) { return 0xD800 <= x && x <= 0xDBFF; }
  223. static bool is_second_surrogate(uint16_t x) { return 0xDC00 <= x && x <= 0xDFFF; }
  224. static code_point combine_surrogate(uint16_t w1, uint16_t w2)
  225. {
  226. return ((code_point(w1 & 0x3FF) << 10) | (w2 & 0x3FF)) + 0x10000;
  227. }
  228. static int trail_length(char_type c)
  229. {
  230. if(is_first_surrogate(c))
  231. return 1;
  232. if(is_second_surrogate(c))
  233. return -1;
  234. return 0;
  235. }
  236. /// Returns true if c is trail code unit, always false for UTF-32
  237. static bool is_trail(char_type c) { return is_second_surrogate(c); }
  238. /// Returns true if c is lead code unit, always true of UTF-32
  239. static bool is_lead(char_type c) { return !is_second_surrogate(c); }
  240. template<typename It>
  241. static code_point decode(It& current, It last)
  242. {
  243. if(BOOST_UNLIKELY(current == last))
  244. return incomplete;
  245. uint16_t w1 = *current++;
  246. if(BOOST_LIKELY(w1 < 0xD800 || 0xDFFF < w1)) {
  247. return w1;
  248. }
  249. if(w1 > 0xDBFF)
  250. return illegal;
  251. if(current == last)
  252. return incomplete;
  253. uint16_t w2 = *current++;
  254. if(w2 < 0xDC00 || 0xDFFF < w2)
  255. return illegal;
  256. return combine_surrogate(w1, w2);
  257. }
  258. template<typename It>
  259. static code_point decode_valid(It& current)
  260. {
  261. uint16_t w1 = *current++;
  262. if(BOOST_LIKELY(w1 < 0xD800 || 0xDFFF < w1)) {
  263. return w1;
  264. }
  265. uint16_t w2 = *current++;
  266. return combine_surrogate(w1, w2);
  267. }
  268. static constexpr int max_width = 2;
  269. static int width(code_point u) { return u >= 0x10000 ? 2 : 1; }
  270. template<typename It>
  271. static It encode(code_point u, It out)
  272. {
  273. if(BOOST_LIKELY(u <= 0xFFFF)) {
  274. *out++ = static_cast<char_type>(u);
  275. } else {
  276. u -= 0x10000;
  277. *out++ = static_cast<char_type>(0xD800 | (u >> 10));
  278. *out++ = static_cast<char_type>(0xDC00 | (u & 0x3FF));
  279. }
  280. return out;
  281. }
  282. }; // utf16;
  283. template<typename CharType>
  284. struct utf_traits<CharType, 4> {
  285. typedef CharType char_type;
  286. static int trail_length(char_type c)
  287. {
  288. if(is_valid_codepoint(c))
  289. return 0;
  290. return -1;
  291. }
  292. static bool is_trail(char_type /*c*/) { return false; }
  293. static bool is_lead(char_type /*c*/) { return true; }
  294. template<typename It>
  295. static code_point decode_valid(It& current)
  296. {
  297. return *current++;
  298. }
  299. template<typename It>
  300. static code_point decode(It& current, It last)
  301. {
  302. if(BOOST_UNLIKELY(current == last))
  303. return boost::locale::utf::incomplete;
  304. code_point c = *current++;
  305. if(BOOST_UNLIKELY(!is_valid_codepoint(c)))
  306. return boost::locale::utf::illegal;
  307. return c;
  308. }
  309. static constexpr int max_width = 1;
  310. static int width(code_point /*u*/) { return 1; }
  311. template<typename It>
  312. static It encode(code_point u, It out)
  313. {
  314. *out++ = static_cast<char_type>(u);
  315. return out;
  316. }
  317. }; // utf32
  318. #endif
  319. } // namespace utf
  320. }} // namespace boost::locale
  321. #endif