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 <boost/locale/util/string.hpp>
12 #include <locale>
13 
14 #ifdef BOOST_MSVC
15 # pragma warning(push)
16 # pragma warning(disable : 4275 4251 4231 4660)
17 #endif
18 
19 namespace boost { namespace locale {
20 
25 
28  public:
36  };
37  };
38 
39  template<typename CharType>
40  class converter;
41 
42 #ifdef BOOST_LOCALE_DOXYGEN
43  template<typename Char>
49  class BOOST_LOCALE_DECL converter : public converter_base, public std::locale::facet {
50  public:
52  static std::locale::id id;
53 
55  converter(size_t refs = 0) : std::locale::facet(refs) {}
56 
59  virtual std::basic_string<Char>
60  convert(conversion_type how, const Char* begin, const Char* end, int flags = 0) const = 0;
61  };
62 #else
63 
64  template<>
65  class BOOST_LOCALE_DECL converter<char> : public converter_base, public std::locale::facet {
66  public:
67  static std::locale::id id;
68 
69  converter(size_t refs = 0) : std::locale::facet(refs) {}
70  ~converter();
71  virtual std::string convert(conversion_type how, const char* begin, const char* end, int flags = 0) const = 0;
72  };
73 
74  template<>
75  class BOOST_LOCALE_DECL converter<wchar_t> : public converter_base, public std::locale::facet {
76  public:
77  static std::locale::id id;
78  converter(size_t refs = 0) : std::locale::facet(refs) {}
79  ~converter();
80  virtual std::wstring
81  convert(conversion_type how, const wchar_t* begin, const wchar_t* end, int flags = 0) const = 0;
82  };
83 
84 # ifdef BOOST_LOCALE_ENABLE_CHAR16_T
85  template<>
86  class BOOST_LOCALE_DECL converter<char16_t> : public converter_base, public std::locale::facet {
87  public:
88  static std::locale::id id;
89  converter(size_t refs = 0) : std::locale::facet(refs) {}
90  ~converter();
91  virtual std::u16string
92  convert(conversion_type how, const char16_t* begin, const char16_t* end, int flags = 0) const = 0;
93  };
94 # endif
95 
96 # ifdef BOOST_LOCALE_ENABLE_CHAR32_T
97  template<>
98  class BOOST_LOCALE_DECL converter<char32_t> : public converter_base, public std::locale::facet {
99  public:
100  static std::locale::id id;
101  converter(size_t refs = 0) : std::locale::facet(refs) {}
102  ~converter();
103  virtual std::u32string
104  convert(conversion_type how, const char32_t* begin, const char32_t* end, int flags = 0) const = 0;
105  };
106 # endif
107 
108 #endif
109 
111  enum norm_type {
117  };
118 
126  template<typename CharType>
127  std::basic_string<CharType> normalize(const CharType* begin,
128  const CharType* end,
130  const std::locale& loc = std::locale())
131  {
132  return std::use_facet<converter<CharType>>(loc).convert(converter_base::normalization, begin, end, n);
133  }
134 
142  template<typename CharType>
143  std::basic_string<CharType> normalize(const std::basic_string<CharType>& str,
145  const std::locale& loc = std::locale())
146  {
147  return normalize(str.data(), str.data() + str.size(), n, loc);
148  }
149 
157  template<typename CharType>
158  std::basic_string<CharType>
159  normalize(const CharType* str, norm_type n = norm_default, const std::locale& loc = std::locale())
160  {
161  return normalize(str, util::str_end(str), n, loc);
162  }
163 
165 
169  template<typename CharType>
170  std::basic_string<CharType>
171  to_upper(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
172  {
173  return std::use_facet<converter<CharType>>(loc).convert(converter_base::upper_case, begin, end);
174  }
175 
179  template<typename CharType>
180  std::basic_string<CharType> to_upper(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
181  {
182  return to_upper(str.data(), str.data() + str.size(), loc);
183  }
184 
188  template<typename CharType>
189  std::basic_string<CharType> to_upper(const CharType* str, const std::locale& loc = std::locale())
190  {
191  return to_upper(str, util::str_end(str), loc);
192  }
193 
195 
199  template<typename CharType>
200  std::basic_string<CharType>
201  to_lower(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
202  {
203  return std::use_facet<converter<CharType>>(loc).convert(converter_base::lower_case, begin, end);
204  }
205 
209  template<typename CharType>
210  std::basic_string<CharType> to_lower(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
211  {
212  return to_lower(str.data(), str.data() + str.size(), loc);
213  }
214 
218  template<typename CharType>
219  std::basic_string<CharType> to_lower(const CharType* str, const std::locale& loc = std::locale())
220  {
221  return to_lower(str, util::str_end(str), loc);
222  }
223 
225 
229  template<typename CharType>
230  std::basic_string<CharType>
231  to_title(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
232  {
233  return std::use_facet<converter<CharType>>(loc).convert(converter_base::title_case, begin, end);
234  }
235 
239  template<typename CharType>
240  std::basic_string<CharType> to_title(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
241  {
242  return to_title(str.data(), str.data() + str.size(), loc);
243  }
244 
248  template<typename CharType>
249  std::basic_string<CharType> to_title(const CharType* str, const std::locale& loc = std::locale())
250  {
251  return to_title(str, util::str_end(str), loc);
252  }
253 
255 
259  template<typename CharType>
260  std::basic_string<CharType>
261  fold_case(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
262  {
263  return std::use_facet<converter<CharType>>(loc).convert(converter_base::case_folding, begin, end);
264  }
265 
269  template<typename CharType>
270  std::basic_string<CharType> fold_case(const std::basic_string<CharType>& str,
271  const std::locale& loc = std::locale())
272  {
273  return fold_case(str.data(), str.data() + str.size(), loc);
274  }
275 
279  template<typename CharType>
280  std::basic_string<CharType> fold_case(const CharType* str, const std::locale& loc = std::locale())
281  {
282  return fold_case(str, util::str_end(str), loc);
283  }
284 
286 }} // namespace boost::locale
287 
288 #ifdef BOOST_MSVC
289 # pragma warning(pop)
290 #endif
291 
299 
300 #endif
std::basic_string< CharType > fold_case(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:261
Canonical decomposition.
Definition: conversion.hpp:112
Apply Unicode normalization on the text.
Definition: conversion.hpp:31
The facet that implements text manipulation.
Definition: conversion.hpp:40
Convert text to lower case.
Definition: conversion.hpp:33
std::basic_string< CharType > normalize(const CharType *begin, const CharType *end, norm_type n=norm_default, const std::locale &loc=std::locale())
Definition: conversion.hpp:127
Convert text to upper case.
Definition: conversion.hpp:32
Compatibility decomposition.
Definition: conversion.hpp:114
This class provides base flags for text manipulation. It is used as base for converter facet.
Definition: conversion.hpp:27
Convert text to title case.
Definition: conversion.hpp:35
Generate conversion facets.
norm_type
The type that defined normalization form
Definition: conversion.hpp:111
Compatibility decomposition followed by canonical composition.
Definition: conversion.hpp:115
conversion_type
The flag used for facet - the type of operation to perform.
Definition: conversion.hpp:30
Char * str_end(Char *str)
Return the end of a C-string, i.e. the pointer to the trailing NULL byte.
Definition: string.hpp:15
Fold case in the text.
Definition: conversion.hpp:34
std::basic_string< CharType > to_lower(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:201
static std::locale::id id
Locale identification.
Definition: conversion.hpp:52
Canonical decomposition followed by canonical composition.
Definition: conversion.hpp:113
std::basic_string< CharType > to_upper(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:171
converter(size_t refs=0)
Standard constructor.
Definition: conversion.hpp:55
Default normalization - canonical decomposition followed by canonical composition.
Definition: conversion.hpp:116
std::basic_string< CharType > to_title(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:231