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