utf8_codecvt.hpp 16 KB

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