encoding.hpp 8.5 KB

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