unicode.hpp

Tue, 27 Dec 2016 13:35:57 +0100

author
David Demelier <markand@malikania.fr>
date
Tue, 27 Dec 2016 13:35:57 +0100
changeset 0
f94206b2e05e
child 1
0d9603b420c2
permissions
-rw-r--r--

Initial import

0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * unicode.hpp -- UTF-8 to UTF-32 conversions and various operations
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #ifndef UNICODE_HPP
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #define UNICODE_HPP
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
21
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 * \file unicode.hpp
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 * \brief UTF-8 to UTF-32 conversions
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 * \author David Demelier <markand@malikania.fr>
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
26 * \warning These files are auto-generated!
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
28
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 * \page unicode Basic unicode management.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 * ## Export macros
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 * You must define `UNICODE_DLL` globally and `UNICODE_BUILDING_DLL` when compiling the library if you want a DLL, alternatively you can provide
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 * your own `UNICODE_EXPORT` macro instead.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
37
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 * \cond UNICODE_HIDDEN_SYMBOLS
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
41
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
42 #if !defined(UNICODE_EXPORT)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
43 # if defined(UNICODE_DLL)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 # if defined(_WIN32)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 # if defined(UNICODE_BUILDING_DLL)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 # define UNICODE_EXPORT __declspec(dllexport)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 # else
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 # define UNICODE_EXPORT __declspec(dllimport)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 # endif
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 # else
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 # define UNICODE_EXPORT
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 # endif
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 # else
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 # define UNICODE_EXPORT
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 # endif
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 #endif
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
57
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 * \endcond
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
61
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 #include <stdexcept>
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 #include <string>
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
64
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
66 * \brief Unicode namespace.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 namespace unicode {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
69
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
71 * Encode the unicode code point into multibyte string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
72 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 * \param point the unicode code point
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 * \param res the output buffer
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 UNICODE_EXPORT void encode(char32_t point, char res[5]) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
77
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 * Decode the multibyte buffer into an unicode code point.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 * \param c the code point destination
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 * \param res the multibyte string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 UNICODE_EXPORT void decode(char32_t &c, const char *res) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
85
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
87 * Get the number of bytes for the first multi byte character from a
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 * utf-8 string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 * This can be used to iterate a valid UTF-8 string to jump to the next
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 * real character.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 * \param c the first multi byte character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 * \return the number of bytes [1-4] or -1 if invalid
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
96 UNICODE_EXPORT int nbytesUtf8(char c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
97
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
99 * Get the number of bytes for the unicode point.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 * \param point the unicode point
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 * \return the number of bytes [1-4] or -1 if invalid
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
103 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
104 UNICODE_EXPORT int nbytesPoint(char32_t point) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
105
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
106 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 * Get real number of character in a string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
108 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 * \param str the string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
110 * \return the length
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
111 * \throw std::invalid_argument on invalid sequence
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
112 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
113 UNICODE_EXPORT unsigned length(const std::string &str);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
114
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
115 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
116 * Iterate over all real characters in the UTF-8 string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
117 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
118 * The function must have the following signature:
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
119 * void f(char ch)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
120 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
121 * \param str the UTF-8 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
122 * \param function the function callback
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
123 * \throw std::invalid_argument on invalid sequence
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
124 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
125 template <typename Func>
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
126 void forEach(const std::string &str, Func function)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
127 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
128 for (size_t i = 0; i < str.size(); ) {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
129 char32_t point = 0;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
130 int size = nbytesUtf8(str[i]);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
131
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
132 if (size < 0)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
133 throw std::invalid_argument("invalid sequence");
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
134
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
135 decode(point, str.data() + i);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
136 function(point);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
137
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
138 i += size;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
139 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
140 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
141
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
142 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
143 * Convert a UTF-32 string to UTF-8 string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
144 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
145 * \param array the UTF-32 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
146 * \return the UTF-8 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
147 * \throw std::invalid_argument on invalid sequence
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
148 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
149 UNICODE_EXPORT std::string toUtf8(const std::u32string &array);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
150
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
151 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
152 * Convert a UTF-8 string to UTF-32 string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
153 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
154 * \param str the UTF-8 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
155 * \return the UTF-32 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
156 * \throw std::invalid_argument on invalid sequence
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
157 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
158 UNICODE_EXPORT std::u32string toUtf32(const std::string &str);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
159
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
160 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
161 * Check if the unicode character is space.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
162 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
163 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
164 * \return true if space
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
165 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
166 UNICODE_EXPORT bool isspace(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
167
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
168 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
169 * Check if the unicode character is digit.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
170 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
171 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
172 * \return true if digit
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
173 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
174 UNICODE_EXPORT bool isdigit(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
175
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
176 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
177 * Check if the unicode character is alpha category.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
178 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
179 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
180 * \return true if alpha
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
181 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
182 UNICODE_EXPORT bool isalpha(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
183
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
184 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
185 * Check if the unicode character is upper case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
186 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
187 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
188 * \return true if upper case
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
189 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
190 UNICODE_EXPORT bool isupper(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
191
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
192 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
193 * Check if the unicode character is lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
194 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
195 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
196 * \return true if lower case
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
197 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
198 UNICODE_EXPORT bool islower(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
199
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
200 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
201 * Check if the unicode character is title case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
202 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
203 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
204 * \return true if title case
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
205 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
206 UNICODE_EXPORT bool istitle(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
207
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
208 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
209 * Convert to upper case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
210 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
211 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
212 * \return the upper case character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
213 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
214 UNICODE_EXPORT char32_t toupper(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
215
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
216 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
217 * Convert to lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
218 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
219 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
220 * \return the lower case character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
221 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
222 UNICODE_EXPORT char32_t tolower(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
223
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
224 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
225 * Convert to title case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
226 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
227 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
228 * \return the title case character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
229 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
230 UNICODE_EXPORT char32_t totitle(char32_t c) noexcept;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
231
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
232 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
233 * Convert the UTF-32 string to upper case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
234 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
235 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
236 * \return the upper case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
237 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
238 inline std::u32string toupper(std::u32string str)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
239 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
240 for (size_t i = 0; i < str.size(); ++i)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
241 str[i] = toupper(str[i]);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
242
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
243 return str;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
244 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
245
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
246 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
247 * Convert the UTF-8 string to upper case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
248 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
249 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
250 * \return the upper case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
251 * \warning very slow at the moment
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
252 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
253 inline std::string toupper(const std::string &str)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
254 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
255 std::string result;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
256 char buffer[5];
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
257
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
258 forEach(str, [&] (char32_t code) {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
259 encode(toupper(code), buffer);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
260 result += buffer;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
261 });
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
262
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
263 return result;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
264 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
265
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
266 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
267 * Convert the UTF-32 string to lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
268 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
269 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
270 * \return the lower case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
271 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
272 inline std::u32string tolower(std::u32string str)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
273 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
274 for (size_t i = 0; i < str.size(); ++i)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
275 str[i] = tolower(str[i]);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
276
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
277 return str;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
278 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
279
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
280 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
281 * Convert the UTF-8 string to lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
282 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
283 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
284 * \return the lower case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
285 * \warning very slow at the moment
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
286 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
287 inline std::string tolower(const std::string &str)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
288 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
289 std::string result;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
290 char buffer[5];
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
291
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
292 forEach(str, [&] (char32_t code) {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
293 encode(tolower(code), buffer);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
294 result += buffer;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
295 });
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
296
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
297 return result;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
298 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
299
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
300 } // !unicode
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
301
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
302 #endif // !UNICODE_HPP

mercurial