Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_RTL_CHARACTER_HXX
21 : #define INCLUDED_RTL_CHARACTER_HXX
22 :
23 : #include <sal/config.h>
24 :
25 : #include <cassert>
26 :
27 : #include <sal/types.h>
28 :
29 : namespace rtl
30 : {
31 :
32 : /** Check for ASCII character.
33 :
34 : @param code A Unicode code point.
35 :
36 : @return True if code is an ASCII character (0x00--0x7F).
37 :
38 : @since LibreOffice 4.1
39 : */
40 33595294 : inline bool isAscii(sal_uInt32 code)
41 : {
42 : assert(code <= 0x10FFFF);
43 33595294 : return code <= 0x7F;
44 : }
45 :
46 : /** Check for ASCII lower case character.
47 :
48 : @param code A Unicode code point.
49 :
50 : @return True if code is an ASCII lower case alphabetic character (ASCII
51 : 'a'--'z').
52 :
53 : @since LibreOffice 4.1
54 : */
55 126190884 : inline bool isAsciiLowerCase(sal_uInt32 code)
56 : {
57 : assert(code <= 0x10FFFF);
58 126190884 : return code >= 'a' && code <= 'z';
59 : }
60 :
61 : /** Check for ASCII upper case character.
62 :
63 : @param code A Unicode code point.
64 :
65 : @return True if code is an ASCII upper case alphabetic character (ASCII
66 : 'A'--'Z').
67 :
68 : @since LibreOffice 4.1
69 : */
70 38646426369 : inline bool isAsciiUpperCase(sal_uInt32 code)
71 : {
72 : assert(code <= 0x10FFFF);
73 38646426369 : return code >= 'A' && code <= 'Z';
74 : }
75 :
76 : /** Check for ASCII alphabetic character.
77 :
78 : @param code A Unicode code point.
79 :
80 : @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
81 : 'a'--'z').
82 :
83 : @since LibreOffice 4.1
84 : */
85 120807407 : inline bool isAsciiAlpha(sal_uInt32 code)
86 : {
87 : assert(code <= 0x10FFFF);
88 120807407 : return isAsciiLowerCase(code) || isAsciiUpperCase(code);
89 : }
90 :
91 : /** Check for ASCII digit character.
92 :
93 : @param code A Unicode code point.
94 :
95 : @return True if code is an ASCII (decimal) digit character (ASCII
96 : '0'--'9').
97 :
98 : @since LibreOffice 4.1
99 : */
100 118831307 : inline bool isAsciiDigit(sal_uInt32 code)
101 : {
102 : assert(code <= 0x10FFFF);
103 118831307 : return code >= '0' && code <= '9';
104 : }
105 :
106 : /** Check for ASCII alphanumeric character.
107 :
108 : @param code A Unicode code point.
109 :
110 : @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
111 : 'A'--'Z', or 'a'--'z').
112 :
113 : @since LibreOffice 4.1
114 : */
115 116417008 : inline bool isAsciiAlphanumeric(sal_uInt32 code)
116 : {
117 : assert(code <= 0x10FFFF);
118 116417008 : return isAsciiDigit(code) || isAsciiAlpha(code);
119 : }
120 :
121 : /** Check for ASCII canonic hexadecimal digit character.
122 :
123 : @param code A Unicode code point.
124 :
125 : @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
126 : digit character (ASCII '0'--'9' or 'A'--'F').
127 :
128 : @since LibreOffice 4.1
129 : */
130 59 : inline bool isAsciiCanonicHexDigit(sal_uInt32 code)
131 : {
132 : assert(code <= 0x10FFFF);
133 59 : return isAsciiDigit(code) || (code >= 'A' && code <= 'F');
134 : }
135 :
136 : /** Check for ASCII hexadecimal digit character.
137 :
138 : @param code A Unicode code point.
139 :
140 : @return True if code is an ASCII hexadecimal digit character (ASCII
141 : '0'--'9', 'A'--'F', or 'a'--'f').
142 :
143 : @since LibreOffice 4.1
144 : */
145 59 : inline bool isAsciiHexDigit(sal_uInt32 code)
146 : {
147 : assert(code <= 0x10FFFF);
148 59 : return isAsciiCanonicHexDigit(code) || (code >= 'a' && code <= 'f');
149 : }
150 :
151 : /** Check for ASCII octal digit character.
152 :
153 : @param code A Unicode code point.
154 :
155 : @return True if code is an ASCII octal digit character (ASCII '0'--'7').
156 :
157 : @since LibreOffice 5.0
158 : */
159 26 : inline bool isAsciiOctalDigit(sal_uInt32 code)
160 : {
161 : assert(code <= 0x10FFFF);
162 26 : return code >= '0' && code <= '7';
163 : }
164 :
165 :
166 : /** Convert a character, if ASCII, to upper case.
167 :
168 : @param code A Unicode code point.
169 :
170 : @return code converted to ASCII upper case.
171 :
172 : @since LibreOffice 4.2
173 : */
174 1653120 : inline sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
175 : {
176 : assert(code <= 0x10FFFF);
177 1653120 : return isAsciiLowerCase(code) ? code - 32 : code;
178 : }
179 :
180 : /** Convert a character, if ASCII, to lower case.
181 :
182 : @param code A Unicode code point.
183 :
184 : @return code converted to ASCII lower case.
185 :
186 : @since LibreOffice 4.2
187 : */
188 38615237812 : inline sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
189 : {
190 : assert(code <= 0x10FFFF);
191 38615237812 : return isAsciiUpperCase(code) ? code + 32 : code;
192 : }
193 :
194 : /** Compare two characters ignoring ASCII case.
195 :
196 : @param code1 A Unicode code point.
197 :
198 : @param code2 A unicode code point.
199 :
200 : @return 0 if both code points are equal,
201 : < 0 if code1 is less than code2,
202 : > 0 if code1 is greater than code2.
203 :
204 : @since LibreOffice 4.2
205 : */
206 19278238205 : inline sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
207 : {
208 : assert(code1 <= 0x10FFFF);
209 : assert(code2 <= 0x10FFFF);
210 19278238205 : return static_cast<sal_Int32>(toAsciiLowerCase(code1))
211 19278238205 : - static_cast<sal_Int32>(toAsciiLowerCase(code2));
212 : }
213 :
214 : /// @cond INTERNAL
215 : namespace detail {
216 :
217 : sal_uInt32 const surrogatesHighFirst = 0xD800;
218 : sal_uInt32 const surrogatesHighLast = 0xDBFF;
219 : sal_uInt32 const surrogatesLowFirst = 0xDC00;
220 : sal_uInt32 const surrogatesLowLast = 0xDFFF;
221 :
222 : }
223 : /// @endcond
224 :
225 : /** Check for high surrogate.
226 :
227 : @param code A Unicode code point.
228 :
229 : @return True if code is a high surrogate code point (0xD800--0xDBFF).
230 :
231 : @since LibreOffice 5.0
232 : */
233 5498470864 : inline bool isHighSurrogate(sal_uInt32 code) {
234 : assert(code <= 0x10FFFF);
235 : return code >= detail::surrogatesHighFirst
236 5498470864 : && code <= detail::surrogatesHighLast;
237 : }
238 :
239 : /** Check for low surrogate.
240 :
241 : @param code A Unicode code point.
242 :
243 : @return True if code is a low surrogate code point (0xDC00--0xDFFF).
244 :
245 : @since LibreOffice 5.0
246 : */
247 187355287 : inline bool isLowSurrogate(sal_uInt32 code) {
248 : assert(code <= 0x10FFFF);
249 : return code >= detail::surrogatesLowFirst
250 187355287 : && code <= detail::surrogatesLowLast;
251 : }
252 :
253 : /** Get high surrogate half of a non-BMP Unicode code point.
254 :
255 : @param code A non-BMP Unicode code point.
256 :
257 : @return The UTF-16 high surrogate half for the give code point.
258 :
259 : @since LibreOffice 5.0
260 : */
261 4 : inline sal_Unicode getHighSurrogate(sal_uInt32 code) {
262 : assert(code <= 0x10FFFF);
263 : assert(code >= 0x10000);
264 4 : return ((code - 0x10000) >> 10) | detail::surrogatesHighFirst;
265 : }
266 :
267 : /** Get low surrogate half of a non-BMP Unicode code point.
268 :
269 : @param code A non-BMP Unicode code point.
270 :
271 : @return The UTF-16 low surrogate half for the give code point.
272 :
273 : @since LibreOffice 5.0
274 : */
275 4 : inline sal_Unicode getLowSurrogate(sal_uInt32 code) {
276 : assert(code <= 0x10FFFF);
277 : assert(code >= 0x10000);
278 4 : return ((code - 0x10000) & 0x3FF) | detail::surrogatesLowFirst;
279 : }
280 :
281 : /** Combine surrogates to form a code point.
282 :
283 : @param high A high surrogate code point.
284 :
285 : @param low A low surrogate code point.
286 :
287 : @return The code point represented by the surrogate pair.
288 :
289 : @since LibreOffice 5.0
290 : */
291 16 : inline sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low) {
292 : assert(isHighSurrogate(high));
293 : assert(isLowSurrogate(low));
294 16 : return ((high - detail::surrogatesHighFirst) << 10)
295 16 : + (low - detail::surrogatesLowFirst) + 0x10000;
296 : }
297 :
298 : }
299 :
300 : #endif
301 :
302 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|