1 | // |
2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. |
5 | // https://www.boost.org/LICENSE_1_0.txt |
6 | |
7 | #ifndef BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED |
8 | #define BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED |
9 | |
10 | #include <boost/locale/encoding_errors.hpp> |
11 | #include <boost/locale/utf.hpp> |
12 | #include <boost/locale/util/string.hpp> |
13 | #include <iterator> |
14 | |
15 | #ifdef BOOST_MSVC |
16 | # pragma warning(push) |
17 | # pragma warning(disable : 4275 4251 4231 4660) |
18 | #endif |
19 | |
20 | namespace boost { namespace locale { namespace conv { |
21 | /// \addtogroup codepage |
22 | /// |
23 | /// @{ |
24 | |
25 | /// Convert a Unicode text in range [begin,end) to other Unicode encoding |
26 | /// |
27 | /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded) |
28 | template<typename CharOut, typename CharIn> |
29 | std::basic_string<CharOut> utf_to_utf(const CharIn* begin, const CharIn* end, method_type how = default_method) |
30 | { |
31 | std::basic_string<CharOut> result; |
32 | result.reserve(end - begin); |
33 | std::back_insert_iterator<std::basic_string<CharOut>> inserter(result); |
34 | while(begin != end) { |
35 | const utf::code_point c = utf::utf_traits<CharIn>::decode(begin, end); |
36 | if(c == utf::illegal || c == utf::incomplete) { |
37 | if(how == stop) |
38 | throw conversion_error(); |
39 | } else |
40 | utf::utf_traits<CharOut>::encode(c, inserter); |
41 | } |
42 | return result; |
43 | } |
44 | |
45 | /// Convert a Unicode NULL terminated string \a str other Unicode encoding |
46 | /// |
47 | /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded) |
48 | template<typename CharOut, typename CharIn> |
49 | std::basic_string<CharOut> utf_to_utf(const CharIn* str, method_type how = default_method) |
50 | { |
51 | return utf_to_utf<CharOut, CharIn>(str, util::str_end(str), how); |
52 | } |
53 | |
54 | /// Convert a Unicode string \a str other Unicode encoding |
55 | /// |
56 | /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded) |
57 | template<typename CharOut, typename CharIn> |
58 | std::basic_string<CharOut> utf_to_utf(const std::basic_string<CharIn>& str, method_type how = default_method) |
59 | { |
60 | return utf_to_utf<CharOut, CharIn>(str.c_str(), str.c_str() + str.size(), how); |
61 | } |
62 | |
63 | /// @} |
64 | |
65 | }}} // namespace boost::locale::conv |
66 | |
67 | #ifdef BOOST_MSVC |
68 | # pragma warning(pop) |
69 | #endif |
70 | |
71 | #endif |
72 | |