utf8_codecvt_facet.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // utf8_codecvt_facet.cpp
  3. // Copyright (c) 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
  4. // Andrew Lumsdaine, Indiana University (lums@osl.iu.edu).
  5. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  6. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/libs/iostreams for documentation.
  8. //#include <cstdlib> // for multi-byte converson routines
  9. // Jonathan Turkanis:
  10. // - Replaced test for BOOST_NO_STD_WSTREAMBUF with test for
  11. // BOOST_IOSTREAMS_NO_WIDE_STREAMS;
  12. // - Derived from codecvt_helper instead of codecvt.
  13. #include <boost/config.hpp>
  14. #include <boost/iostreams/detail/config/wide_streams.hpp>
  15. #include <boost/numeric/conversion/cast.hpp>
  16. #ifdef BOOST_IOSTREAMS_NO_LOCALES
  17. # error "C++ locales not supported on this platform"
  18. #else
  19. #include <cassert>
  20. #include <cstddef>
  21. #include <boost/detail/workaround.hpp>
  22. #include "./utf8_codecvt_facet.hpp"
  23. #if BOOST_WORKAROUND(__BORLANDC__, <= 0x600)
  24. # pragma warn -sig // Conversion may lose significant digits
  25. # pragma warn -rng // Constant is out of range in comparison
  26. #endif
  27. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  28. // implementation for wchar_t
  29. // Translate incoming UTF-8 into UCS-4
  30. std::codecvt_base::result utf8_codecvt_facet_wchar_t::do_in(
  31. std::mbstate_t&,
  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_wchar_t::do_out(
  98. std::mbstate_t &,
  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. while (from != from_end && to != to_end) {
  112. #define BOOST_NULL // Prevent macro expansion
  113. // Check for invalid UCS-4 character
  114. if (*from > std::numeric_limits<wchar_t>::max BOOST_NULL ()) {
  115. from_next = from;
  116. to_next = to;
  117. return std::codecvt_base::error;
  118. }
  119. #undef BOOST_NULL
  120. int cont_octet_count = get_cont_octet_out_count(*from);
  121. // RG - comment this formula better
  122. int shift_exponent = (cont_octet_count) * 6;
  123. // Process the first character
  124. *to++ = octet1_modifier_table[cont_octet_count] +
  125. (unsigned char)(*from / (1 << shift_exponent));
  126. // Process the continuation characters
  127. // Invariants: At the start of the loop:
  128. // 1) 'i' continuing octets have been generated
  129. // 2) '*to' points to the next location to place an octet
  130. // 3) shift_exponent is 6 more than needed for the next octet
  131. int i = 0;
  132. while (i != cont_octet_count && to != to_end) {
  133. shift_exponent -= 6;
  134. *to++ = 0x80 + ((*from / (1 << shift_exponent)) % (1 << 6));
  135. ++i;
  136. }
  137. // If we filled up the out buffer before encoding the character
  138. if(to == to_end && i != cont_octet_count) {
  139. from_next = from;
  140. to_next = to - (i+1);
  141. return std::codecvt_base::partial;
  142. }
  143. ++from;
  144. }
  145. from_next = from;
  146. to_next = to;
  147. // Were we done or did we run out of destination space
  148. if(from == from_end) return std::codecvt_base::ok;
  149. else return std::codecvt_base::partial;
  150. }
  151. // How many char objects can I process to get <= max_limit
  152. // wchar_t objects?
  153. int utf8_codecvt_facet_wchar_t::do_length(
  154. BOOST_IOSTREAMS_CODECVT_CV_QUALIFIER std::mbstate_t &,
  155. const char * from,
  156. const char * from_end,
  157. std::size_t max_limit
  158. ) const throw()
  159. {
  160. // RG - this code is confusing! I need a better way to express it.
  161. // and test cases.
  162. // Invariants:
  163. // 1) last_octet_count has the size of the last measured character
  164. // 2) char_count holds the number of characters shown to fit
  165. // within the bounds so far (no greater than max_limit)
  166. // 3) from_next points to the octet 'last_octet_count' before the
  167. // last measured character.
  168. int last_octet_count=0;
  169. std::size_t char_count = 0;
  170. const char* from_next = from;
  171. // Use "<" because the buffer may represent incomplete characters
  172. while (from_next+last_octet_count <= from_end && char_count <= max_limit) {
  173. from_next += last_octet_count;
  174. last_octet_count = (get_octet_count(*from_next));
  175. ++char_count;
  176. }
  177. return boost::numeric_cast<int>(from_next - from_end);
  178. }
  179. unsigned int utf8_codecvt_facet_wchar_t::get_octet_count(
  180. unsigned char lead_octet
  181. ){
  182. // if the 0-bit (MSB) is 0, then 1 character
  183. if (lead_octet <= 0x7f) return 1;
  184. // Otherwise the count number of consecutive 1 bits starting at MSB
  185. assert(0xc0 <= lead_octet && lead_octet <= 0xfd);
  186. if (0xc0 <= lead_octet && lead_octet <= 0xdf) return 2;
  187. else if (0xe0 <= lead_octet && lead_octet <= 0xef) return 3;
  188. else if (0xf0 <= lead_octet && lead_octet <= 0xf7) return 4;
  189. else if (0xf8 <= lead_octet && lead_octet <= 0xfb) return 5;
  190. else return 6;
  191. }
  192. namespace {
  193. template<std::size_t s>
  194. int get_cont_octet_out_count_impl(wchar_t word){
  195. if (word < 0x80) {
  196. return 0;
  197. }
  198. if (word < 0x800) {
  199. return 1;
  200. }
  201. return 2;
  202. }
  203. // note the following code will generate on some platforms where
  204. // wchar_t is defined as UCS2. The warnings are superfluous as
  205. // the specialization is never instantitiated with such compilers.
  206. template<>
  207. int get_cont_octet_out_count_impl<4>(wchar_t word)
  208. {
  209. if (word < 0x80) {
  210. return 0;
  211. }
  212. if (word < 0x800) {
  213. return 1;
  214. }
  215. if (word < 0x10000) {
  216. return 2;
  217. }
  218. if (word < 0x200000) {
  219. return 3;
  220. }
  221. if (word < 0x4000000) {
  222. return 4;
  223. }
  224. return 5;
  225. }
  226. } // namespace anonymous
  227. // How many "continuing octets" will be needed for this word
  228. // == total octets - 1.
  229. int utf8_codecvt_facet_wchar_t::get_cont_octet_out_count(
  230. wchar_t word
  231. ) const {
  232. return get_cont_octet_out_count_impl<sizeof(wchar_t)>(word);
  233. }
  234. #if 0 // not used?
  235. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  236. // implementation for char
  237. std::codecvt_base::result utf8_codecvt_facet_char::do_in(
  238. std::mbstate_t & state,
  239. const char * from,
  240. const char * from_end,
  241. const char * & from_next,
  242. char * to,
  243. char * to_end,
  244. char * & to_next
  245. ) const
  246. {
  247. while(from_next < from_end){
  248. wchar_t w;
  249. wchar_t *wnext = & w;
  250. utf8_codecvt_facet_wchar_t::result ucs4_result;
  251. ucs4_result = base_class::do_in(
  252. state,
  253. from, from_end, from_next,
  254. wnext, wnext + 1, wnext
  255. );
  256. if(codecvt_base::ok != ucs4_result)
  257. return ucs4_result;
  258. // if the conversion succeeds.
  259. int length = std::wctomb(to_next, w);
  260. assert(-1 != length);
  261. to_next += length;
  262. }
  263. return codecvt_base::ok;
  264. }
  265. std::codecvt_base::result utf8_codecvt_facet_char::do_out(
  266. mbstate_t & state,
  267. const char * from,
  268. const char * from_end,
  269. const char * & from_next,
  270. char * to,
  271. char * to_end,
  272. char * & to_next
  273. ) const
  274. {
  275. while(from_next < from_end){
  276. wchar_t w;
  277. int result = std::mbtowc(&w, from_next, MB_LENGTH_MAX);
  278. assert(-1 != result);
  279. from_next += result;
  280. utf8_codecvt_facet_wchar_t::result ucs4_result;
  281. const wchar_t *wptr = & w;
  282. ucs4_result = base_class::do_out(
  283. state,
  284. wptr, wptr+1, wptr,
  285. to_next, to_end, to_next
  286. );
  287. if(codecvt_base::ok != ucs4_result)
  288. return ucs4_result;
  289. }
  290. return codecvt_base::ok;
  291. }
  292. // How many bytes objects can I process to get <= max_limit
  293. // char objects?
  294. int utf8_codecvt_facet_char::do_length(
  295. // it seems that the standard doesn't use const so these librarires
  296. // would be in error
  297. BOOST_IOSTREAMS_CODECVT_CV_QUALIFIER
  298. utf8_codecvt_facet_wchar_t::mbstate_t & initial_state,
  299. const char * from_next,
  300. const char * from_end,
  301. std::size_t max_limit
  302. ) const
  303. {
  304. int total_length = 0;
  305. const char *from = from_next;
  306. mbstate_t state = initial_state;
  307. while(from_next < from_end){
  308. wchar_t w;
  309. wchar_t *wnext = & w;
  310. utf8_codecvt_facet_wchar_t::result ucs4_result;
  311. ucs4_result = base_class::do_in(
  312. state,
  313. from_next, from_end, from_next,
  314. wnext, wnext + 1, wnext
  315. );
  316. if(codecvt_base::ok != ucs4_result)
  317. break;
  318. char carray[MB_LENGTH_MAX];
  319. std::size_t count = wctomb(carray, w);
  320. if(count > max_limit)
  321. break;
  322. max_limit -= count;
  323. total_length = from_next - from;
  324. }
  325. return total_length;
  326. }
  327. #endif
  328. #endif //BOOST_IOSTREAMS_NO_WIDE_STREAMS