2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
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)
8 #ifndef BOOST_LOCALE_ENCODING_HPP_INCLUDED
9 #define BOOST_LOCALE_ENCODING_HPP_INCLUDED
11 #include <boost/locale/config.hpp>
13 # pragma warning(push)
14 # pragma warning(disable : 4275 4251 4231 4660)
16 #include <boost/locale/info.hpp>
17 #include <boost/locale/encoding_errors.hpp>
18 #include <boost/locale/encoding_utf.hpp>
26 /// \brief Namespace that contains all functions related to character set conversion
30 /// \defgroup codepage Character conversion functions
35 /// convert string to UTF string from text in range [begin,end) encoded with \a charset according to policy \a how
37 template<typename CharType>
38 std::basic_string<CharType> to_utf(char const *begin,char const *end,std::string const &charset,method_type how=default_method);
41 /// convert UTF text in range [begin,end) to a text encoded with \a charset according to policy \a how
43 template<typename CharType>
44 std::string from_utf(CharType const *begin,CharType const *end,std::string const &charset,method_type how=default_method);
47 /// convert string to UTF string from text in range [begin,end) encoded according to locale \a loc according to policy \a how
49 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
51 template<typename CharType>
52 std::basic_string<CharType> to_utf(char const *begin,char const *end,std::locale const &loc,method_type how=default_method)
54 return to_utf<CharType>(begin,end,std::use_facet<info>(loc).encoding(),how);
58 /// convert UTF text in range [begin,end) to a text encoded according to locale \a loc according to policy \a how
60 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
62 template<typename CharType>
63 std::string from_utf(CharType const *begin,CharType const *end,std::locale const &loc,method_type how=default_method)
65 return from_utf(begin,end,std::use_facet<info>(loc).encoding(),how);
69 /// convert a string \a text encoded with \a charset to UTF string
72 template<typename CharType>
73 std::basic_string<CharType> to_utf(std::string const &text,std::string const &charset,method_type how=default_method)
75 return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),charset,how);
79 /// Convert a \a text from \a charset to UTF string
81 template<typename CharType>
82 std::string from_utf(std::basic_string<CharType> const &text,std::string const &charset,method_type how=default_method)
84 return from_utf(text.c_str(),text.c_str()+text.size(),charset,how);
88 /// Convert a \a text from \a charset to UTF string
90 template<typename CharType>
91 std::basic_string<CharType> to_utf(char const *text,std::string const &charset,method_type how=default_method)
93 char const *text_end = text;
96 return to_utf<CharType>(text,text_end,charset,how);
100 /// Convert a \a text from UTF to \a charset
102 template<typename CharType>
103 std::string from_utf(CharType const *text,std::string const &charset,method_type how=default_method)
105 CharType const *text_end = text;
108 return from_utf(text,text_end,charset,how);
112 /// Convert a \a text in locale encoding given by \a loc to UTF
114 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
116 template<typename CharType>
117 std::basic_string<CharType> to_utf(std::string const &text,std::locale const &loc,method_type how=default_method)
119 return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),loc,how);
123 /// Convert a \a text in UTF to locale encoding given by \a loc
125 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
127 template<typename CharType>
128 std::string from_utf(std::basic_string<CharType> const &text,std::locale const &loc,method_type how=default_method)
130 return from_utf(text.c_str(),text.c_str()+text.size(),loc,how);
134 /// Convert a \a text in locale encoding given by \a loc to UTF
136 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
138 template<typename CharType>
139 std::basic_string<CharType> to_utf(char const *text,std::locale const &loc,method_type how=default_method)
141 char const *text_end = text;
144 return to_utf<CharType>(text,text_end,loc,how);
148 /// Convert a \a text in UTF to locale encoding given by \a loc
150 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
152 template<typename CharType>
153 std::string from_utf(CharType const *text,std::locale const &loc,method_type how=default_method)
155 CharType const *text_end = text;
158 return from_utf(text,text_end,loc,how);
163 /// Convert a text in range [begin,end) to \a to_encoding from \a from_encoding
167 std::string between(char const *begin,
169 std::string const &to_encoding,
170 std::string const &from_encoding,
171 method_type how=default_method);
174 /// Convert a \a text to \a to_encoding from \a from_encoding
178 std::string between(char const *text,
179 std::string const &to_encoding,
180 std::string const &from_encoding,
181 method_type how=default_method)
183 char const *end=text;
186 return boost::locale::conv::between(text,end,to_encoding,from_encoding,how);
190 /// Convert a \a text to \a to_encoding from \a from_encoding
193 std::string between(std::string const &text,
194 std::string const &to_encoding,
195 std::string const &from_encoding,
196 method_type how=default_method)
198 return boost::locale::conv::between(text.c_str(),text.c_str()+text.size(),to_encoding,from_encoding,how);
204 BOOST_LOCALE_DECL std::basic_string<char> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
207 BOOST_LOCALE_DECL std::string from_utf(char const *begin,char const *end,std::string const &charset,method_type how);
210 BOOST_LOCALE_DECL std::basic_string<wchar_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
213 BOOST_LOCALE_DECL std::string from_utf(wchar_t const *begin,wchar_t const *end,std::string const &charset,method_type how);
215 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
217 BOOST_LOCALE_DECL std::basic_string<char16_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
220 BOOST_LOCALE_DECL std::string from_utf(char16_t const *begin,char16_t const *end,std::string const &charset,method_type how);
223 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
225 BOOST_LOCALE_DECL std::basic_string<char32_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
228 BOOST_LOCALE_DECL std::string from_utf(char32_t const *begin,char32_t const *end,std::string const &charset,method_type how);
247 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4