utf8_codecvt_facet.ipp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // RG - this code is confusing! I need a better way to express it.
  165. // and test cases.
  166. // Invariants:
  167. // 1) last_octet_count has the size of the last measured character
  168. // 2) char_count holds the number of characters shown to fit
  169. // within the bounds so far (no greater than max_limit)
  170. // 3) from_next points to the octet 'last_octet_count' before the
  171. // last measured character.
  172. int last_octet_count=0;
  173. std::size_t char_count = 0;
  174. const char* from_next = from;
  175. // Use "<" because the buffer may represent incomplete characters
  176. while (from_next+last_octet_count <= from_end && char_count <= max_limit) {
  177. from_next += last_octet_count;
  178. last_octet_count = (get_octet_count(*from_next));
  179. ++char_count;
  180. }
  181. return static_cast<int>(from_next-from);
  182. }
  183. unsigned int utf8_codecvt_facet::get_octet_count(
  184. unsigned char lead_octet
  185. ){
  186. // if the 0-bit (MSB) is 0, then 1 character
  187. if (lead_octet <= 0x7f) return 1;
  188. // Otherwise the count number of consecutive 1 bits starting at MSB
  189. // assert(0xc0 <= lead_octet && lead_octet <= 0xfd);
  190. if (0xc0 <= lead_octet && lead_octet <= 0xdf) return 2;
  191. else if (0xe0 <= lead_octet && lead_octet <= 0xef) return 3;
  192. else if (0xf0 <= lead_octet && lead_octet <= 0xf7) return 4;
  193. else if (0xf8 <= lead_octet && lead_octet <= 0xfb) return 5;
  194. else return 6;
  195. }
  196. namespace detail {
  197. template<std::size_t s>
  198. int get_cont_octet_out_count_impl(wchar_t word){
  199. if (word < 0x80) {
  200. return 0;
  201. }
  202. if (word < 0x800) {
  203. return 1;
  204. }
  205. return 2;
  206. }
  207. template<>
  208. int get_cont_octet_out_count_impl<4>(wchar_t word){
  209. if (word < 0x80) {
  210. return 0;
  211. }
  212. if (word < 0x800) {
  213. return 1;
  214. }
  215. // Note that the following code will generate warnings on some platforms
  216. // where wchar_t is defined as UCS2. The warnings are superfluous as the
  217. // specialization is never instantitiated with such compilers, but this
  218. // can cause problems if warnings are being treated as errors, so we guard
  219. // against that. Including <boost/detail/utf8_codecvt_facet.hpp> as we do
  220. // should be enough to get WCHAR_MAX defined.
  221. #if !defined(WCHAR_MAX)
  222. # error WCHAR_MAX not defined!
  223. #endif
  224. // cope with VC++ 7.1 or earlier having invalid WCHAR_MAX
  225. #if defined(_MSC_VER) && _MSC_VER <= 1310 // 7.1 or earlier
  226. return 2;
  227. #elif WCHAR_MAX > 0x10000
  228. if (word < 0x10000) {
  229. return 2;
  230. }
  231. if (word < 0x200000) {
  232. return 3;
  233. }
  234. if (word < 0x4000000) {
  235. return 4;
  236. }
  237. return 5;
  238. #else
  239. return 2;
  240. #endif
  241. }
  242. } // namespace detail
  243. // How many "continuing octets" will be needed for this word
  244. // == total octets - 1.
  245. int utf8_codecvt_facet::get_cont_octet_out_count(
  246. wchar_t word
  247. ) const {
  248. return detail::get_cont_octet_out_count_impl<sizeof(wchar_t)>(word);
  249. }
  250. BOOST_UTF8_END_NAMESPACE
  251. #endif