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_UTF_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/locale/utf.hpp>
|
Chris@16
|
12 #include <boost/locale/encoding_errors.hpp>
|
Chris@16
|
13 #include <iterator>
|
Chris@16
|
14 #ifdef BOOST_MSVC
|
Chris@16
|
15 # pragma warning(push)
|
Chris@16
|
16 # pragma warning(disable : 4275 4251 4231 4660)
|
Chris@16
|
17 #endif
|
Chris@16
|
18
|
Chris@16
|
19
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost {
|
Chris@16
|
22 namespace locale {
|
Chris@16
|
23 namespace conv {
|
Chris@16
|
24 ///
|
Chris@16
|
25 /// \addtogroup codepage
|
Chris@16
|
26 ///
|
Chris@16
|
27 /// @{
|
Chris@16
|
28
|
Chris@16
|
29 ///
|
Chris@16
|
30 /// Convert a Unicode text in range [begin,end) to other Unicode encoding
|
Chris@16
|
31 ///
|
Chris@16
|
32 template<typename CharOut,typename CharIn>
|
Chris@16
|
33 std::basic_string<CharOut>
|
Chris@16
|
34 utf_to_utf(CharIn const *begin,CharIn const *end,method_type how = default_method)
|
Chris@16
|
35 {
|
Chris@16
|
36 std::basic_string<CharOut> result;
|
Chris@16
|
37 result.reserve(end-begin);
|
Chris@16
|
38 typedef std::back_insert_iterator<std::basic_string<CharOut> > inserter_type;
|
Chris@16
|
39 inserter_type inserter(result);
|
Chris@16
|
40 utf::code_point c;
|
Chris@16
|
41 while(begin!=end) {
|
Chris@16
|
42 c=utf::utf_traits<CharIn>::template decode<CharIn const *>(begin,end);
|
Chris@16
|
43 if(c==utf::illegal || c==utf::incomplete) {
|
Chris@16
|
44 if(how==stop)
|
Chris@16
|
45 throw conversion_error();
|
Chris@16
|
46 }
|
Chris@16
|
47 else {
|
Chris@16
|
48 utf::utf_traits<CharOut>::template encode<inserter_type>(c,inserter);
|
Chris@16
|
49 }
|
Chris@16
|
50 }
|
Chris@16
|
51 return result;
|
Chris@16
|
52 }
|
Chris@16
|
53
|
Chris@16
|
54 ///
|
Chris@16
|
55 /// Convert a Unicode NUL terminated string \a str other Unicode encoding
|
Chris@16
|
56 ///
|
Chris@16
|
57 template<typename CharOut,typename CharIn>
|
Chris@16
|
58 std::basic_string<CharOut>
|
Chris@16
|
59 utf_to_utf(CharIn const *str,method_type how = default_method)
|
Chris@16
|
60 {
|
Chris@16
|
61 CharIn const *end = str;
|
Chris@16
|
62 while(*end)
|
Chris@16
|
63 end++;
|
Chris@16
|
64 return utf_to_utf<CharOut,CharIn>(str,end,how);
|
Chris@16
|
65 }
|
Chris@16
|
66
|
Chris@16
|
67
|
Chris@16
|
68 ///
|
Chris@16
|
69 /// Convert a Unicode string \a str other Unicode encoding
|
Chris@16
|
70 ///
|
Chris@16
|
71 template<typename CharOut,typename CharIn>
|
Chris@16
|
72 std::basic_string<CharOut>
|
Chris@16
|
73 utf_to_utf(std::basic_string<CharIn> const &str,method_type how = default_method)
|
Chris@16
|
74 {
|
Chris@16
|
75 return utf_to_utf<CharOut,CharIn>(str.c_str(),str.c_str()+str.size(),how);
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78
|
Chris@16
|
79 /// @}
|
Chris@16
|
80
|
Chris@16
|
81 } // conv
|
Chris@16
|
82
|
Chris@16
|
83 } // locale
|
Chris@16
|
84 } // boost
|
Chris@16
|
85
|
Chris@16
|
86 #ifdef BOOST_MSVC
|
Chris@16
|
87 #pragma warning(pop)
|
Chris@16
|
88 #endif
|
Chris@16
|
89
|
Chris@16
|
90 #endif
|
Chris@16
|
91
|
Chris@16
|
92 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
|
Chris@16
|
93
|