utf8_codecvt_facet.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (c) 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
  2. // Andrew Lumsdaine, Indiana University (lums@osl.iu.edu).
  3. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  4. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_UTF8_CODECVT_FACET_HPP
  6. #define BOOST_UTF8_CODECVT_FACET_HPP
  7. // MS compatible compilers support #pragma once
  8. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  9. # pragma once
  10. #endif
  11. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  12. // utf8_codecvt_facet.hpp
  13. // This header defines class utf8_codecvt_facet, derived fro
  14. // std::codecvt<wchar_t, char>, which can be used to convert utf8 data in
  15. // files into wchar_t strings in the application.
  16. //
  17. // The header is NOT STANDALONE, and is not to be included by the USER.
  18. // There are at least two libraries which want to use this functionality, and
  19. // we want to avoid code duplication. It would be possible to create utf8
  20. // library, but:
  21. // - this requires review process first
  22. // - in the case, when linking the a library which uses utf8
  23. // (say 'program_options'), user should also link to the utf8 library.
  24. // This seems inconvenient, and asking a user to link to an unrevieved
  25. // library is strange.
  26. // Until the above points are fixed, a library which wants to use utf8 must:
  27. // - include this header from one of it's headers or sources
  28. // - include the corresponding .cpp file from one of the sources
  29. // - before including either file, the library must define
  30. // - BOOST_UTF8_BEGIN_NAMESPACE to the namespace declaration that must be used
  31. // - BOOST_UTF8_END_NAMESPACE to the code to close the previous namespace
  32. // - declaration.
  33. // - BOOST_UTF8_DECL -- to the code which must be used for all 'exportable'
  34. // symbols.
  35. //
  36. // For example, program_options library might contain:
  37. // #define BOOST_UTF8_BEGIN_NAMESPACE <backslash character>
  38. // namespace boost { namespace program_options {
  39. // #define BOOST_UTF8_END_NAMESPACE }}
  40. // #define BOOST_UTF8_DECL BOOST_PROGRAM_OPTIONS_DECL
  41. // #include "../../detail/utf8/utf8_codecvt.cpp"
  42. //
  43. // Essentially, each library will have its own copy of utf8 code, in
  44. // different namespaces.
  45. // Note:(Robert Ramey). I have made the following alterations in the original
  46. // code.
  47. // a) Rendered utf8_codecvt<wchar_t, char> with using templates
  48. // b) Move longer functions outside class definition to prevent inlining
  49. // and make code smaller
  50. // c) added on a derived class to permit translation to/from current
  51. // locale to utf8
  52. // See http://www.boost.org for updates, documentation, and revision history.
  53. // archives stored as text - note these ar templated on the basic
  54. // stream templates to accommodate wide (and other?) kind of characters
  55. //
  56. // note the fact that on libraries without wide characters, ostream is
  57. // is not a specialization of basic_ostream which in fact is not defined
  58. // in such cases. So we can't use basic_ostream<OStream::char_type> but rather
  59. // use two template parameters
  60. //
  61. // utf8_codecvt_facet
  62. // This is an implementation of a std::codecvt facet for translating
  63. // from UTF-8 externally to UCS-4. Note that this is not tied to
  64. // any specific types in order to allow customization on platforms
  65. // where wchar_t is not big enough.
  66. //
  67. // NOTES: The current implementation jumps through some unpleasant hoops in
  68. // order to deal with signed character types. As a std::codecvt_base::result,
  69. // it is necessary for the ExternType to be convertible to unsigned char.
  70. // I chose not to tie the extern_type explicitly to char. But if any combination
  71. // of types other than <wchar_t,char_t> is used, then std::codecvt must be
  72. // specialized on those types for this to work.
  73. #include <locale>
  74. #include <cwchar> // for mbstate_t
  75. #include <cstddef> // for std::size_t
  76. #include <boost/config.hpp>
  77. #include <boost/detail/workaround.hpp>
  78. #if defined(BOOST_NO_STDC_NAMESPACE)
  79. namespace std {
  80. using ::mbstate_t;
  81. using ::size_t;
  82. }
  83. #endif
  84. #if !defined(__MSL_CPP__) && !defined(__LIBCOMO__)
  85. #define BOOST_CODECVT_DO_LENGTH_CONST const
  86. #else
  87. #define BOOST_CODECVT_DO_LENGTH_CONST
  88. #endif
  89. // maximum lenght of a multibyte string
  90. #define MB_LENGTH_MAX 8
  91. BOOST_UTF8_BEGIN_NAMESPACE
  92. struct BOOST_UTF8_DECL utf8_codecvt_facet :
  93. public std::codecvt<wchar_t, char, std::mbstate_t>
  94. {
  95. public:
  96. explicit utf8_codecvt_facet(std::size_t no_locale_manage=0)
  97. : std::codecvt<wchar_t, char, std::mbstate_t>(no_locale_manage)
  98. {}
  99. protected:
  100. virtual std::codecvt_base::result do_in(
  101. std::mbstate_t& state,
  102. const char * from,
  103. const char * from_end,
  104. const char * & from_next,
  105. wchar_t * to,
  106. wchar_t * to_end,
  107. wchar_t*& to_next
  108. ) const;
  109. virtual std::codecvt_base::result do_out(
  110. std::mbstate_t & state, const wchar_t * from,
  111. const wchar_t * from_end, const wchar_t* & from_next,
  112. char * to, char * to_end, char * & to_next
  113. ) const;
  114. bool invalid_continuing_octet(unsigned char octet_1) const {
  115. return (octet_1 < 0x80|| 0xbf< octet_1);
  116. }
  117. bool invalid_leading_octet(unsigned char octet_1) const {
  118. return (0x7f < octet_1 && octet_1 < 0xc0) ||
  119. (octet_1 > 0xfd);
  120. }
  121. // continuing octets = octets except for the leading octet
  122. static unsigned int get_cont_octet_count(unsigned char lead_octet) {
  123. return get_octet_count(lead_octet) - 1;
  124. }
  125. static unsigned int get_octet_count(unsigned char lead_octet);
  126. // How many "continuing octets" will be needed for this word
  127. // == total octets - 1.
  128. int get_cont_octet_out_count(wchar_t word) const ;
  129. virtual bool do_always_noconv() const throw() { return false; }
  130. // UTF-8 isn't really stateful since we rewind on partial conversions
  131. virtual std::codecvt_base::result do_unshift(
  132. std::mbstate_t&,
  133. char * from,
  134. char * /*to*/,
  135. char * & next
  136. ) const
  137. {
  138. next = from;
  139. return ok;
  140. }
  141. virtual int do_encoding() const throw() {
  142. const int variable_byte_external_encoding=0;
  143. return variable_byte_external_encoding;
  144. }
  145. // How many char objects can I process to get <= max_limit
  146. // wchar_t objects?
  147. virtual int do_length(
  148. BOOST_CODECVT_DO_LENGTH_CONST std::mbstate_t &,
  149. const char * from,
  150. const char * from_end,
  151. std::size_t max_limit
  152. #if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
  153. ) const throw();
  154. #else
  155. ) const;
  156. #endif
  157. // Largest possible value do_length(state,from,from_end,1) could return.
  158. virtual int do_max_length() const throw () {
  159. return 6; // largest UTF-8 encoding of a UCS-4 character
  160. }
  161. };
  162. BOOST_UTF8_END_NAMESPACE
  163. #endif // BOOST_UTF8_CODECVT_FACET_HPP