utf8_codecvt_facet.ipp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // utf8_codecvt_facet.ipp
  3. // Copyright (c) 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
  4. // Andrew Lumsdaine, Indiana University (lums@osl.iu.edu).
  5. // Use, modification and distribution is subject to the Boost Software
  6. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // Please see the comments in <boost/detail/utf8_codecvt_facet.hpp> to
  9. // learn how this file should be used.
  10. #include <boost/detail/utf8_codecvt_facet.hpp>
  11. #include <cstdlib> // for multi-byte converson routines
  12. #include <cassert>
  13. #include <boost/limits.hpp>
  14. #include <boost/config.hpp>
  15. // If we don't have wstring, then Unicode support
  16. // is not available anyway, so we don't need to even
  17. // compiler this file. This also fixes the problem
  18. // with mingw, which can compile this file, but will
  19. // generate link error when building DLL.
  20. #ifndef BOOST_NO_STD_WSTRING
  21. BOOST_UTF8_BEGIN_NAMESPACE
  22. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  23. // implementation for wchar_t
  24. utf8_codecvt_facet::utf8_codecvt_facet(
  25. std::size_t no_locale_manage
  26. ) :
  27. std::codecvt<wchar_t, char, std::mbstate_t>(no_locale_manage)
  28. {}
  29. utf8_codecvt_facet::~utf8_codecvt_facet()
  30. {}
  31. // Translate incoming UTF-8 into UCS-4
  32. std::codecvt_base::result utf8_codecvt_facet::do_in(
  33. std::mbstate_t& /*state*/,
  34. const char * from,
  35. const char * from_end,
  36. const char * & from_next,
  37. wchar_t * to,
  38. wchar_t * to_end,
  39. wchar_t * & to_next
  40. ) const {
  41. // Basic algorithm: The first octet determines how many
  42. // octets total make up the UCS-4 character. The remaining
  43. // "continuing octets" all begin with "10". To convert, subtract
  44. // the amount that specifies the number of octets from the first
  45. // octet. Subtract 0x80 (1000 0000) from each continuing octet,
  46. // then mash the whole lot together. Note that each continuing
  47. // octet only uses 6 bits as unique values, so only shift by
  48. // multiples of 6 to combine.
  49. while (from != from_end && to != to_end) {
  50. // Error checking on the first octet
  51. if (invalid_leading_octet(*from)){
  52. from_next = from;
  53. to_next = to;
  54. return std::codecvt_base::error;
  55. }
  56. // The first octet is adjusted by a value dependent upon
  57. // the number of "continuing octets" encoding the character
  58. const int cont_octet_count = get_cont_octet_count(*from);
  59. const wchar_t octet1_modifier_table[] = {
  60. 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
  61. };
  62. // The unsigned char conversion is necessary in case char is
  63. // signed (I learned this the hard way)
  64. wchar_t ucs_result =
  65. (unsigned char)(*from++) - octet1_modifier_table[cont_octet_count];
  66. // Invariants :
  67. // 1) At the start of the loop, 'i' continuing characters have been
  68. // processed
  69. // 2) *from points to the next continuing character to be processed.
  70. int i = 0;
  71. while(i != cont_octet_count && from != from_end) {
  72. // Error checking on continuing characters
  73. if (invalid_continuing_octet(*from)) {
  74. from_next = from;
  75. to_next = to;
  76. return std::codecvt_base::error;
  77. }
  78. ucs_result *= (1 << 6);
  79. // each continuing character has an extra (10xxxxxx)b attached to
  80. // it that must be removed.
  81. ucs_result += (unsigned char)(*from++) - 0x80;
  82. ++i;
  83. }
  84. // If the buffer ends with an incomplete unicode character...
  85. if (from == from_end && i != cont_octet_count) {
  86. // rewind "from" to before the current character translation
  87. from_next = from - (i+1);
  88. to_next = to;
  89. return std::codecvt_base::partial;
  90. }
  91. *to++ = ucs_result;
  92. }
  93. from_next = from;
  94. to_next = to;
  95. // Were we done converting or did we run out of destination space?
  96. if(from == from_end) return std::codecvt_base::ok;
  97. else return std::codecvt_base::partial;
  98. }
  99. std::codecvt_base::result utf8_codecvt_facet::do_out(
  100. std::mbstate_t& /*state*/,
  101. const wchar_t * from,
  102. const wchar_t * from_end,
  103. const wchar_t * & from_next,
  104. char * to,
  105. char * to_end,
  106. char * & to_next
  107. ) const
  108. {
  109. // RG - consider merging this table with the other one
  110. const wchar_t octet1_modifier_table[] = {
  111. 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
  112. };
  113. wchar_t max_wchar = (std::numeric_limits<wchar_t>::max)();
  114. while (from != from_end && to != to_end) {
  115. // Check for invalid UCS-4 character
  116. if (*from > max_wchar) {
  117. from_next = from;
  118. to_next = to;
  119. return std::codecvt_base::error;
  120. }
  121. int cont_octet_count = get_cont_octet_out_count(*from);
  122. // RG - comment this formula better
  123. int shift_exponent = (cont_octet_count) * 6;
  124. // Process the first character
  125. *to++ = static_cast<char>(octet1_modifier_table[cont_octet_count] +
  126. (unsigned char)(*from / (1 << shift_exponent)));
  127. // Process the continuation characters
  128. // Invariants: At the start of the loop:
  129. // 1) 'i' continuing octets have been generated
  130. // 2) '*to' points to the next location to place an octet
  131. // 3) shift_exponent is 6 more than needed for the next octet
  132. int i = 0;
  133. while (i != cont_octet_count && to != to_end) {
  134. shift_exponent -= 6;
  135. *to++ = static_cast<char>(0x80 + ((*from / (1 << shift_exponent)) % (1 << 6)));
  136. ++i;
  137. }
  138. // If we filled up the out buffer before encoding the character
  139. if(to == to_end && i != cont_octet_count) {
  140. from_next = from;
  141. to_next = to - (i+1);
  142. return std::codecvt_base::partial;
  143. }
  144. ++from;
  145. }
  146. from_next = from;
  147. to_next = to;
  148. // Were we done or did we run out of destination space
  149. if(from == from_end) return std::codecvt_base::ok;
  150. else return std::codecvt_base::partial;
  151. }
  152. // How many char objects can I process to get <= max_limit
  153. // wchar_t objects?
  154. int utf8_codecvt_facet::do_length(
  155. std::mbstate_t &,
  156. const char * from,
  157. const char * from_end,
  158. std::size_t max_limit
  159. ) const
  160. #if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
  161. throw()
  162. #endif
  163. {
  164. const char * from_next = from;
  165. for (std::size_t char_count = 0u; char_count < max_limit && from_next < from_end; ++char_count) {
  166. unsigned int octet_count = get_octet_count(*from_next);
  167. // The buffer may represent incomplete characters, so terminate early if one is found
  168. if (octet_count > static_cast<std::size_t>(from_end - from_next))
  169. break;
  170. from_next += octet_count;
  171. }
  172. return static_cast<int>(from_next - from);
  173. }
  174. unsigned int utf8_codecvt_facet::get_octet_count(
  175. unsigned char lead_octet
  176. ){
  177. // if the 0-bit (MSB) is 0, then 1 character
  178. if (lead_octet <= 0x7f) return 1;
  179. // Otherwise the count number of consecutive 1 bits starting at MSB
  180. // assert(0xc0 <= lead_octet && lead_octet <= 0xfd);
  181. if (0xc0 <= lead_octet && lead_octet <= 0xdf) return 2;
  182. else if (0xe0 <= lead_octet && lead_octet <= 0xef) return 3;
  183. else if (0xf0 <= lead_octet && lead_octet <= 0xf7) return 4;
  184. else if (0xf8 <= lead_octet && lead_octet <= 0xfb) return 5;
  185. else return 6;
  186. }
  187. namespace detail {
  188. template<std::size_t s>
  189. int get_cont_octet_out_count_impl(wchar_t word){
  190. if (word < 0x80) {
  191. return 0;
  192. }
  193. if (word < 0x800) {
  194. return 1;
  195. }
  196. return 2;
  197. }
  198. template<>
  199. int get_cont_octet_out_count_impl<4>(wchar_t word){
  200. if (word < 0x80) {
  201. return 0;
  202. }
  203. if (word < 0x800) {
  204. return 1;
  205. }
  206. // Note that the following code will generate warnings on some platforms
  207. // where wchar_t is defined as UCS2. The warnings are superfluous as the
  208. // specialization is never instantitiated with such compilers, but this
  209. // can cause problems if warnings are being treated as errors, so we guard
  210. // against that. Including <boost/detail/utf8_codecvt_facet.hpp> as we do
  211. // should be enough to get WCHAR_MAX defined.
  212. #if !defined(WCHAR_MAX)
  213. # error WCHAR_MAX not defined!
  214. #endif
  215. // cope with VC++ 7.1 or earlier having invalid WCHAR_MAX
  216. #if defined(_MSC_VER) && _MSC_VER <= 1310 // 7.1 or earlier
  217. return 2;
  218. #elif WCHAR_MAX > 0x10000
  219. if (word < 0x10000) {
  220. return 2;
  221. }
  222. if (word < 0x200000) {
  223. return 3;
  224. }
  225. if (word < 0x4000000) {
  226. return 4;
  227. }
  228. return 5;
  229. #else
  230. return 2;
  231. #endif
  232. }
  233. } // namespace detail
  234. // How many "continuing octets" will be needed for this word
  235. // == total octets - 1.
  236. int utf8_codecvt_facet::get_cont_octet_out_count(
  237. wchar_t word
  238. ) const {
  239. return detail::get_cont_octet_out_count_impl<sizeof(wchar_t)>(word);
  240. }
  241. BOOST_UTF8_END_NAMESPACE
  242. #endif