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