Boost.Locale
encoding_utf.hpp
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 {
24 
26  template<typename CharOut, typename CharIn>
27  std::basic_string<CharOut> utf_to_utf(const CharIn* begin, const CharIn* end, method_type how = default_method)
28  {
29  std::basic_string<CharOut> result;
30  result.reserve(end - begin);
31  typedef std::back_insert_iterator<std::basic_string<CharOut>> inserter_type;
32  inserter_type inserter(result);
34  while(begin != end) {
35  c = utf::utf_traits<CharIn>::template decode<const CharIn*>(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>::template encode<inserter_type>(c, inserter);
41  }
42  }
43  return result;
44  }
45 
47  template<typename CharOut, typename CharIn>
48  std::basic_string<CharOut> utf_to_utf(const CharIn* str, method_type how = default_method)
49  {
50  return utf_to_utf<CharOut, CharIn>(str, util::str_end(str), how);
51  }
52 
54  template<typename CharOut, typename CharIn>
55  std::basic_string<CharOut> utf_to_utf(const std::basic_string<CharIn>& str, method_type how = default_method)
56  {
57  return utf_to_utf<CharOut, CharIn>(str.c_str(), str.c_str() + str.size(), how);
58  }
59 
61 
62 }}} // namespace boost::locale::conv
63 
64 #ifdef BOOST_MSVC
65 # pragma warning(pop)
66 #endif
67 
68 #endif
The exception that is thrown in case of conversion error.
Definition: encoding_errors.hpp:25
uint32_t code_point
The integral type that can hold a Unicode code point.
Definition: utf.hpp:19
UTF Traits class - functions to convert UTF sequences to and from Unicode code points.
Definition: utf.hpp:40
Default method - skip.
Definition: encoding_errors.hpp:44
Char * str_end(Char *str)
Return the end of a C-string, i.e. the pointer to the trailing NULL byte.
Definition: string.hpp:15
std::basic_string< CharOut > utf_to_utf(const CharIn *begin, const CharIn *end, method_type how=default_method)
Convert a Unicode text in range [begin,end) to other Unicode encoding.
Definition: encoding_utf.hpp:27
Stop conversion and throw conversion_error.
Definition: encoding_errors.hpp:43
constexpr code_point illegal
Special constant that defines illegal code point.
Definition: utf.hpp:22
method_type
enum that defines conversion policy
Definition: encoding_errors.hpp:41
constexpr code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:24