Boost.Locale
encoding.hpp
1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3// Copyright (c) 2025 Alexander Grund
4//
5// Distributed under the Boost Software License, Version 1.0.
6// https://www.boost.org/LICENSE_1_0.txt
7
8#ifndef BOOST_LOCALE_ENCODING_HPP_INCLUDED
9#define BOOST_LOCALE_ENCODING_HPP_INCLUDED
10
11#include <boost/locale/config.hpp>
12#include <boost/locale/detail/encoding.hpp>
13#include <boost/locale/encoding_errors.hpp>
14#include <boost/locale/encoding_utf.hpp>
15#include <boost/locale/info.hpp>
16#include <boost/locale/util/string.hpp>
17#include <memory>
18
19#ifdef BOOST_MSVC
20# pragma warning(push)
21# pragma warning(disable : 4275 4251 4231 4660)
22#endif
23
24namespace boost { namespace locale {
25
27 namespace conv {
28
32
38 template<typename CharType>
39 BOOST_LOCALE_DECL std::basic_string<CharType>
40 to_utf(const char* begin, const char* end, const std::string& charset, method_type how = default_method);
41
47 template<typename CharType>
48 BOOST_LOCALE_DECL std::string from_utf(const CharType* begin,
49 const CharType* end,
50 const std::string& charset,
52
58 template<typename CharType>
59 std::basic_string<CharType>
60 to_utf(const std::string& text, const std::string& charset, method_type how = default_method)
61 {
62 return to_utf<CharType>(text.c_str(), text.c_str() + text.size(), charset, how);
63 }
64
70 template<typename CharType>
71 std::basic_string<CharType>
72 to_utf(const char* text, const std::string& charset, method_type how = default_method)
73 {
74 return to_utf<CharType>(text, util::str_end(text), charset, how);
75 }
76
84 template<typename CharType>
85 std::basic_string<CharType>
86 to_utf(const char* begin, const char* end, const std::locale& loc, method_type how = default_method)
87 {
88 return to_utf<CharType>(begin, end, std::use_facet<info>(loc).encoding(), how);
89 }
90
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, std::use_facet<info>(loc).encoding(), how);
102 }
103
110 template<typename CharType>
111 std::basic_string<CharType> to_utf(const char* text, const std::locale& loc, method_type how = default_method)
112 {
113 return to_utf<CharType>(text, std::use_facet<info>(loc).encoding(), how);
114 }
115
121 template<typename CharType>
122 std::string
123 from_utf(const std::basic_string<CharType>& text, const std::string& charset, method_type how = default_method)
124 {
125 return from_utf(text.c_str(), text.c_str() + text.size(), charset, how);
126 }
127
133 template<typename CharType>
134 std::string from_utf(const CharType* text, const std::string& charset, method_type how = default_method)
135 {
136 return from_utf(text, util::str_end(text), charset, how);
137 }
138
145 template<typename CharType>
146 std::string
147 from_utf(const CharType* begin, const CharType* end, const std::locale& loc, method_type how = default_method)
148 {
149 return from_utf(begin, end, std::use_facet<info>(loc).encoding(), how);
150 }
151
158 template<typename CharType>
159 std::string
160 from_utf(const std::basic_string<CharType>& text, const std::locale& loc, method_type how = default_method)
161 {
162 return from_utf(text, std::use_facet<info>(loc).encoding(), how);
163 }
164
171 template<typename CharType>
172 std::string from_utf(const CharType* text, const std::locale& loc, method_type how = default_method)
173 {
174 return from_utf(text, std::use_facet<info>(loc).encoding(), how);
175 }
176
183 BOOST_LOCALE_DECL
184 std::string between(const char* begin,
185 const char* end,
186 const std::string& to_encoding,
187 const std::string& from_encoding,
189
196 inline std::string between(const char* text,
197 const std::string& to_encoding,
198 const std::string& from_encoding,
200 {
201 return between(text, util::str_end(text), to_encoding, from_encoding, how);
202 }
203
210 inline std::string between(const std::string& text,
211 const std::string& to_encoding,
212 const std::string& from_encoding,
214 {
215 return between(text.c_str(), text.c_str() + text.size(), to_encoding, from_encoding, how);
216 }
217
219
221 template<typename CharType>
223 std::unique_ptr<detail::utf_encoder<CharType>> impl_;
224
225 public:
226 using char_type = CharType;
227 using string_type = std::basic_string<CharType>;
228
233 utf_encoder(const std::string& charset, method_type how = default_method) :
234 impl_(detail::make_utf_encoder<CharType>(charset, how))
235 {}
236
240 string_type convert(const char* begin, const char* end) const { return impl_->convert(begin, end); }
244 string_type convert(const core::string_view text) const { return impl_->convert(text); }
248 string_type operator()(const core::string_view text) const { return convert(text); }
249 };
250
252 template<typename CharType>
254 std::unique_ptr<detail::utf_decoder<CharType>> impl_;
255
256 public:
257 using char_type = CharType;
258 using stringview_type = core::basic_string_view<CharType>;
259
264 utf_decoder(const std::string& charset, method_type how = default_method) :
265 impl_(detail::make_utf_decoder<CharType>(charset, how))
266 {}
267
271 std::string convert(const CharType* begin, const CharType* end) const { return impl_->convert(begin, end); }
275 std::string convert(const stringview_type& text) const { return impl_->convert(text); }
279 std::string operator()(const stringview_type& text) const { return convert(text); }
280 };
281
283 std::unique_ptr<detail::narrow_converter> impl_;
284
285 public:
289 narrow_converter(const std::string& src_encoding,
290 const std::string& target_encoding,
292 impl_(detail::make_narrow_converter(src_encoding, target_encoding, how))
293 {}
294
298 std::string convert(const char* begin, const char* end) const { return impl_->convert(begin, end); }
302 std::string convert(const core::string_view text) const { return impl_->convert(text); }
306 std::string operator()(const core::string_view text) const { return convert(text); }
307 };
308 } // namespace conv
309}} // namespace boost::locale
310
311#ifdef BOOST_MSVC
312# pragma warning(pop)
313#endif
314
315#endif
Definition encoding.hpp:282
std::string convert(const core::string_view text) const
Definition encoding.hpp:302
narrow_converter(const std::string &src_encoding, const std::string &target_encoding, method_type how=default_method)
Definition encoding.hpp:289
std::string convert(const char *begin, const char *end) const
Definition encoding.hpp:298
std::string operator()(const core::string_view text) const
Definition encoding.hpp:306
Converter class to decode an UTF string and encode it using a local encoding.
Definition encoding.hpp:253
utf_decoder(const std::string &charset, method_type how=default_method)
Definition encoding.hpp:264
std::string convert(const CharType *begin, const CharType *end) const
Definition encoding.hpp:271
std::string operator()(const stringview_type &text) const
Definition encoding.hpp:279
std::string convert(const stringview_type &text) const
Definition encoding.hpp:275
Converter class to decode a narrow string using a local encoding and encode it with UTF.
Definition encoding.hpp:222
string_type convert(const char *begin, const char *end) const
Definition encoding.hpp:240
utf_encoder(const std::string &charset, method_type how=default_method)
Definition encoding.hpp:233
string_type convert(const core::string_view text) const
Definition encoding.hpp:244
string_type operator()(const core::string_view text) const
Definition encoding.hpp:248
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.