encoding.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_LOCALE_ENCODING_HPP_INCLUDED
  9. #define BOOST_LOCALE_ENCODING_HPP_INCLUDED
  10. #include <boost/locale/config.hpp>
  11. #ifdef BOOST_MSVC
  12. # pragma warning(push)
  13. # pragma warning(disable : 4275 4251 4231 4660)
  14. #endif
  15. #include <boost/locale/info.hpp>
  16. #include <boost/locale/encoding_errors.hpp>
  17. #include <boost/locale/encoding_utf.hpp>
  18. namespace boost {
  19. namespace locale {
  20. ///
  21. /// \brief Namespace that contains all functions related to character set conversion
  22. ///
  23. namespace conv {
  24. ///
  25. /// \defgroup codepage Character conversion functions
  26. ///
  27. /// @{
  28. ///
  29. /// convert string to UTF string from text in range [begin,end) encoded with \a charset according to policy \a how
  30. ///
  31. template<typename CharType>
  32. std::basic_string<CharType> to_utf(char const *begin,char const *end,std::string const &charset,method_type how=default_method);
  33. ///
  34. /// convert UTF text in range [begin,end) to a text encoded with \a charset according to policy \a how
  35. ///
  36. template<typename CharType>
  37. std::string from_utf(CharType const *begin,CharType const *end,std::string const &charset,method_type how=default_method);
  38. ///
  39. /// convert string to UTF string from text in range [begin,end) encoded according to locale \a loc according to policy \a how
  40. ///
  41. /// \note throws std::bad_cast if the loc does not have \ref info facet installed
  42. ///
  43. template<typename CharType>
  44. std::basic_string<CharType> to_utf(char const *begin,char const *end,std::locale const &loc,method_type how=default_method)
  45. {
  46. return to_utf<CharType>(begin,end,std::use_facet<info>(loc).encoding(),how);
  47. }
  48. ///
  49. /// convert UTF text in range [begin,end) to a text encoded according to locale \a loc according to policy \a how
  50. ///
  51. /// \note throws std::bad_cast if the loc does not have \ref info facet installed
  52. ///
  53. template<typename CharType>
  54. std::string from_utf(CharType const *begin,CharType const *end,std::locale const &loc,method_type how=default_method)
  55. {
  56. return from_utf(begin,end,std::use_facet<info>(loc).encoding(),how);
  57. }
  58. ///
  59. /// convert a string \a text encoded with \a charset to UTF string
  60. ///
  61. template<typename CharType>
  62. std::basic_string<CharType> to_utf(std::string const &text,std::string const &charset,method_type how=default_method)
  63. {
  64. return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),charset,how);
  65. }
  66. ///
  67. /// Convert a \a text from \a charset to UTF string
  68. ///
  69. template<typename CharType>
  70. std::string from_utf(std::basic_string<CharType> const &text,std::string const &charset,method_type how=default_method)
  71. {
  72. return from_utf(text.c_str(),text.c_str()+text.size(),charset,how);
  73. }
  74. ///
  75. /// Convert a \a text from \a charset to UTF string
  76. ///
  77. template<typename CharType>
  78. std::basic_string<CharType> to_utf(char const *text,std::string const &charset,method_type how=default_method)
  79. {
  80. char const *text_end = text;
  81. while(*text_end)
  82. text_end++;
  83. return to_utf<CharType>(text,text_end,charset,how);
  84. }
  85. ///
  86. /// Convert a \a text from UTF to \a charset
  87. ///
  88. template<typename CharType>
  89. std::string from_utf(CharType const *text,std::string const &charset,method_type how=default_method)
  90. {
  91. CharType const *text_end = text;
  92. while(*text_end)
  93. text_end++;
  94. return from_utf(text,text_end,charset,how);
  95. }
  96. ///
  97. /// Convert a \a text in locale encoding given by \a loc to UTF
  98. ///
  99. /// \note throws std::bad_cast if the loc does not have \ref info facet installed
  100. ///
  101. template<typename CharType>
  102. std::basic_string<CharType> to_utf(std::string const &text,std::locale const &loc,method_type how=default_method)
  103. {
  104. return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),loc,how);
  105. }
  106. ///
  107. /// Convert a \a text in UTF to locale encoding given by \a loc
  108. ///
  109. /// \note throws std::bad_cast if the loc does not have \ref info facet installed
  110. ///
  111. template<typename CharType>
  112. std::string from_utf(std::basic_string<CharType> const &text,std::locale const &loc,method_type how=default_method)
  113. {
  114. return from_utf(text.c_str(),text.c_str()+text.size(),loc,how);
  115. }
  116. ///
  117. /// Convert a \a text in locale encoding given by \a loc to UTF
  118. ///
  119. /// \note throws std::bad_cast if the loc does not have \ref info facet installed
  120. ///
  121. template<typename CharType>
  122. std::basic_string<CharType> to_utf(char const *text,std::locale const &loc,method_type how=default_method)
  123. {
  124. char const *text_end = text;
  125. while(*text_end)
  126. text_end++;
  127. return to_utf<CharType>(text,text_end,loc,how);
  128. }
  129. ///
  130. /// Convert a \a text in UTF to locale encoding given by \a loc
  131. ///
  132. /// \note throws std::bad_cast if the loc does not have \ref info facet installed
  133. ///
  134. template<typename CharType>
  135. std::string from_utf(CharType const *text,std::locale const &loc,method_type how=default_method)
  136. {
  137. CharType const *text_end = text;
  138. while(*text_end)
  139. text_end++;
  140. return from_utf(text,text_end,loc,how);
  141. }
  142. ///
  143. /// Convert a text in range [begin,end) to \a to_encoding from \a from_encoding
  144. ///
  145. BOOST_LOCALE_DECL
  146. std::string between(char const *begin,
  147. char const *end,
  148. std::string const &to_encoding,
  149. std::string const &from_encoding,
  150. method_type how=default_method);
  151. ///
  152. /// Convert a \a text to \a to_encoding from \a from_encoding
  153. ///
  154. inline
  155. std::string between(char const *text,
  156. std::string const &to_encoding,
  157. std::string const &from_encoding,
  158. method_type how=default_method)
  159. {
  160. char const *end=text;
  161. while(*end)
  162. end++;
  163. return boost::locale::conv::between(text,end,to_encoding,from_encoding,how);
  164. }
  165. ///
  166. /// Convert a \a text to \a to_encoding from \a from_encoding
  167. ///
  168. inline
  169. std::string between(std::string const &text,
  170. std::string const &to_encoding,
  171. std::string const &from_encoding,
  172. method_type how=default_method)
  173. {
  174. return boost::locale::conv::between(text.c_str(),text.c_str()+text.size(),to_encoding,from_encoding,how);
  175. }
  176. /// \cond INTERNAL
  177. template<>
  178. BOOST_LOCALE_DECL std::basic_string<char> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
  179. template<>
  180. BOOST_LOCALE_DECL std::string from_utf(char const *begin,char const *end,std::string const &charset,method_type how);
  181. template<>
  182. BOOST_LOCALE_DECL std::basic_string<wchar_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
  183. template<>
  184. BOOST_LOCALE_DECL std::string from_utf(wchar_t const *begin,wchar_t const *end,std::string const &charset,method_type how);
  185. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  186. template<>
  187. BOOST_LOCALE_DECL std::basic_string<char16_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
  188. template<>
  189. BOOST_LOCALE_DECL std::string from_utf(char16_t const *begin,char16_t const *end,std::string const &charset,method_type how);
  190. #endif
  191. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  192. template<>
  193. BOOST_LOCALE_DECL std::basic_string<char32_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
  194. template<>
  195. BOOST_LOCALE_DECL std::string from_utf(char32_t const *begin,char32_t const *end,std::string const &charset,method_type how);
  196. #endif
  197. /// \endcond
  198. /// @}
  199. } // conv
  200. } // locale
  201. } // boost
  202. #ifdef BOOST_MSVC
  203. #pragma warning(pop)
  204. #endif
  205. #endif
  206. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4