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. (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 {
28 
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)
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);
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 
57  template<typename CharOut,typename CharIn>
58  std::basic_string<CharOut>
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 
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)
74  {
75  return utf_to_utf<CharOut,CharIn>(str.c_str(),str.c_str()+str.size(),how);
76  }
77 
78 
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 
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:31
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:34
Default method - skip.
Definition: encoding_errors.hpp:57
Stop conversion and throw conversion_error.
Definition: encoding_errors.hpp:56
method_type
Definition: encoding_errors.hpp:54