2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
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)
8 #ifndef BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED
9 #define BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED
11 #include <boost/locale/utf.hpp>
12 #include <boost/locale/encoding_errors.hpp>
15 # pragma warning(push)
16 # pragma warning(disable : 4275 4251 4231 4660)
25 /// \addtogroup codepage
30 /// Convert a Unicode text in range [begin,end) to other Unicode encoding
32 template<typename CharOut,typename CharIn>
33 std::basic_string<CharOut>
34 utf_to_utf(CharIn const *begin,CharIn const *end,method_type how = default_method)
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);
42 c=utf::utf_traits<CharIn>::template decode<CharIn const *>(begin,end);
43 if(c==utf::illegal || c==utf::incomplete) {
45 throw conversion_error();
48 utf::utf_traits<CharOut>::template encode<inserter_type>(c,inserter);
55 /// Convert a Unicode NUL terminated string \a str other Unicode encoding
57 template<typename CharOut,typename CharIn>
58 std::basic_string<CharOut>
59 utf_to_utf(CharIn const *str,method_type how = default_method)
61 CharIn const *end = str;
64 return utf_to_utf<CharOut,CharIn>(str,end,how);
69 /// Convert a Unicode string \a str other Unicode encoding
71 template<typename CharOut,typename CharIn>
72 std::basic_string<CharOut>
73 utf_to_utf(std::basic_string<CharIn> const &str,method_type how = default_method)
75 return utf_to_utf<CharOut,CharIn>(str.c_str(),str.c_str()+str.size(),how);
92 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4