utf8_codecvt_facet.ipp 9.3 KB

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