Boost.Locale
conversion.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_CONVERTER_HPP_INCLUDED
8 #define BOOST_LOCALE_CONVERTER_HPP_INCLUDED
9 
10 #include <boost/locale/config.hpp>
11 #include <locale>
12 
13 #ifdef BOOST_MSVC
14 # pragma warning(push)
15 # pragma warning(disable : 4275 4251 4231 4660)
16 #endif
17 
18 namespace boost {
19  namespace locale {
20 
27 
28 
33  public:
37  typedef enum {
44  };
45 
46  template<typename CharType>
47  class converter;
48 
49  #ifdef BOOST_LOCALE_DOXYGEN
50  template<typename Char>
57  class BOOST_LOCALE_DECL converter: public converter_base, public std::locale::facet {
58  public:
60  static std::locale::id id;
61 
63  converter(size_t refs = 0) : std::locale::facet(refs)
64  {
65  }
70  virtual std::basic_string<Char> convert(conversion_type how,Char const *begin,Char const *end,int flags = 0) const = 0;
71 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
72  std::locale::id& __get_id (void) const { return id; }
73 #endif
74  };
75  #else
76 
77  template<>
78  class BOOST_LOCALE_DECL converter<char> : public converter_base, public std::locale::facet {
79  public:
80  static std::locale::id id;
81 
82  converter(size_t refs = 0) : std::locale::facet(refs)
83  {
84  }
85  ~converter();
86  virtual std::string convert(conversion_type how,char const *begin,char const *end,int flags = 0) const = 0;
87 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
88  std::locale::id& __get_id (void) const { return id; }
89 #endif
90  };
91 
92  template<>
93  class BOOST_LOCALE_DECL converter<wchar_t> : public converter_base, public std::locale::facet {
94  public:
95  static std::locale::id id;
96  converter(size_t refs = 0) : std::locale::facet(refs)
97  {
98  }
99  ~converter();
100  virtual std::wstring convert(conversion_type how,wchar_t const *begin,wchar_t const *end,int flags = 0) const = 0;
101 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
102  std::locale::id& __get_id (void) const { return id; }
103 #endif
104  };
105 
106  #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
107  template<>
108  class BOOST_LOCALE_DECL converter<char16_t> : public converter_base, public std::locale::facet {
109  public:
110  static std::locale::id id;
111  converter(size_t refs = 0) : std::locale::facet(refs)
112  {
113  }
114  ~converter();
115  virtual std::u16string convert(conversion_type how,char16_t const *begin,char16_t const *end,int flags = 0) const = 0;
116 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
117  std::locale::id& __get_id (void) const { return id; }
118 #endif
119  };
120  #endif
121 
122  #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
123  template<>
124  class BOOST_LOCALE_DECL converter<char32_t> : public converter_base, public std::locale::facet {
125  public:
126  static std::locale::id id;
127  converter(size_t refs = 0) : std::locale::facet(refs)
128  {
129  }
130  ~converter();
131  virtual std::u32string convert(conversion_type how,char32_t const *begin,char32_t const *end,int flags = 0) const = 0;
132 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
133  std::locale::id& __get_id (void) const { return id; }
134 #endif
135  };
136  #endif
137 
138  #endif
139 
143 
144  typedef enum {
150  } norm_type;
151 
161  template<typename CharType>
162  std::basic_string<CharType> normalize(std::basic_string<CharType> const &str,norm_type n=norm_default,std::locale const &loc=std::locale())
163  {
164  return std::use_facet<converter<CharType> >(loc).convert(converter_base::normalization,str.data(),str.data() + str.size(),n);
165  }
166 
176  template<typename CharType>
177  std::basic_string<CharType> normalize(CharType const *str,norm_type n=norm_default,std::locale const &loc=std::locale())
178  {
179  CharType const *end=str;
180  while(*end)
181  end++;
182  return std::use_facet<converter<CharType> >(loc).convert(converter_base::normalization,str,end,n);
183  }
184 
194  template<typename CharType>
195  std::basic_string<CharType> normalize( CharType const *begin,
196  CharType const *end,
198  std::locale const &loc=std::locale())
199  {
200  return std::use_facet<converter<CharType> >(loc).convert(converter_base::normalization,begin,end,n);
201  }
202 
204 
210 
211  template<typename CharType>
212  std::basic_string<CharType> to_upper(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
213  {
214  return std::use_facet<converter<CharType> >(loc).convert(converter_base::upper_case,str.data(),str.data()+str.size());
215  }
216 
222  template<typename CharType>
223  std::basic_string<CharType> to_upper(CharType const *str,std::locale const &loc=std::locale())
224  {
225  CharType const *end=str;
226  while(*end)
227  end++;
228  return std::use_facet<converter<CharType> >(loc).convert(converter_base::upper_case,str,end);
229  }
230 
236  template<typename CharType>
237  std::basic_string<CharType> to_upper(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
238  {
239  return std::use_facet<converter<CharType> >(loc).convert(converter_base::upper_case,begin,end);
240  }
241 
243 
249 
250  template<typename CharType>
251  std::basic_string<CharType> to_lower(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
252  {
253  return std::use_facet<converter<CharType> >(loc).convert(converter_base::lower_case,str.data(),str.data()+str.size());
254  }
255 
261  template<typename CharType>
262  std::basic_string<CharType> to_lower(CharType const *str,std::locale const &loc=std::locale())
263  {
264  CharType const *end=str;
265  while(*end)
266  end++;
267  return std::use_facet<converter<CharType> >(loc).convert(converter_base::lower_case,str,end);
268  }
269 
275  template<typename CharType>
276  std::basic_string<CharType> to_lower(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
277  {
278  return std::use_facet<converter<CharType> >(loc).convert(converter_base::lower_case,begin,end);
279  }
281 
287 
288  template<typename CharType>
289  std::basic_string<CharType> to_title(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
290  {
291  return std::use_facet<converter<CharType> >(loc).convert(converter_base::title_case,str.data(),str.data()+str.size());
292  }
293 
299  template<typename CharType>
300  std::basic_string<CharType> to_title(CharType const *str,std::locale const &loc=std::locale())
301  {
302  CharType const *end=str;
303  while(*end)
304  end++;
305  return std::use_facet<converter<CharType> >(loc).convert(converter_base::title_case,str,end);
306  }
307 
313  template<typename CharType>
314  std::basic_string<CharType> to_title(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
315  {
316  return std::use_facet<converter<CharType> >(loc).convert(converter_base::title_case,begin,end);
317  }
318 
320 
326 
327  template<typename CharType>
328  std::basic_string<CharType> fold_case(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
329  {
330  return std::use_facet<converter<CharType> >(loc).convert(converter_base::case_folding,str.data(),str.data()+str.size());
331  }
332 
338  template<typename CharType>
339  std::basic_string<CharType> fold_case(CharType const *str,std::locale const &loc=std::locale())
340  {
341  CharType const *end=str;
342  while(*end)
343  end++;
344  return std::use_facet<converter<CharType> >(loc).convert(converter_base::case_folding,str,end);
345  }
346 
352  template<typename CharType>
353  std::basic_string<CharType> fold_case(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
354  {
355  return std::use_facet<converter<CharType> >(loc).convert(converter_base::case_folding,begin,end);
356  }
357 
361  } // locale
362 
363 } // boost
364 
365 #ifdef BOOST_MSVC
366 #pragma warning(pop)
367 #endif
368 
369 
370 #endif
371 
381 
382 
Canonical decomposition.
Definition: conversion.hpp:145
std::basic_string< CharType > fold_case(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.hpp:328
Apply Unicode normalization on the text.
Definition: conversion.hpp:38
The facet that implements text manipulation.
Definition: conversion.hpp:47
Convert text to lower case.
Definition: conversion.hpp:40
std::basic_string< CharType > to_title(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.hpp:289
Convert text to upper case.
Definition: conversion.hpp:39
std::basic_string< CharType > to_upper(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.hpp:212
Compatibility decomposition.
Definition: conversion.hpp:147
This class provides base flags for text manipulation. It is used as base for converter facet.
Definition: conversion.hpp:32
Convert text to title case.
Definition: conversion.hpp:42
norm_type
Definition: conversion.hpp:144
Compatibility decomposition followed by canonical composition.
Definition: conversion.hpp:148
conversion_type
Definition: conversion.hpp:37
std::basic_string< CharType > to_lower(std::basic_string< CharType > const &str, std::locale const &loc=std::locale())
Definition: conversion.hpp:251
Fold case in the text.
Definition: conversion.hpp:41
std::basic_string< CharType > normalize(std::basic_string< CharType > const &str, norm_type n=norm_default, std::locale const &loc=std::locale())
Definition: conversion.hpp:162
static std::locale::id id
Locale identification.
Definition: conversion.hpp:60
Canonical decomposition followed by canonical composition.
Definition: conversion.hpp:146
converter(size_t refs=0)
Standard constructor.
Definition: conversion.hpp:63
Default normalization - canonical decomposition followed by canonical composition.
Definition: conversion.hpp:149