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/utf.hpp>
11 #include <boost/locale/encoding_errors.hpp>
12 #include <iterator>
13 
14 #ifdef BOOST_MSVC
15 # pragma warning(push)
16 # pragma warning(disable : 4275 4251 4231 4660)
17 #endif
18 
19 namespace boost {
20  namespace locale {
21  namespace conv {
26 
30  template<typename CharOut,typename CharIn>
31  std::basic_string<CharOut>
32  utf_to_utf(CharIn const *begin,CharIn const *end,method_type how = default_method)
33  {
34  std::basic_string<CharOut> result;
35  result.reserve(end-begin);
36  typedef std::back_insert_iterator<std::basic_string<CharOut> > inserter_type;
37  inserter_type inserter(result);
39  while(begin!=end) {
40  c=utf::utf_traits<CharIn>::template decode<CharIn const *>(begin,end);
41  if(c==utf::illegal || c==utf::incomplete) {
42  if(how==stop)
43  throw conversion_error();
44  }
45  else {
46  utf::utf_traits<CharOut>::template encode<inserter_type>(c,inserter);
47  }
48  }
49  return result;
50  }
51 
55  template<typename CharOut,typename CharIn>
56  std::basic_string<CharOut>
57  utf_to_utf(CharIn const *str,method_type how = default_method)
58  {
59  CharIn const *end = str;
60  while(*end)
61  end++;
62  return utf_to_utf<CharOut,CharIn>(str,end,how);
63  }
64 
65 
69  template<typename CharOut,typename CharIn>
70  std::basic_string<CharOut>
71  utf_to_utf(std::basic_string<CharIn> const &str,method_type how = default_method)
72  {
73  return utf_to_utf<CharOut,CharIn>(str.c_str(),str.c_str()+str.size(),how);
74  }
75 
76 
78 
79  } // conv
80 
81  } // locale
82 } // boost
83 
84 #ifdef BOOST_MSVC
85 #pragma warning(pop)
86 #endif
87 
88 #endif
89 
90 
static const code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:44
The excepton that is thrown in case of conversion error.
Definition: encoding_errors.hpp:29
uint32_t code_point
The integral type that can hold a Unicode code point.
Definition: utf.hpp:34
static const code_point illegal
Special constant that defines illegal code point.
Definition: utf.hpp:39
UTF Traits class - functions to convert UTF sequences to and from Unicode code points.
Definition: utf.hpp:63
std::basic_string< CharOut > utf_to_utf(CharIn const *begin, CharIn const *end, method_type how=default_method)
Definition: encoding_utf.hpp:32
Default method - skip.
Definition: encoding_errors.hpp:55
Stop conversion and throw conversion_error.
Definition: encoding_errors.hpp:54
method_type
Definition: encoding_errors.hpp:52