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/detail/facet_id.hpp>
11#include <boost/locale/detail/is_supported_char.hpp>
12#include <boost/locale/util/string.hpp>
13#include <locale>
14
15#ifdef BOOST_MSVC
16# pragma warning(push)
17# pragma warning(disable : 4275 4251 4231 4660)
18#endif
19
20namespace boost { namespace locale {
21
26
29 public:
37 };
38 };
39
44 template<typename Char>
45 class BOOST_SYMBOL_VISIBLE converter : public converter_base,
46 public std::locale::facet,
47 public detail::facet_id<converter<Char>> {
48 BOOST_LOCALE_ASSERT_IS_SUPPORTED(Char);
49
50 public:
52 converter(size_t refs = 0) : std::locale::facet(refs) {}
55 virtual std::basic_string<Char>
56 convert(conversion_type how, const Char* begin, const Char* end, int flags = 0) const = 0;
57 };
58
60 enum norm_type {
66 };
67
75 template<typename CharType>
76 std::basic_string<CharType> normalize(const CharType* begin,
77 const CharType* end,
79 const std::locale& loc = std::locale())
80 {
81 return std::use_facet<converter<CharType>>(loc).convert(converter_base::normalization, begin, end, n);
82 }
83
91 template<typename CharType>
92 std::basic_string<CharType> normalize(const std::basic_string<CharType>& str,
94 const std::locale& loc = std::locale())
95 {
96 return normalize(str.data(), str.data() + str.size(), n, loc);
97 }
98
106 template<typename CharType>
107 std::basic_string<CharType>
108 normalize(const CharType* str, norm_type n = norm_default, const std::locale& loc = std::locale())
109 {
110 return normalize(str, util::str_end(str), n, loc);
111 }
112
114
118 template<typename CharType>
119 std::basic_string<CharType>
120 to_upper(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
121 {
122 return std::use_facet<converter<CharType>>(loc).convert(converter_base::upper_case, begin, end);
123 }
124
128 template<typename CharType>
129 std::basic_string<CharType> to_upper(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
130 {
131 return to_upper(str.data(), str.data() + str.size(), loc);
132 }
133
137 template<typename CharType>
138 std::basic_string<CharType> to_upper(const CharType* str, const std::locale& loc = std::locale())
139 {
140 return to_upper(str, util::str_end(str), loc);
141 }
142
144
148 template<typename CharType>
149 std::basic_string<CharType>
150 to_lower(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
151 {
152 return std::use_facet<converter<CharType>>(loc).convert(converter_base::lower_case, begin, end);
153 }
154
158 template<typename CharType>
159 std::basic_string<CharType> to_lower(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
160 {
161 return to_lower(str.data(), str.data() + str.size(), loc);
162 }
163
167 template<typename CharType>
168 std::basic_string<CharType> to_lower(const CharType* str, const std::locale& loc = std::locale())
169 {
170 return to_lower(str, util::str_end(str), loc);
171 }
172
174
178 template<typename CharType>
179 std::basic_string<CharType>
180 to_title(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
181 {
182 return std::use_facet<converter<CharType>>(loc).convert(converter_base::title_case, begin, end);
183 }
184
188 template<typename CharType>
189 std::basic_string<CharType> to_title(const std::basic_string<CharType>& str, const std::locale& loc = std::locale())
190 {
191 return to_title(str.data(), str.data() + str.size(), loc);
192 }
193
197 template<typename CharType>
198 std::basic_string<CharType> to_title(const CharType* str, const std::locale& loc = std::locale())
199 {
200 return to_title(str, util::str_end(str), loc);
201 }
202
204
208 template<typename CharType>
209 std::basic_string<CharType>
210 fold_case(const CharType* begin, const CharType* end, const std::locale& loc = std::locale())
211 {
212 return std::use_facet<converter<CharType>>(loc).convert(converter_base::case_folding, begin, end);
213 }
214
218 template<typename CharType>
219 std::basic_string<CharType> fold_case(const std::basic_string<CharType>& str,
220 const std::locale& loc = std::locale())
221 {
222 return fold_case(str.data(), str.data() + str.size(), loc);
223 }
224
228 template<typename CharType>
229 std::basic_string<CharType> fold_case(const CharType* str, const std::locale& loc = std::locale())
230 {
231 return fold_case(str, util::str_end(str), loc);
232 }
233
235}} // namespace boost::locale
236
237#ifdef BOOST_MSVC
238# pragma warning(pop)
239#endif
240
248
249#endif
This class provides base flags for text manipulation. It is used as base for converter facet.
Definition: conversion.hpp:28
conversion_type
The flag used for facet - the type of operation to perform.
Definition: conversion.hpp:31
@ upper_case
Convert text to upper case.
Definition: conversion.hpp:33
@ lower_case
Convert text to lower case.
Definition: conversion.hpp:34
@ case_folding
Fold case in the text.
Definition: conversion.hpp:35
@ title_case
Convert text to title case.
Definition: conversion.hpp:36
@ normalization
Apply Unicode normalization on the text.
Definition: conversion.hpp:32
The facet that implements text manipulation.
Definition: conversion.hpp:47
virtual std::basic_string< Char > convert(conversion_type how, const Char *begin, const Char *end, int flags=0) const =0
converter(size_t refs=0)
Standard constructor.
Definition: conversion.hpp:52
std::basic_string< CharType > to_title(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:180
std::basic_string< CharType > to_upper(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:120
std::basic_string< CharType > to_lower(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:150
norm_type
The type that defined normalization form
Definition: conversion.hpp:60
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:76
std::basic_string< CharType > fold_case(const CharType *begin, const CharType *end, const std::locale &loc=std::locale())
Definition: conversion.hpp:210
@ norm_nfkc
Compatibility decomposition followed by canonical composition.
Definition: conversion.hpp:64
@ norm_nfkd
Compatibility decomposition.
Definition: conversion.hpp:63
@ norm_nfd
Canonical decomposition.
Definition: conversion.hpp:61
@ norm_default
Default normalization - canonical decomposition followed by canonical composition.
Definition: conversion.hpp:65
@ norm_nfc
Canonical decomposition followed by canonical composition.
Definition: conversion.hpp:62
Char * str_end(Char *str)
Return the end of a C-string, i.e. the pointer to the trailing NULL byte.
Definition: string.hpp:16
@ convert
Generate conversion facets.