Boost.Locale
encoding.hpp
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 
7 #ifndef BOOST_LOCALE_ENCODING_HPP_INCLUDED
8 #define BOOST_LOCALE_ENCODING_HPP_INCLUDED
9 
10 #include <boost/locale/config.hpp>
11 #include <boost/locale/encoding_errors.hpp>
12 #include <boost/locale/encoding_utf.hpp>
13 #include <boost/locale/info.hpp>
14 #include <boost/locale/util/string.hpp>
15 
16 #ifdef BOOST_MSVC
17 # pragma warning(push)
18 # pragma warning(disable : 4275 4251 4231 4660)
19 #endif
20 
21 namespace boost { namespace locale {
22 
24  namespace conv {
28 
30  template<typename CharType>
31  std::basic_string<CharType>
32  to_utf(const char* begin, const char* end, const std::string& charset, method_type how = default_method);
33 
35  template<typename CharType>
36  std::string from_utf(const CharType* begin,
37  const CharType* end,
38  const std::string& charset,
40 
45  template<typename CharType>
46  std::basic_string<CharType>
47  to_utf(const char* begin, const char* end, const std::locale& loc, method_type how = default_method)
48  {
49  return to_utf<CharType>(begin, end, std::use_facet<info>(loc).encoding(), how);
50  }
51 
56  template<typename CharType>
57  std::string
58  from_utf(const CharType* begin, const CharType* end, const std::locale& loc, method_type how = default_method)
59  {
60  return from_utf(begin, end, std::use_facet<info>(loc).encoding(), how);
61  }
62 
64  template<typename CharType>
65  std::basic_string<CharType>
66  to_utf(const std::string& text, const std::string& charset, method_type how = default_method)
67  {
68  return to_utf<CharType>(text.c_str(), text.c_str() + text.size(), charset, how);
69  }
70 
72  template<typename CharType>
73  std::string
74  from_utf(const std::basic_string<CharType>& text, const std::string& charset, method_type how = default_method)
75  {
76  return from_utf(text.c_str(), text.c_str() + text.size(), charset, how);
77  }
78 
80  template<typename CharType>
81  std::basic_string<CharType>
82  to_utf(const char* text, const std::string& charset, method_type how = default_method)
83  {
84  return to_utf<CharType>(text, util::str_end(text), charset, how);
85  }
86 
88  template<typename CharType>
89  std::string from_utf(const CharType* text, const std::string& charset, method_type how = default_method)
90  {
91  return from_utf(text, util::str_end(text), charset, how);
92  }
93 
97  template<typename CharType>
98  std::basic_string<CharType>
99  to_utf(const std::string& text, const std::locale& loc, method_type how = default_method)
100  {
101  return to_utf<CharType>(text.c_str(), text.c_str() + text.size(), loc, how);
102  }
103 
107  template<typename CharType>
108  std::string
109  from_utf(const std::basic_string<CharType>& text, const std::locale& loc, method_type how = default_method)
110  {
111  return from_utf(text.c_str(), text.c_str() + text.size(), loc, how);
112  }
113 
117  template<typename CharType>
118  std::basic_string<CharType> to_utf(const char* text, const std::locale& loc, method_type how = default_method)
119  {
120  return to_utf<CharType>(text, util::str_end(text), loc, how);
121  }
122 
126  template<typename CharType>
127  std::string from_utf(const CharType* text, const std::locale& loc, method_type how = default_method)
128  {
129  return from_utf(text, util::str_end(text), loc, how);
130  }
131 
133  BOOST_LOCALE_DECL
134  std::string between(const char* begin,
135  const char* end,
136  const std::string& to_encoding,
137  const std::string& from_encoding,
139 
141  inline std::string between(const char* text,
142  const std::string& to_encoding,
143  const std::string& from_encoding,
145  {
146  return boost::locale::conv::between(text, util::str_end(text), to_encoding, from_encoding, how);
147  }
148 
150  inline std::string between(const std::string& text,
151  const std::string& to_encoding,
152  const std::string& from_encoding,
154  {
155  return boost::locale::conv::between(text.c_str(),
156  text.c_str() + text.size(),
157  to_encoding,
158  from_encoding,
159  how);
160  }
161 
163 
164  template<>
165  BOOST_LOCALE_DECL std::basic_string<char>
166  to_utf(const char* begin, const char* end, const std::string& charset, method_type how);
167 
168  template<>
169  BOOST_LOCALE_DECL std::string
170  from_utf(const char* begin, const char* end, const std::string& charset, method_type how);
171 
172  template<>
173  BOOST_LOCALE_DECL std::basic_string<wchar_t>
174  to_utf(const char* begin, const char* end, const std::string& charset, method_type how);
175 
176  template<>
177  BOOST_LOCALE_DECL std::string
178  from_utf(const wchar_t* begin, const wchar_t* end, const std::string& charset, method_type how);
179 
180 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
181  template<>
182  BOOST_LOCALE_DECL std::basic_string<char16_t>
183  to_utf(const char* begin, const char* end, const std::string& charset, method_type how);
184 
185  template<>
186  BOOST_LOCALE_DECL std::string
187  from_utf(const char16_t* begin, const char16_t* end, const std::string& charset, method_type how);
188 #endif
189 
190 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
191  template<>
192  BOOST_LOCALE_DECL std::basic_string<char32_t>
193  to_utf(const char* begin, const char* end, const std::string& charset, method_type how);
194 
195  template<>
196  BOOST_LOCALE_DECL std::string
197  from_utf(const char32_t* begin, const char32_t* end, const std::string& charset, method_type how);
198 #endif
199 
201 
203 
204  } // namespace conv
205 
206 }} // namespace boost::locale
207 
208 #ifdef BOOST_MSVC
209 # pragma warning(pop)
210 #endif
211 
212 #endif
Default method - skip.
Definition: encoding_errors.hpp:44
std::string between(const char *begin, const char *end, const std::string &to_encoding, const std::string &from_encoding, method_type how=default_method)
Convert a text in range [begin,end) to to_encoding from from_encoding.
Char * str_end(Char *str)
Return the end of a C-string, i.e. the pointer to the trailing NULL byte.
Definition: string.hpp:15
std::string from_utf(const CharType *begin, const CharType *end, const std::string &charset, method_type how=default_method)
convert UTF text in range [begin,end) to a text encoded with charset according to policy how
std::basic_string< CharType > to_utf(const char *begin, const char *end, const std::string &charset, method_type how=default_method)
convert text in range [begin,end) encoded with charset to UTF string according to policy how
method_type
enum that defines conversion policy
Definition: encoding_errors.hpp:41