unicode.hpp

Wed, 09 May 2018 08:56:34 +0200

author
David Demelier <markand@malikania.fr>
date
Wed, 09 May 2018 08:56:34 +0200
changeset 6
d9c9a35cb4b2
parent 5
ebcc8c9c8831
child 7
6ecc84c922b2
permissions
-rw-r--r--

Get rid of export macro, use CMake

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 *
5
ebcc8c9c8831 Update copyrights
David Demelier <markand@malikania.fr>
parents: 3
diff changeset
4 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
0
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 #include <stdexcept>
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 #include <string>
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
31
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 * \brief Unicode namespace.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 namespace unicode {
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 * Encode the unicode code point into multibyte string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 * \param point the unicode code point
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 * \param res the output buffer
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
42 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
43 void encode(char32_t point, char res[5]) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
44
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 * Decode the multibyte buffer into an unicode code point.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 * \param c the code point destination
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 * \param res the multibyte string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
51 void decode(char32_t& c, const char* res) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
52
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 * Get the number of bytes for the first multi byte character from a
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 * utf-8 string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 * 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
58 * real character.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 * \param c the first multi byte character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
61 * \return the number of bytes [1-4] or -1 if invalid
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
63 int nbytes_utf8(char c) noexcept;
0
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 * Get the number of bytes for the unicode point.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 * \param point the unicode point
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 * \return the number of bytes [1-4] or -1 if invalid
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
71 int nbytes_point(char32_t point) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
72
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 * Get real number of character in a string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 * \param str the string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
77 * \return the length
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 * \throw std::invalid_argument on invalid sequence
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
80 unsigned length(const std::string& str);
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
81
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 * Iterate over all real characters in the UTF-8 string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
85 * The function must have the following signature:
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 * void f(char ch)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
87 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 * \param str the UTF-8 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 * \param function the function callback
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 * \throw std::invalid_argument on invalid sequence
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 template <typename Func>
2
84765c6f4872 New style
David Demelier <markand@malikania.fr>
parents: 1
diff changeset
93 void for_each(const std::string& str, Func function)
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 for (size_t i = 0; i < str.size(); ) {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
96 char32_t point = 0;
2
84765c6f4872 New style
David Demelier <markand@malikania.fr>
parents: 1
diff changeset
97 int size = nbytes_utf8(str[i]);
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
98
3
d9d3406c1250 New style
David Demelier <markand@malikania.fr>
parents: 2
diff changeset
99 if (size < 0)
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 throw std::invalid_argument("invalid sequence");
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
101
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 decode(point, str.data() + i);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
103 function(point);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
104
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
105 i += size;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
106 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
108
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
110 * Convert a UTF-32 string to UTF-8 string.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
111 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
112 * \param array the UTF-32 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
113 * \return the UTF-8 string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
114 * \throw std::invalid_argument on invalid sequence
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
115 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
116 std::string to_utf8(const std::u32string& array);
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
117
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
118 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
119 * Convert a UTF-8 string to UTF-32 string.
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 * \return the UTF-32 string
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 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
125 std::u32string to_utf32(const std::string& str);
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
126
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
127 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
128 * Check if the unicode character is space.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
129 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
130 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
131 * \return true if space
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
132 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
133 bool isspace(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
134
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
135 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
136 * Check if the unicode character is digit.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
137 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
138 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
139 * \return true if digit
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
140 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
141 bool isdigit(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
142
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
143 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
144 * Check if the unicode character is alpha category.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
145 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
146 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
147 * \return true if alpha
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
148 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
149 bool isalpha(char32_t c) noexcept;
0
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 * Check if the unicode character is upper case.
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 c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
155 * \return true if upper case
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
156 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
157 bool isupper(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
158
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
159 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
160 * Check if the unicode character is lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
161 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
162 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
163 * \return true if lower case
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
164 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
165 bool islower(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
166
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
167 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
168 * Check if the unicode character is title case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
169 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
170 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
171 * \return true if title case
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
172 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
173 bool istitle(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
174
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
175 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
176 * Convert to upper case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
177 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
178 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
179 * \return the upper case character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
180 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
181 char32_t toupper(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
182
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
183 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
184 * Convert to lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
185 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
186 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
187 * \return the lower case character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
188 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
189 char32_t tolower(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
190
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
191 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
192 * Convert to title case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
193 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
194 * \param c the character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
195 * \return the title case character
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
196 */
6
d9c9a35cb4b2 Get rid of export macro, use CMake
David Demelier <markand@malikania.fr>
parents: 5
diff changeset
197 char32_t totitle(char32_t c) noexcept;
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
198
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
199 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
200 * Convert the UTF-32 string to upper case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
201 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
202 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
203 * \return the upper case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
204 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
205 inline std::u32string toupper(std::u32string str)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
206 {
3
d9d3406c1250 New style
David Demelier <markand@malikania.fr>
parents: 2
diff changeset
207 for (size_t i = 0; i < str.size(); ++i)
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
208 str[i] = toupper(str[i]);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
209
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
210 return str;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
211 }
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
212
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
213 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
214 * Convert the UTF-8 string to upper case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
215 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
216 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
217 * \return the upper case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
218 * \warning very slow at the moment
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
219 */
2
84765c6f4872 New style
David Demelier <markand@malikania.fr>
parents: 1
diff changeset
220 inline std::string toupper(const std::string& str)
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
221 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
222 std::string result;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
223 char buffer[5];
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
224
2
84765c6f4872 New style
David Demelier <markand@malikania.fr>
parents: 1
diff changeset
225 for_each(str, [&] (char32_t code) {
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
226 encode(toupper(code), buffer);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
227 result += buffer;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
228 });
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
229
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
230 return result;
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 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
234 * Convert the UTF-32 string to lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
235 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
236 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
237 * \return the lower case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
238 */
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
239 inline std::u32string tolower(std::u32string str)
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
240 {
3
d9d3406c1250 New style
David Demelier <markand@malikania.fr>
parents: 2
diff changeset
241 for (size_t i = 0; i < str.size(); ++i)
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
242 str[i] = tolower(str[i]);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
243
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
244 return str;
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 /**
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
248 * Convert the UTF-8 string to lower case.
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
249 *
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
250 * \param str the str
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
251 * \return the lower case string
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
252 * \warning very slow at the moment
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
253 */
2
84765c6f4872 New style
David Demelier <markand@malikania.fr>
parents: 1
diff changeset
254 inline std::string tolower(const std::string& str)
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
255 {
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
256 std::string result;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
257 char buffer[5];
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
258
2
84765c6f4872 New style
David Demelier <markand@malikania.fr>
parents: 1
diff changeset
259 for_each(str, [&] (char32_t code) {
0
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
260 encode(tolower(code), buffer);
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
261 result += buffer;
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
262 });
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
263
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
264 return result;
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 } // !unicode
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
268
f94206b2e05e Initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
269 #endif // !UNICODE_HPP

mercurial