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 
33  BOOST_LOCALE_DECL
34  std::string get_system_locale(bool use_utf8_on_windows = false);
35 
51  BOOST_LOCALE_DECL
52  std::locale create_info(const std::locale& in, const std::string& name);
53 
66  class BOOST_LOCALE_DECL base_converter {
67  public:
71  static constexpr uint32_t illegal = utf::illegal;
72 
75  static constexpr uint32_t incomplete = utf::incomplete;
76 
77  virtual ~base_converter();
78 
81  virtual int max_len() const { return 1; }
82 
90  virtual bool is_thread_safe() const { return false; }
91 
93  virtual base_converter* clone() const
94  {
95  BOOST_ASSERT(typeid(*this) == typeid(base_converter));
96  return new base_converter();
97  }
98 
113  virtual uint32_t to_unicode(const char*& begin, const char* end)
114  {
115  if(begin == end)
116  return incomplete;
117  unsigned char cp = *begin;
118  if(cp <= 0x7F) {
119  begin++;
120  return cp;
121  }
122  return illegal;
123  }
124 
135  virtual uint32_t from_unicode(uint32_t u, char* begin, const char* end)
136  {
137  if(begin == end)
138  return incomplete;
139  if(u >= 0x80)
140  return illegal;
141  *begin = static_cast<char>(u);
142  return 1;
143  }
144  };
145 
148  BOOST_LOCALE_DECL std::unique_ptr<base_converter> create_utf8_converter();
149 
150  BOOST_DEPRECATED("This function is deprecated, use 'create_utf8_converter()'")
151  inline std::unique_ptr<base_converter> create_utf8_converter_unique_ptr()
152  {
153  return create_utf8_converter();
154  }
155 
161  BOOST_LOCALE_DECL std::unique_ptr<base_converter> create_simple_converter(const std::string& encoding);
162 
163  BOOST_DEPRECATED("This function is deprecated, use 'create_simple_converter()'")
164  inline std::unique_ptr<base_converter> create_simple_converter_unique_ptr(const std::string& encoding)
165  {
166  return create_simple_converter(encoding);
167  }
168 
179  BOOST_LOCALE_DECL
180  std::locale create_codecvt(const std::locale& in, std::unique_ptr<base_converter> cvt, char_facet_t type);
181 
182  BOOST_DEPRECATED("This function is deprecated, use 'create_codecvt()'")
183  inline std::locale create_codecvt_from_pointer(const std::locale& in, base_converter* cvt, char_facet_t type)
184  {
185  return create_codecvt(in, std::unique_ptr<base_converter>(cvt), type);
186  }
187 
190  BOOST_LOCALE_DECL base_converter* create_utf8_converter_new_ptr();
191 
197  BOOST_LOCALE_DECL base_converter* create_simple_converter_new_ptr(const std::string& encoding);
198 
201  BOOST_LOCALE_DECL
202  std::locale create_utf8_codecvt(const std::locale& in, char_facet_t type);
203 
209  BOOST_LOCALE_DECL
210  std::locale create_simple_codecvt(const std::locale& in, const std::string& encoding, char_facet_t type);
211  } // namespace util
212 }} // namespace boost::locale
213 
214 #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:93
virtual uint32_t from_unicode(uint32_t u, char *begin, const char *end)
Definition: util.hpp:135
char_facet_t
Definition: generator.hpp:34
virtual int max_len() const
Definition: util.hpp:81
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:66
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:113
std::unique_ptr< base_converter > create_simple_converter(const std::string &encoding)
virtual bool is_thread_safe() const
Definition: util.hpp:90
constexpr code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:24