Chris@16
|
1 //
|
Chris@16
|
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 #ifndef BOOST_LOCALE_ENCODING_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_LOCALE_ENCODING_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/locale/config.hpp>
|
Chris@16
|
12 #ifdef BOOST_MSVC
|
Chris@16
|
13 # pragma warning(push)
|
Chris@16
|
14 # pragma warning(disable : 4275 4251 4231 4660)
|
Chris@16
|
15 #endif
|
Chris@16
|
16 #include <boost/locale/info.hpp>
|
Chris@16
|
17 #include <boost/locale/encoding_errors.hpp>
|
Chris@16
|
18 #include <boost/locale/encoding_utf.hpp>
|
Chris@16
|
19
|
Chris@16
|
20
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost {
|
Chris@16
|
23 namespace locale {
|
Chris@16
|
24
|
Chris@16
|
25 ///
|
Chris@16
|
26 /// \brief Namespace that contains all functions related to character set conversion
|
Chris@16
|
27 ///
|
Chris@16
|
28 namespace conv {
|
Chris@16
|
29 ///
|
Chris@16
|
30 /// \defgroup codepage Character conversion functions
|
Chris@16
|
31 ///
|
Chris@16
|
32 /// @{
|
Chris@16
|
33
|
Chris@16
|
34 ///
|
Chris@16
|
35 /// convert string to UTF string from text in range [begin,end) encoded with \a charset according to policy \a how
|
Chris@16
|
36 ///
|
Chris@16
|
37 template<typename CharType>
|
Chris@16
|
38 std::basic_string<CharType> to_utf(char const *begin,char const *end,std::string const &charset,method_type how=default_method);
|
Chris@16
|
39
|
Chris@16
|
40 ///
|
Chris@16
|
41 /// convert UTF text in range [begin,end) to a text encoded with \a charset according to policy \a how
|
Chris@16
|
42 ///
|
Chris@16
|
43 template<typename CharType>
|
Chris@16
|
44 std::string from_utf(CharType const *begin,CharType const *end,std::string const &charset,method_type how=default_method);
|
Chris@16
|
45
|
Chris@16
|
46 ///
|
Chris@16
|
47 /// convert string to UTF string from text in range [begin,end) encoded according to locale \a loc according to policy \a how
|
Chris@16
|
48 ///
|
Chris@16
|
49 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
|
Chris@16
|
50 ///
|
Chris@16
|
51 template<typename CharType>
|
Chris@16
|
52 std::basic_string<CharType> to_utf(char const *begin,char const *end,std::locale const &loc,method_type how=default_method)
|
Chris@16
|
53 {
|
Chris@16
|
54 return to_utf<CharType>(begin,end,std::use_facet<info>(loc).encoding(),how);
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 ///
|
Chris@16
|
58 /// convert UTF text in range [begin,end) to a text encoded according to locale \a loc according to policy \a how
|
Chris@16
|
59 ///
|
Chris@16
|
60 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
|
Chris@16
|
61 ///
|
Chris@16
|
62 template<typename CharType>
|
Chris@16
|
63 std::string from_utf(CharType const *begin,CharType const *end,std::locale const &loc,method_type how=default_method)
|
Chris@16
|
64 {
|
Chris@16
|
65 return from_utf(begin,end,std::use_facet<info>(loc).encoding(),how);
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 ///
|
Chris@16
|
69 /// convert a string \a text encoded with \a charset to UTF string
|
Chris@16
|
70 ///
|
Chris@16
|
71
|
Chris@16
|
72 template<typename CharType>
|
Chris@16
|
73 std::basic_string<CharType> to_utf(std::string const &text,std::string const &charset,method_type how=default_method)
|
Chris@16
|
74 {
|
Chris@16
|
75 return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),charset,how);
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 ///
|
Chris@16
|
79 /// Convert a \a text from \a charset to UTF string
|
Chris@16
|
80 ///
|
Chris@16
|
81 template<typename CharType>
|
Chris@16
|
82 std::string from_utf(std::basic_string<CharType> const &text,std::string const &charset,method_type how=default_method)
|
Chris@16
|
83 {
|
Chris@16
|
84 return from_utf(text.c_str(),text.c_str()+text.size(),charset,how);
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 ///
|
Chris@16
|
88 /// Convert a \a text from \a charset to UTF string
|
Chris@16
|
89 ///
|
Chris@16
|
90 template<typename CharType>
|
Chris@16
|
91 std::basic_string<CharType> to_utf(char const *text,std::string const &charset,method_type how=default_method)
|
Chris@16
|
92 {
|
Chris@16
|
93 char const *text_end = text;
|
Chris@16
|
94 while(*text_end)
|
Chris@16
|
95 text_end++;
|
Chris@16
|
96 return to_utf<CharType>(text,text_end,charset,how);
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 ///
|
Chris@16
|
100 /// Convert a \a text from UTF to \a charset
|
Chris@16
|
101 ///
|
Chris@16
|
102 template<typename CharType>
|
Chris@16
|
103 std::string from_utf(CharType const *text,std::string const &charset,method_type how=default_method)
|
Chris@16
|
104 {
|
Chris@16
|
105 CharType const *text_end = text;
|
Chris@16
|
106 while(*text_end)
|
Chris@16
|
107 text_end++;
|
Chris@16
|
108 return from_utf(text,text_end,charset,how);
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 ///
|
Chris@16
|
112 /// Convert a \a text in locale encoding given by \a loc to UTF
|
Chris@16
|
113 ///
|
Chris@16
|
114 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
|
Chris@16
|
115 ///
|
Chris@16
|
116 template<typename CharType>
|
Chris@16
|
117 std::basic_string<CharType> to_utf(std::string const &text,std::locale const &loc,method_type how=default_method)
|
Chris@16
|
118 {
|
Chris@16
|
119 return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),loc,how);
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 ///
|
Chris@16
|
123 /// Convert a \a text in UTF to locale encoding given by \a loc
|
Chris@16
|
124 ///
|
Chris@16
|
125 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
|
Chris@16
|
126 ///
|
Chris@16
|
127 template<typename CharType>
|
Chris@16
|
128 std::string from_utf(std::basic_string<CharType> const &text,std::locale const &loc,method_type how=default_method)
|
Chris@16
|
129 {
|
Chris@16
|
130 return from_utf(text.c_str(),text.c_str()+text.size(),loc,how);
|
Chris@16
|
131 }
|
Chris@16
|
132
|
Chris@16
|
133 ///
|
Chris@16
|
134 /// Convert a \a text in locale encoding given by \a loc to UTF
|
Chris@16
|
135 ///
|
Chris@16
|
136 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
|
Chris@16
|
137 ///
|
Chris@16
|
138 template<typename CharType>
|
Chris@16
|
139 std::basic_string<CharType> to_utf(char const *text,std::locale const &loc,method_type how=default_method)
|
Chris@16
|
140 {
|
Chris@16
|
141 char const *text_end = text;
|
Chris@16
|
142 while(*text_end)
|
Chris@16
|
143 text_end++;
|
Chris@16
|
144 return to_utf<CharType>(text,text_end,loc,how);
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 ///
|
Chris@16
|
148 /// Convert a \a text in UTF to locale encoding given by \a loc
|
Chris@16
|
149 ///
|
Chris@16
|
150 /// \note throws std::bad_cast if the loc does not have \ref info facet installed
|
Chris@16
|
151 ///
|
Chris@16
|
152 template<typename CharType>
|
Chris@16
|
153 std::string from_utf(CharType const *text,std::locale const &loc,method_type how=default_method)
|
Chris@16
|
154 {
|
Chris@16
|
155 CharType const *text_end = text;
|
Chris@16
|
156 while(*text_end)
|
Chris@16
|
157 text_end++;
|
Chris@16
|
158 return from_utf(text,text_end,loc,how);
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161
|
Chris@16
|
162 ///
|
Chris@16
|
163 /// Convert a text in range [begin,end) to \a to_encoding from \a from_encoding
|
Chris@16
|
164 ///
|
Chris@16
|
165
|
Chris@16
|
166 BOOST_LOCALE_DECL
|
Chris@16
|
167 std::string between(char const *begin,
|
Chris@16
|
168 char const *end,
|
Chris@16
|
169 std::string const &to_encoding,
|
Chris@16
|
170 std::string const &from_encoding,
|
Chris@16
|
171 method_type how=default_method);
|
Chris@16
|
172
|
Chris@16
|
173 ///
|
Chris@16
|
174 /// Convert a \a text to \a to_encoding from \a from_encoding
|
Chris@16
|
175 ///
|
Chris@16
|
176
|
Chris@16
|
177 inline
|
Chris@16
|
178 std::string between(char const *text,
|
Chris@16
|
179 std::string const &to_encoding,
|
Chris@16
|
180 std::string const &from_encoding,
|
Chris@16
|
181 method_type how=default_method)
|
Chris@16
|
182 {
|
Chris@16
|
183 char const *end=text;
|
Chris@16
|
184 while(*end)
|
Chris@16
|
185 end++;
|
Chris@16
|
186 return boost::locale::conv::between(text,end,to_encoding,from_encoding,how);
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 ///
|
Chris@16
|
190 /// Convert a \a text to \a to_encoding from \a from_encoding
|
Chris@16
|
191 ///
|
Chris@16
|
192 inline
|
Chris@16
|
193 std::string between(std::string const &text,
|
Chris@16
|
194 std::string const &to_encoding,
|
Chris@16
|
195 std::string const &from_encoding,
|
Chris@16
|
196 method_type how=default_method)
|
Chris@16
|
197 {
|
Chris@16
|
198 return boost::locale::conv::between(text.c_str(),text.c_str()+text.size(),to_encoding,from_encoding,how);
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 /// \cond INTERNAL
|
Chris@16
|
202
|
Chris@16
|
203 template<>
|
Chris@16
|
204 BOOST_LOCALE_DECL std::basic_string<char> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
|
Chris@16
|
205
|
Chris@16
|
206 template<>
|
Chris@16
|
207 BOOST_LOCALE_DECL std::string from_utf(char const *begin,char const *end,std::string const &charset,method_type how);
|
Chris@16
|
208
|
Chris@16
|
209 template<>
|
Chris@16
|
210 BOOST_LOCALE_DECL std::basic_string<wchar_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
|
Chris@16
|
211
|
Chris@16
|
212 template<>
|
Chris@16
|
213 BOOST_LOCALE_DECL std::string from_utf(wchar_t const *begin,wchar_t const *end,std::string const &charset,method_type how);
|
Chris@16
|
214
|
Chris@16
|
215 #ifdef BOOST_HAS_CHAR16_T
|
Chris@16
|
216 template<>
|
Chris@16
|
217 BOOST_LOCALE_DECL std::basic_string<char16_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
|
Chris@16
|
218
|
Chris@16
|
219 template<>
|
Chris@16
|
220 BOOST_LOCALE_DECL std::string from_utf(char16_t const *begin,char16_t const *end,std::string const &charset,method_type how);
|
Chris@16
|
221 #endif
|
Chris@16
|
222
|
Chris@16
|
223 #ifdef BOOST_HAS_CHAR32_T
|
Chris@16
|
224 template<>
|
Chris@16
|
225 BOOST_LOCALE_DECL std::basic_string<char32_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how);
|
Chris@16
|
226
|
Chris@16
|
227 template<>
|
Chris@16
|
228 BOOST_LOCALE_DECL std::string from_utf(char32_t const *begin,char32_t const *end,std::string const &charset,method_type how);
|
Chris@16
|
229 #endif
|
Chris@16
|
230
|
Chris@16
|
231
|
Chris@16
|
232 /// \endcond
|
Chris@16
|
233
|
Chris@16
|
234 /// @}
|
Chris@16
|
235
|
Chris@16
|
236 } // conv
|
Chris@16
|
237
|
Chris@16
|
238 } // locale
|
Chris@16
|
239 } // boost
|
Chris@16
|
240
|
Chris@16
|
241 #ifdef BOOST_MSVC
|
Chris@16
|
242 #pragma warning(pop)
|
Chris@16
|
243 #endif
|
Chris@16
|
244
|
Chris@16
|
245 #endif
|
Chris@16
|
246
|
Chris@16
|
247 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
|
Chris@16
|
248
|