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/util/string.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 { namespace locale {
19 
24 
27  public:
35  };
36  };
37 
38  template<typename CharType>
39  class converter;
40 
41 #ifdef BOOST_LOCALE_DOXYGEN
42  template<typename Char>
48  class BOOST_LOCALE_DECL converter : public converter_base, public std::locale::facet {
49  public:
51  static std::locale::id id;
52 
54  converter(size_t refs = 0) : std::locale::facet(refs) {}
55 
58  virtual std::basic_string<Char>
59  convert(conversion_type how, const Char* begin, const Char* end, int flags = 0) const = 0;
60  };
61 #else
62 
63  template<>
64  class BOOST_LOCALE_DECL converter<char> : public converter_base, public std::locale::facet {
65  public:
66  static std::locale::id id;
67 
68  converter(size_t refs = 0) : std::locale::facet(refs) {}
69  ~converter();
70  virtual std::string convert(conversion_type how, const char* begin, const char* end, int flags = 0) const = 0;
71  };
72 
73  template<>
74  class BOOST_LOCALE_DECL converter<wchar_t> : public converter_base, public std::locale::facet {
75  public:
76  static std::locale::id id;
77  converter(size_t refs = 0) : std::locale::facet(refs) {}
78  ~converter();
79  virtual std::wstring
80  convert(conversion_type how, const wchar_t* begin, const wchar_t* end, int flags = 0) const = 0;
81  };
82 
83 # ifdef BOOST_LOCALE_ENABLE_CHAR16_T
84  template<>
85  class BOOST_LOCALE_DECL converter<char16_t> : public converter_base, public std::locale::facet {
86  public:
87  static std::locale::id id;
88  converter(size_t refs = 0) : std::locale::facet(refs) {}
89  ~converter();
90  virtual std::u16string
91  convert(conversion_type how, const char16_t* begin, const char16_t* end, int flags = 0) const = 0;
92  };
93 # endif
94 
95 # ifdef BOOST_LOCALE_ENABLE_CHAR32_T
96  template<>
97  class BOOST_LOCALE_DECL converter<char32_t> : public converter_base, public std::locale::facet {
98  public:
99  static std::locale::id id;
100  converter(size_t refs = 0) : std::locale::facet(refs) {}
101  ~converter();
102  virtual std::u32string
103  convert(conversion_type how, const char32_t* begin, const char32_t* end, int flags = 0) const = 0;
104  };
105 # endif
106 
107 #endif
108 
110  enum norm_type {
116  };
117 
125  template<typename CharType>
126  std::basic_string<CharType> normalize(const CharType* begin,
127  const CharType* end,
129  const std::locale& loc = std::locale())
130  {
131  return std::use_facet<converter<CharType>>(loc).convert(converter_base::normalization, begin, end, n);
132  }
133 
141  template<typename CharType>
142  std::basic_string<CharType> normalize(const std::basic_string<CharType>& str,
144  const std::locale& loc = std::locale())
145  {
146  return normalize(str.data(), str.data() + str.size(), n, loc);
147  }
148 
156  template<typename CharType>
157  std::basic_string<CharType>
158  normalize(const CharType* str, norm_type n = norm_default, const std::locale& loc = std::locale())
159  {
160  return normalize(str, util::str_end(str), n, loc);
161  }
162 
164 
168  template<typename CharType>
169  std::basic_string<CharType>
170  to_upper(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
171  {
172  return std::use_facet<converter<CharType>>(loc).convert(converter_base::upper_case, begin, end);
173  }
174 
178  template<typename CharType>
179  std::basic_string<CharType> to_upper(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
180  {
181  return to_upper(str.data(), str.data() + str.size(), loc);
182  }
183 
187  template<typename CharType>
188  std::basic_string<CharType> to_upper(const CharType* str, const std::locale& loc = std::locale())
189  {
190  return to_upper(str, util::str_end(str), loc);
191  }
192 
194 
198  template<typename CharType>
199  std::basic_string<CharType>
200  to_lower(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
201  {
202  return std::use_facet<converter<CharType>>(loc).convert(converter_base::lower_case, begin, end);
203  }
204 
208  template<typename CharType>
209  std::basic_string<CharType> to_lower(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
210  {
211  return to_lower(str.data(), str.data() + str.size(), loc);
212  }
213 
217  template<typename CharType>
218  std::basic_string<CharType> to_lower(const CharType* str, const std::locale& loc = std::locale())
219  {
220  return to_lower(str, util::str_end(str), loc);
221  }
222 
224 
228  template<typename CharType>
229  std::basic_string<CharType>
230  to_title(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
231  {
232  return std::use_facet<converter<CharType>>(loc).convert(converter_base::title_case, begin, end);
233  }
234 
238  template<typename CharType>
239  std::basic_string<CharType> to_title(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
240  {
241  return to_title(str.data(), str.data() + str.size(), loc);
242  }
243 
247  template<typename CharType>
248  std::basic_string<CharType> to_title(const CharType* str, const std::locale& loc = std::locale())
249  {
250  return to_title(str, util::str_end(str), loc);
251  }
252 
254 
258  template<typename CharType>
259  std::basic_string<CharType>
260  fold_case(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
261  {
262  return std::use_facet<converter<CharType>>(loc).convert(converter_base::case_folding, begin, end);
263  }
264 
268  template<typename CharType>
269  std::basic_string<CharType> fold_case(const std::basic_string<CharType>& str,
270  const std::locale& loc = std::locale())
271  {
272  return fold_case(str.data(), str.data() + str.size(), loc);
273  }
274 
278  template<typename CharType>
279  std::basic_string<CharType> fold_case(const CharType* str, const std::locale& loc = std::locale())
280  {
281  return fold_case(str, util::str_end(str), loc);
282  }
283 
285 }} // namespace boost::locale
286 
287 #ifdef BOOST_MSVC
288 # pragma warning(pop)
289 #endif
290 
298 
299 #endif
std::basic_string< CharType > fold_case(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:260
Canonical decomposition.
Definition: conversion.hpp:111
Apply Unicode normalization on the text.
Definition: conversion.hpp:30
The facet that implements text manipulation.
Definition: conversion.hpp:39
Convert text to lower case.
Definition: conversion.hpp:32
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:126
Convert text to upper case.
Definition: conversion.hpp:31
Compatibility decomposition.
Definition: conversion.hpp:113
This class provides base flags for text manipulation. It is used as base for converter facet.
Definition: conversion.hpp:26
Convert text to title case.
Definition: conversion.hpp:34
Generate conversion facets.
norm_type
The type that defined normalization form
Definition: conversion.hpp:110
Compatibility decomposition followed by canonical composition.
Definition: conversion.hpp:114
conversion_type
The flag used for facet - the type of operation to perform.
Definition: conversion.hpp:29
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:33
std::basic_string< CharType > to_lower(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:200
static std::locale::id id
Locale identification.
Definition: conversion.hpp:51
Canonical decomposition followed by canonical composition.
Definition: conversion.hpp:112
std::basic_string< CharType > to_upper(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:170
converter(size_t refs=0)
Standard constructor.
Definition: conversion.hpp:54
Default normalization - canonical decomposition followed by canonical composition.
Definition: conversion.hpp:115
std::basic_string< CharType > to_title(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:230