Boost.Locale
util.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_UTIL_HPP
8 #define BOOST_LOCALE_UTIL_HPP
9 
10 #include <boost/locale/generator.hpp>
11 #include <boost/locale/utf.hpp>
12 #include <boost/assert.hpp>
13 #include <boost/cstdint.hpp>
14 #include <locale>
15 #include <memory>
16 #include <typeinfo>
17 
18 namespace boost { namespace locale {
21  namespace util {
22 
34  BOOST_LOCALE_DECL
35  std::string get_system_locale(bool use_utf8_on_windows = false);
36 
52  BOOST_LOCALE_DECL
53  std::locale create_info(const std::locale& in, const std::string& name);
54 
67  class BOOST_LOCALE_DECL base_converter {
68  public:
72  static constexpr uint32_t illegal = utf::illegal;
73 
76  static constexpr uint32_t incomplete = utf::incomplete;
77 
78  virtual ~base_converter();
79 
82  virtual int max_len() const { return 1; }
83 
91  virtual bool is_thread_safe() const { return false; }
92 
94  virtual base_converter* clone() const
95  {
96  BOOST_ASSERT(typeid(*this) == typeid(base_converter));
97  return new base_converter();
98  }
99 
114  virtual uint32_t to_unicode(const char*& begin, const char* end)
115  {
116  if(begin == end)
117  return incomplete;
118  unsigned char cp = *begin;
119  if(cp <= 0x7F) {
120  begin++;
121  return cp;
122  }
123  return illegal;
124  }
125 
136  virtual uint32_t from_unicode(uint32_t u, char* begin, const char* end)
137  {
138  if(begin == end)
139  return incomplete;
140  if(u >= 0x80)
141  return illegal;
142  *begin = static_cast<char>(u);
143  return 1;
144  }
145  };
146 
149  BOOST_LOCALE_DECL std::unique_ptr<base_converter> create_utf8_converter();
150 
151  BOOST_DEPRECATED("This function is deprecated, use 'create_utf8_converter()'")
152  inline std::unique_ptr<base_converter> create_utf8_converter_unique_ptr()
153  {
154  return create_utf8_converter();
155  }
156 
162  BOOST_LOCALE_DECL std::unique_ptr<base_converter> create_simple_converter(const std::string& encoding);
163 
164  BOOST_DEPRECATED("This function is deprecated, use 'create_simple_converter()'")
165  inline std::unique_ptr<base_converter> create_simple_converter_unique_ptr(const std::string& encoding)
166  {
167  return create_simple_converter(encoding);
168  }
169 
180  BOOST_LOCALE_DECL
181  std::locale create_codecvt(const std::locale& in, std::unique_ptr<base_converter> cvt, char_facet_t type);
182 
183  BOOST_DEPRECATED("This function is deprecated, use 'create_codecvt()'")
184  inline std::locale create_codecvt_from_pointer(const std::locale& in, base_converter* cvt, char_facet_t type)
185  {
186  return create_codecvt(in, std::unique_ptr<base_converter>(cvt), type);
187  }
188 
191  BOOST_LOCALE_DECL base_converter* create_utf8_converter_new_ptr();
192 
198  BOOST_LOCALE_DECL base_converter* create_simple_converter_new_ptr(const std::string& encoding);
199 
202  BOOST_LOCALE_DECL
203  std::locale create_utf8_codecvt(const std::locale& in, char_facet_t type);
204 
210  BOOST_LOCALE_DECL
211  std::locale create_simple_codecvt(const std::locale& in, const std::string& encoding, char_facet_t type);
212  } // namespace util
213 }} // namespace boost::locale
214 
215 #endif
std::locale create_codecvt(const std::locale &in, std::unique_ptr< base_converter > cvt, char_facet_t type)
virtual base_converter * clone() const
Create a polymorphic copy of this object, usually called only if is_thread_safe() return false.
Definition: util.hpp:94
virtual uint32_t from_unicode(uint32_t u, char *begin, const char *end)
Definition: util.hpp:136
char_facet_t
Definition: generator.hpp:34
virtual int max_len() const
Definition: util.hpp:82
std::locale create_info(const std::locale &in, const std::string &name)
Installs information facet to locale in based on locale name name.
base_converter * create_utf8_converter_new_ptr()
std::locale create_utf8_codecvt(const std::locale &in, char_facet_t type)
This class represent a simple stateless converter from UCS-4 and to UCS-4 for each single code point.
Definition: util.hpp:67
std::string get_system_locale(bool use_utf8_on_windows=false)
Return default system locale name in POSIX format.
std::unique_ptr< base_converter > create_utf8_converter()
std::locale create_simple_codecvt(const std::locale &in, const std::string &encoding, char_facet_t type)
constexpr code_point illegal
Special constant that defines illegal code point.
Definition: utf.hpp:22
base_converter * create_simple_converter_new_ptr(const std::string &encoding)
virtual uint32_t to_unicode(const char *&begin, const char *end)
Definition: util.hpp:114
std::unique_ptr< base_converter > create_simple_converter(const std::string &encoding)
virtual bool is_thread_safe() const
Definition: util.hpp:91
constexpr code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:24