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/detail/encoding.hpp>
12#include <boost/locale/encoding_errors.hpp>
13#include <boost/locale/encoding_utf.hpp>
14#include <boost/locale/info.hpp>
15#include <boost/locale/util/string.hpp>
16#include <memory>
17
18#ifdef BOOST_MSVC
19# pragma warning(push)
20# pragma warning(disable : 4275 4251 4231 4660)
21#endif
22
23namespace boost { namespace locale {
24
26 namespace conv {
27
31
37 template<typename CharType>
38 BOOST_LOCALE_DECL std::basic_string<CharType>
39 to_utf(const char* begin, const char* end, const std::string& charset, method_type how = default_method);
40
46 template<typename CharType>
47 BOOST_LOCALE_DECL std::string from_utf(const CharType* begin,
48 const CharType* end,
49 const std::string& charset,
51
57 template<typename CharType>
58 std::basic_string<CharType>
59 to_utf(const std::string& text, const std::string& charset, method_type how = default_method)
60 {
61 return to_utf<CharType>(text.c_str(), text.c_str() + text.size(), charset, how);
62 }
63
69 template<typename CharType>
70 std::basic_string<CharType>
71 to_utf(const char* text, const std::string& charset, method_type how = default_method)
72 {
73 return to_utf<CharType>(text, util::str_end(text), charset, how);
74 }
75
83 template<typename CharType>
84 std::basic_string<CharType>
85 to_utf(const char* begin, const char* end, const std::locale& loc, method_type how = default_method)
86 {
87 return to_utf<CharType>(begin, end, std::use_facet<info>(loc).encoding(), how);
88 }
89
96 template<typename CharType>
97 std::basic_string<CharType>
98 to_utf(const std::string& text, const std::locale& loc, method_type how = default_method)
99 {
100 return to_utf<CharType>(text, std::use_facet<info>(loc).encoding(), how);
101 }
102
109 template<typename CharType>
110 std::basic_string<CharType> to_utf(const char* text, const std::locale& loc, method_type how = default_method)
111 {
112 return to_utf<CharType>(text, std::use_facet<info>(loc).encoding(), how);
113 }
114
120 template<typename CharType>
121 std::string
122 from_utf(const std::basic_string<CharType>& text, const std::string& charset, method_type how = default_method)
123 {
124 return from_utf(text.c_str(), text.c_str() + text.size(), charset, how);
125 }
126
132 template<typename CharType>
133 std::string from_utf(const CharType* text, const std::string& charset, method_type how = default_method)
134 {
135 return from_utf(text, util::str_end(text), charset, how);
136 }
137
144 template<typename CharType>
145 std::string
146 from_utf(const CharType* begin, const CharType* end, const std::locale& loc, method_type how = default_method)
147 {
148 return from_utf(begin, end, std::use_facet<info>(loc).encoding(), how);
149 }
150
157 template<typename CharType>
158 std::string
159 from_utf(const std::basic_string<CharType>& text, const std::locale& loc, method_type how = default_method)
160 {
161 return from_utf(text, std::use_facet<info>(loc).encoding(), how);
162 }
163
170 template<typename CharType>
171 std::string from_utf(const CharType* text, const std::locale& loc, method_type how = default_method)
172 {
173 return from_utf(text, std::use_facet<info>(loc).encoding(), how);
174 }
175
182 BOOST_LOCALE_DECL
183 std::string between(const char* begin,
184 const char* end,
185 const std::string& to_encoding,
186 const std::string& from_encoding,
188
195 inline std::string between(const char* text,
196 const std::string& to_encoding,
197 const std::string& from_encoding,
199 {
200 return between(text, util::str_end(text), to_encoding, from_encoding, how);
201 }
202
209 inline std::string between(const std::string& text,
210 const std::string& to_encoding,
211 const std::string& from_encoding,
213 {
214 return between(text.c_str(), text.c_str() + text.size(), to_encoding, from_encoding, how);
215 }
216
218
220 template<typename CharType>
222 std::unique_ptr<detail::utf_encoder<CharType>> impl_;
223
224 public:
225 using char_type = CharType;
226 using string_type = std::basic_string<CharType>;
227
232 utf_encoder(const std::string& charset, method_type how = default_method) :
233 impl_(detail::make_utf_encoder<CharType>(charset, how))
234 {}
235
239 string_type convert(const char* begin, const char* end) const { return impl_->convert(begin, end); }
243 string_type convert(const boost::string_view& text) const { return impl_->convert(text); }
247 string_type operator()(const boost::string_view& text) const { return convert(text); }
248 };
249
251 template<typename CharType>
253 std::unique_ptr<detail::utf_decoder<CharType>> impl_;
254
255 public:
256 using char_type = CharType;
257 using stringview_type = boost::basic_string_view<CharType>;
258
263 utf_decoder(const std::string& charset, method_type how = default_method) :
264 impl_(detail::make_utf_decoder<CharType>(charset, how))
265 {}
266
270 std::string convert(const CharType* begin, const CharType* end) const { return impl_->convert(begin, end); }
274 std::string convert(const stringview_type& text) const { return impl_->convert(text); }
278 std::string operator()(const stringview_type& text) const { return convert(text); }
279 };
280
282 std::unique_ptr<detail::narrow_converter> impl_;
283
284 public:
288 narrow_converter(const std::string& src_encoding,
289 const std::string& target_encoding,
291 impl_(detail::make_narrow_converter(src_encoding, target_encoding, how))
292 {}
293
297 std::string convert(const char* begin, const char* end) const { return impl_->convert(begin, end); }
301 std::string convert(const boost::string_view& text) const { return impl_->convert(text); }
305 std::string operator()(const boost::string_view& text) const { return convert(text); }
306 };
307 } // namespace conv
308}} // namespace boost::locale
309
310#ifdef BOOST_MSVC
311# pragma warning(pop)
312#endif
313
314#endif
Definition: encoding.hpp:281
std::string convert(const boost::string_view &text) const
Definition: encoding.hpp:301
narrow_converter(const std::string &src_encoding, const std::string &target_encoding, method_type how=default_method)
Definition: encoding.hpp:288
std::string convert(const char *begin, const char *end) const
Definition: encoding.hpp:297
std::string operator()(const boost::string_view &text) const
Definition: encoding.hpp:305
Converter class to decode an UTF string and encode it using a local encoding.
Definition: encoding.hpp:252
utf_decoder(const std::string &charset, method_type how=default_method)
Definition: encoding.hpp:263
std::string convert(const CharType *begin, const CharType *end) const
Definition: encoding.hpp:270
std::string operator()(const stringview_type &text) const
Definition: encoding.hpp:278
std::string convert(const stringview_type &text) const
Definition: encoding.hpp:274
Converter class to decode a narrow string using a local encoding and encode it with UTF.
Definition: encoding.hpp:221
string_type convert(const char *begin, const char *end) const
Definition: encoding.hpp:239
utf_encoder(const std::string &charset, method_type how=default_method)
Definition: encoding.hpp:232
string_type operator()(const boost::string_view &text) const
Definition: encoding.hpp:247
string_type convert(const boost::string_view &text) const
Definition: encoding.hpp:243
std::basic_string< CharType > to_utf(const char *begin, const char *end, const std::string &charset, method_type how=default_method)
std::string from_utf(const CharType *begin, const CharType *end, const std::string &charset, method_type how=default_method)
std::string between(const char *begin, const char *end, const std::string &to_encoding, const std::string &from_encoding, method_type how=default_method)
method_type
enum that defines conversion policy
Definition: encoding_errors.hpp:41
@ default_method
Default method - skip.
Definition: encoding_errors.hpp:44
Char * str_end(Char *str)
Return the end of a C-string, i.e. the pointer to the trailing NULL byte.
Definition: string.hpp:16
@ convert
Generate conversion facets.