CppCMS
encoding_utf.h
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 BOOSTER_LOCALE_ENCODING_UTF_H_INCLUDED
9 #define BOOSTER_LOCALE_ENCODING_UTF_H_INCLUDED
10 
11 #include <booster/locale/utf.h>
12 #include <booster/locale/encoding_errors.h>
13 #include <iterator>
14 #ifdef BOOSTER_MSVC
15 # pragma warning(push)
16 # pragma warning(disable : 4275 4251 4231 4660)
17 #endif
18 
19 
20 
21 namespace booster {
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 BOOSTER_MSVC
87 #pragma warning(pop)
88 #endif
89 
90 #endif
91 
92 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
93 
uint32_t code_point
The integral type that can hold a Unicode code point.
Definition: utf.h:34
Default method - skip.
Definition: encoding_errors.h:57
std::basic_string< CharOut > utf_to_utf(CharIn const *begin, CharIn const *end, method_type how=default_method)
Definition: encoding_utf.h:34
static const code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.h:44
The excepton that is thrown in case of conversion error.
Definition: encoding_errors.h:31
static const code_point illegal
Special constant that defines illegal code point.
Definition: utf.h:39
This is the main namespace that encloses all localization classes.
Definition: locale_fwd.h:14
method_type
Definition: encoding_errors.h:54
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
Stop conversion and throw conversion_error.
Definition: encoding_errors.h:56