utf8_codecvt.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // Copyright (c) 2015 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_NOWIDE_UTF8_CODECVT_HPP_INCLUDED
  9. #define BOOST_NOWIDE_UTF8_CODECVT_HPP_INCLUDED
  10. #include <boost/nowide/detail/utf.hpp>
  11. #include <boost/nowide/replacement.hpp>
  12. #include <boost/cstdint.hpp>
  13. #include <boost/static_assert.hpp>
  14. #include <locale>
  15. namespace boost {
  16. namespace nowide {
  17. // Make sure that mbstate can keep 16 bit of UTF-16 sequence
  18. BOOST_STATIC_ASSERT(sizeof(std::mbstate_t) >= 2);
  19. namespace detail {
  20. // Avoid including cstring for std::memcpy
  21. inline void copy_uint16_t(void* dst, const void* src)
  22. {
  23. unsigned char* cdst = static_cast<unsigned char*>(dst);
  24. const unsigned char* csrc = static_cast<const unsigned char*>(src);
  25. cdst[0] = csrc[0];
  26. cdst[1] = csrc[1];
  27. }
  28. inline boost::uint16_t read_state(const std::mbstate_t& src)
  29. {
  30. boost::uint16_t dst;
  31. copy_uint16_t(&dst, &src);
  32. return dst;
  33. }
  34. inline void write_state(std::mbstate_t& dst, const boost::uint16_t src)
  35. {
  36. copy_uint16_t(&dst, &src);
  37. }
  38. } // namespace detail
  39. #if defined _MSC_VER && _MSC_VER < 1700
  40. // MSVC do_length is non-standard it counts wide characters instead of narrow and does not change mbstate
  41. #define BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
  42. #endif
  43. /// std::codecvt implementation that converts between UTF-8 and UTF-16 or UTF-32
  44. ///
  45. /// @tparam CharSize Determines the encoding: 2 for UTF-16, 4 for UTF-32
  46. ///
  47. /// Invalid sequences are replaced by #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  48. /// A trailing incomplete sequence will result in a return value of std::codecvt::partial
  49. template<typename CharType, int CharSize = sizeof(CharType)>
  50. class utf8_codecvt;
  51. /// Specialization for the UTF-8 <-> UTF-16 variant of the std::codecvt implementation
  52. template<typename CharType>
  53. class BOOST_SYMBOL_VISIBLE utf8_codecvt<CharType, 2> : public std::codecvt<CharType, char, std::mbstate_t>
  54. {
  55. public:
  56. BOOST_STATIC_ASSERT_MSG(sizeof(CharType) >= 2, "CharType must be able to store UTF16 code point");
  57. utf8_codecvt(size_t refs = 0) : std::codecvt<CharType, char, std::mbstate_t>(refs)
  58. {}
  59. protected:
  60. typedef CharType uchar;
  61. virtual std::codecvt_base::result do_unshift(std::mbstate_t& s, char* from, char* /*to*/, char*& next) const
  62. {
  63. if(detail::read_state(s) != 0)
  64. return std::codecvt_base::error;
  65. next = from;
  66. return std::codecvt_base::ok;
  67. }
  68. virtual int do_encoding() const throw()
  69. {
  70. return 0;
  71. }
  72. virtual int do_max_length() const throw()
  73. {
  74. return 4;
  75. }
  76. virtual bool do_always_noconv() const throw()
  77. {
  78. return false;
  79. }
  80. virtual int do_length(std::mbstate_t
  81. #ifdef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
  82. const
  83. #endif
  84. & std_state,
  85. const char* from,
  86. const char* from_end,
  87. size_t max) const
  88. {
  89. boost::uint16_t state = detail::read_state(std_state);
  90. #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
  91. const char* save_from = from;
  92. #else
  93. size_t save_max = max;
  94. #endif
  95. while(max > 0 && from < from_end)
  96. {
  97. const char* prev_from = from;
  98. boost::uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
  99. if(ch == detail::utf::illegal)
  100. {
  101. ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  102. } else if(ch == detail::utf::incomplete)
  103. {
  104. from = prev_from;
  105. break;
  106. }
  107. max--;
  108. if(ch > 0xFFFF)
  109. {
  110. if(state == 0)
  111. {
  112. from = prev_from;
  113. state = 1;
  114. } else
  115. {
  116. state = 0;
  117. }
  118. }
  119. }
  120. #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
  121. detail::write_state(std_state, state);
  122. return static_cast<int>(from - save_from);
  123. #else
  124. return static_cast<int>(save_max - max);
  125. #endif
  126. }
  127. virtual std::codecvt_base::result do_in(std::mbstate_t& std_state,
  128. const char* from,
  129. const char* from_end,
  130. const char*& from_next,
  131. uchar* to,
  132. uchar* to_end,
  133. uchar*& to_next) const
  134. {
  135. std::codecvt_base::result r = std::codecvt_base::ok;
  136. // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
  137. // according to standard. We use it to keep a flag 0/1 for surrogate pair writing
  138. //
  139. // if 0 no code above >0xFFFF observed, of 1 a code above 0xFFFF observed
  140. // and first pair is written, but no input consumed
  141. boost::uint16_t state = detail::read_state(std_state);
  142. while(to < to_end && from < from_end)
  143. {
  144. const char* from_saved = from;
  145. uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
  146. if(ch == detail::utf::illegal)
  147. {
  148. ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  149. } else if(ch == detail::utf::incomplete)
  150. {
  151. from = from_saved;
  152. r = std::codecvt_base::partial;
  153. break;
  154. }
  155. // Normal codepoints go directly to stream
  156. if(ch <= 0xFFFF)
  157. {
  158. *to++ = static_cast<CharType>(ch);
  159. } else
  160. {
  161. // for other codepoints we do following
  162. //
  163. // 1. We can't consume our input as we may find ourself
  164. // in state where all input consumed but not all output written,i.e. only
  165. // 1st pair is written
  166. // 2. We only write first pair and mark this in the state, we also revert back
  167. // the from pointer in order to make sure this codepoint would be read
  168. // once again and then we would consume our input together with writing
  169. // second surrogate pair
  170. ch -= 0x10000;
  171. boost::uint16_t vh = static_cast<boost::uint16_t>(ch >> 10);
  172. boost::uint16_t vl = ch & 0x3FF;
  173. boost::uint16_t w1 = vh + 0xD800;
  174. boost::uint16_t w2 = vl + 0xDC00;
  175. if(state == 0)
  176. {
  177. from = from_saved;
  178. *to++ = static_cast<CharType>(w1);
  179. state = 1;
  180. } else
  181. {
  182. *to++ = static_cast<CharType>(w2);
  183. state = 0;
  184. }
  185. }
  186. }
  187. from_next = from;
  188. to_next = to;
  189. if(r == std::codecvt_base::ok && (from != from_end || state != 0))
  190. r = std::codecvt_base::partial;
  191. detail::write_state(std_state, state);
  192. return r;
  193. }
  194. virtual std::codecvt_base::result do_out(std::mbstate_t& std_state,
  195. const uchar* from,
  196. const uchar* from_end,
  197. const uchar*& from_next,
  198. char* to,
  199. char* to_end,
  200. char*& to_next) const
  201. {
  202. std::codecvt_base::result r = std::codecvt_base::ok;
  203. // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
  204. // according to standard. We assume that sizeof(mbstate_t) >=2 in order
  205. // to be able to store first observed surrogate pair
  206. //
  207. // State: state!=0 - a first surrogate pair was observed (state = first pair),
  208. // we expect the second one to come and then zero the state
  209. ///
  210. boost::uint16_t state = detail::read_state(std_state);
  211. while(to < to_end && from < from_end)
  212. {
  213. boost::uint32_t ch = 0;
  214. if(state != 0)
  215. {
  216. // if the state indicates that 1st surrogate pair was written
  217. // we should make sure that the second one that comes is actually
  218. // second surrogate
  219. boost::uint16_t w1 = state;
  220. boost::uint16_t w2 = *from;
  221. // we don't forward from as writing may fail to incomplete or
  222. // partial conversion
  223. if(0xDC00 <= w2 && w2 <= 0xDFFF)
  224. {
  225. boost::uint16_t vh = w1 - 0xD800;
  226. boost::uint16_t vl = w2 - 0xDC00;
  227. ch = ((uint32_t(vh) << 10) | vl) + 0x10000;
  228. } else
  229. {
  230. ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  231. }
  232. } else
  233. {
  234. ch = *from;
  235. if(0xD800 <= ch && ch <= 0xDBFF)
  236. {
  237. // if this is a first surrogate pair we put
  238. // it into the state and consume it, note we don't
  239. // go forward as it should be illegal so we increase
  240. // the from pointer manually
  241. state = static_cast<boost::uint16_t>(ch);
  242. from++;
  243. continue;
  244. } else if(0xDC00 <= ch && ch <= 0xDFFF)
  245. {
  246. // if we observe second surrogate pair and
  247. // first only may be expected we should break from the loop with error
  248. // as it is illegal input
  249. ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  250. }
  251. }
  252. if(!detail::utf::is_valid_codepoint(ch))
  253. {
  254. r = std::codecvt_base::error;
  255. break;
  256. }
  257. int len = detail::utf::utf_traits<char>::width(ch);
  258. if(to_end - to < len)
  259. {
  260. r = std::codecvt_base::partial;
  261. break;
  262. }
  263. to = detail::utf::utf_traits<char>::encode(ch, to);
  264. state = 0;
  265. from++;
  266. }
  267. from_next = from;
  268. to_next = to;
  269. if(r == std::codecvt_base::ok && (from != from_end || state != 0))
  270. r = std::codecvt_base::partial;
  271. detail::write_state(std_state, state);
  272. return r;
  273. }
  274. };
  275. /// Specialization for the UTF-8 <-> UTF-32 variant of the std::codecvt implementation
  276. template<typename CharType>
  277. class BOOST_SYMBOL_VISIBLE utf8_codecvt<CharType, 4> : public std::codecvt<CharType, char, std::mbstate_t>
  278. {
  279. public:
  280. utf8_codecvt(size_t refs = 0) : std::codecvt<CharType, char, std::mbstate_t>(refs)
  281. {}
  282. protected:
  283. typedef CharType uchar;
  284. virtual std::codecvt_base::result do_unshift(std::mbstate_t& /*s*/, char* from, char* /*to*/, char*& next) const
  285. {
  286. next = from;
  287. return std::codecvt_base::ok;
  288. }
  289. virtual int do_encoding() const throw()
  290. {
  291. return 0;
  292. }
  293. virtual int do_max_length() const throw()
  294. {
  295. return 4;
  296. }
  297. virtual bool do_always_noconv() const throw()
  298. {
  299. return false;
  300. }
  301. virtual int do_length(std::mbstate_t
  302. #ifdef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
  303. const
  304. #endif
  305. & /*state*/,
  306. const char* from,
  307. const char* from_end,
  308. size_t max) const
  309. {
  310. #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
  311. const char* start_from = from;
  312. #else
  313. size_t save_max = max;
  314. #endif
  315. while(max > 0 && from < from_end)
  316. {
  317. const char* save_from = from;
  318. boost::uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
  319. if(ch == detail::utf::incomplete)
  320. {
  321. from = save_from;
  322. break;
  323. } else if(ch == detail::utf::illegal)
  324. {
  325. ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  326. }
  327. max--;
  328. }
  329. #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
  330. return from - start_from;
  331. #else
  332. return save_max - max;
  333. #endif
  334. }
  335. virtual std::codecvt_base::result do_in(std::mbstate_t& /*state*/,
  336. const char* from,
  337. const char* from_end,
  338. const char*& from_next,
  339. uchar* to,
  340. uchar* to_end,
  341. uchar*& to_next) const
  342. {
  343. std::codecvt_base::result r = std::codecvt_base::ok;
  344. while(to < to_end && from < from_end)
  345. {
  346. const char* from_saved = from;
  347. uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
  348. if(ch == detail::utf::illegal)
  349. {
  350. ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  351. } else if(ch == detail::utf::incomplete)
  352. {
  353. r = std::codecvt_base::partial;
  354. from = from_saved;
  355. break;
  356. }
  357. *to++ = ch;
  358. }
  359. from_next = from;
  360. to_next = to;
  361. if(r == std::codecvt_base::ok && from != from_end)
  362. r = std::codecvt_base::partial;
  363. return r;
  364. }
  365. virtual std::codecvt_base::result do_out(std::mbstate_t& /*std_state*/,
  366. const uchar* from,
  367. const uchar* from_end,
  368. const uchar*& from_next,
  369. char* to,
  370. char* to_end,
  371. char*& to_next) const
  372. {
  373. std::codecvt_base::result r = std::codecvt_base::ok;
  374. while(to < to_end && from < from_end)
  375. {
  376. boost::uint32_t ch = 0;
  377. ch = *from;
  378. if(!detail::utf::is_valid_codepoint(ch))
  379. {
  380. ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  381. }
  382. int len = detail::utf::utf_traits<char>::width(ch);
  383. if(to_end - to < len)
  384. {
  385. r = std::codecvt_base::partial;
  386. break;
  387. }
  388. to = detail::utf::utf_traits<char>::encode(ch, to);
  389. from++;
  390. }
  391. from_next = from;
  392. to_next = to;
  393. if(r == std::codecvt_base::ok && from != from_end)
  394. r = std::codecvt_base::partial;
  395. return r;
  396. }
  397. };
  398. } // namespace nowide
  399. } // namespace boost
  400. #endif