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 #include <cstddef>
27
28 #include "sal/types.h"
29
30 namespace rtl
31 {
32
33 /** Check for Unicode code point.
34
35 @param code An integer.
36
37 @return True if code is a Unicode code point.
38
39 @since LibreOffice 5.2
40 */
isUnicodeCodePoint(sal_uInt32 code)41 inline bool isUnicodeCodePoint(sal_uInt32 code)
42 {
43 return code <= 0x10FFFF;
44 }
45
46 /** Check for ASCII character.
47
48 @param code A Unicode code point.
49
50 @return True if code is an ASCII character (0x00--0x7F).
51
52 @since LibreOffice 4.1
53 */
isAscii(sal_uInt32 code)54 inline bool isAscii(sal_uInt32 code)
55 {
56 assert(isUnicodeCodePoint(code));
57 return code <= 0x7F;
58 }
59
60 #if defined LIBO_INTERNAL_ONLY
61 bool isAscii(char) = delete;
62 bool isAscii(signed char) = delete;
isAscii(T code)63 template<typename T> inline bool isAscii(T code)
64 { return isAscii(sal_uInt32(code)); }
65 #endif
66
67 /** Check for ASCII lower case character.
68
69 @param code A Unicode code point.
70
71 @return True if code is an ASCII lower case alphabetic character (ASCII
72 'a'--'z').
73
74 @since LibreOffice 4.1
75 */
isAsciiLowerCase(sal_uInt32 code)76 inline bool isAsciiLowerCase(sal_uInt32 code)
77 {
78 assert(isUnicodeCodePoint(code));
79 return code >= 'a' && code <= 'z';
80 }
81
82 #if defined LIBO_INTERNAL_ONLY
83 bool isAsciiLowerCase(char) = delete;
84 bool isAsciiLowerCase(signed char) = delete;
isAsciiLowerCase(T code)85 template<typename T> inline bool isAsciiLowerCase(T code)
86 { return isAsciiLowerCase(sal_uInt32(code)); }
87 #endif
88
89 /** Check for ASCII upper case character.
90
91 @param code A Unicode code point.
92
93 @return True if code is an ASCII upper case alphabetic character (ASCII
94 'A'--'Z').
95
96 @since LibreOffice 4.1
97 */
isAsciiUpperCase(sal_uInt32 code)98 inline bool isAsciiUpperCase(sal_uInt32 code)
99 {
100 assert(isUnicodeCodePoint(code));
101 return code >= 'A' && code <= 'Z';
102 }
103
104 #if defined LIBO_INTERNAL_ONLY
105 bool isAsciiUpperCase(char) = delete;
106 bool isAsciiUpperCase(signed char) = delete;
isAsciiUpperCase(T code)107 template<typename T> inline bool isAsciiUpperCase(T code)
108 { return isAsciiUpperCase(sal_uInt32(code)); }
109 #endif
110
111 /** Check for ASCII alphabetic character.
112
113 @param code A Unicode code point.
114
115 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
116 'a'--'z').
117
118 @since LibreOffice 4.1
119 */
isAsciiAlpha(sal_uInt32 code)120 inline bool isAsciiAlpha(sal_uInt32 code)
121 {
122 assert(isUnicodeCodePoint(code));
123 return isAsciiLowerCase(code) || isAsciiUpperCase(code);
124 }
125
126 #if defined LIBO_INTERNAL_ONLY
127 bool isAsciiAlpha(char) = delete;
128 bool isAsciiAlpha(signed char) = delete;
isAsciiAlpha(T code)129 template<typename T> inline bool isAsciiAlpha(T code)
130 { return isAsciiAlpha(sal_uInt32(code)); }
131 #endif
132
133 /** Check for ASCII digit character.
134
135 @param code A Unicode code point.
136
137 @return True if code is an ASCII (decimal) digit character (ASCII
138 '0'--'9').
139
140 @since LibreOffice 4.1
141 */
isAsciiDigit(sal_uInt32 code)142 inline bool isAsciiDigit(sal_uInt32 code)
143 {
144 assert(isUnicodeCodePoint(code));
145 return code >= '0' && code <= '9';
146 }
147
148 #if defined LIBO_INTERNAL_ONLY
149 bool isAsciiDigit(char) = delete;
150 bool isAsciiDigit(signed char) = delete;
isAsciiDigit(T code)151 template<typename T> inline bool isAsciiDigit(T code)
152 { return isAsciiDigit(sal_uInt32(code)); }
153 #endif
154
155 /** Check for ASCII alphanumeric character.
156
157 @param code A Unicode code point.
158
159 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
160 'A'--'Z', or 'a'--'z').
161
162 @since LibreOffice 4.1
163 */
isAsciiAlphanumeric(sal_uInt32 code)164 inline bool isAsciiAlphanumeric(sal_uInt32 code)
165 {
166 assert(isUnicodeCodePoint(code));
167 return isAsciiDigit(code) || isAsciiAlpha(code);
168 }
169
170 #if defined LIBO_INTERNAL_ONLY
171 bool isAsciiAlphanumeric(char) = delete;
172 bool isAsciiAlphanumeric(signed char) = delete;
isAsciiAlphanumeric(T code)173 template<typename T> inline bool isAsciiAlphanumeric(T code)
174 { return isAsciiAlphanumeric(sal_uInt32(code)); }
175 #endif
176
177 /** Check for ASCII canonic hexadecimal digit character.
178
179 @param code A Unicode code point.
180
181 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
182 digit character (ASCII '0'--'9' or 'A'--'F').
183
184 @since LibreOffice 4.1
185 */
isAsciiCanonicHexDigit(sal_uInt32 code)186 inline bool isAsciiCanonicHexDigit(sal_uInt32 code)
187 {
188 assert(isUnicodeCodePoint(code));
189 return isAsciiDigit(code) || (code >= 'A' && code <= 'F');
190 }
191
192 #if defined LIBO_INTERNAL_ONLY
193 bool isAsciiCanonicHexDigit(char) = delete;
194 bool isAsciiCanonicHexDigit(signed char) = delete;
isAsciiCanonicHexDigit(T code)195 template<typename T> inline bool isAsciiCanonicHexDigit(T code)
196 { return isAsciiCanonicHexDigit(sal_uInt32(code)); }
197 #endif
198
199 /** Check for ASCII hexadecimal digit character.
200
201 @param code A Unicode code point.
202
203 @return True if code is an ASCII hexadecimal digit character (ASCII
204 '0'--'9', 'A'--'F', or 'a'--'f').
205
206 @since LibreOffice 4.1
207 */
isAsciiHexDigit(sal_uInt32 code)208 inline bool isAsciiHexDigit(sal_uInt32 code)
209 {
210 assert(isUnicodeCodePoint(code));
211 return isAsciiCanonicHexDigit(code) || (code >= 'a' && code <= 'f');
212 }
213
214 #if defined LIBO_INTERNAL_ONLY
215 bool isAsciiHexDigit(char) = delete;
216 bool isAsciiHexDigit(signed char) = delete;
isAsciiHexDigit(T code)217 template<typename T> inline bool isAsciiHexDigit(T code)
218 { return isAsciiHexDigit(sal_uInt32(code)); }
219 #endif
220
221 /** Check for ASCII octal digit character.
222
223 @param code A Unicode code point.
224
225 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
226
227 @since LibreOffice 5.0
228 */
isAsciiOctalDigit(sal_uInt32 code)229 inline bool isAsciiOctalDigit(sal_uInt32 code)
230 {
231 assert(isUnicodeCodePoint(code));
232 return code >= '0' && code <= '7';
233 }
234
235 #if defined LIBO_INTERNAL_ONLY
236 bool isAsciiOctalDigit(char) = delete;
237 bool isAsciiOctalDigit(signed char) = delete;
isAsciiOctalDigit(T code)238 template<typename T> inline bool isAsciiOctalDigit(T code)
239 { return isAsciiOctalDigit(sal_uInt32(code)); }
240 #endif
241
242 /** Check for ASCII white space character.
243
244 @param code A Unicode code point.
245
246 @return True if code is an ASCII white space character as defined by C for
247 isspace in the "C" locale (ASCII ' ', '\\f', '\\n', '\\r', '\\t' '\\v').
248
249 @since LibreOffice 5.4
250 */
isAsciiWhiteSpace(sal_uInt32 code)251 inline bool isAsciiWhiteSpace(sal_uInt32 code)
252 {
253 assert(isUnicodeCodePoint(code));
254 return code == ' ' || code == '\f' || code == '\n' || code == '\r'
255 || code == '\t' || code == '\v';
256 }
257
258 #if defined LIBO_INTERNAL_ONLY
259 bool isAsciiWhiteSpace(char) = delete;
260 bool isAsciiWhiteSpace(signed char) = delete;
isAsciiWhiteSpace(T code)261 template<typename T> inline bool isAsciiWhiteSpace(T code)
262 { return isAsciiWhiteSpace(sal_uInt32(code)); }
263 #endif
264
265 /** Convert a character, if ASCII, to upper case.
266
267 @param code A Unicode code point.
268
269 @return code converted to ASCII upper case.
270
271 @since LibreOffice 4.2
272 */
toAsciiUpperCase(sal_uInt32 code)273 inline sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
274 {
275 assert(isUnicodeCodePoint(code));
276 return isAsciiLowerCase(code) ? code - 32 : code;
277 }
278
279 #if defined LIBO_INTERNAL_ONLY
280 sal_uInt32 toAsciiUpperCase(char) = delete;
281 sal_uInt32 toAsciiUpperCase(signed char) = delete;
toAsciiUpperCase(T code)282 template<typename T> inline sal_uInt32 toAsciiUpperCase(T code)
283 { return toAsciiUpperCase(sal_uInt32(code)); }
284 #endif
285
286 /** Convert a character, if ASCII, to lower case.
287
288 @param code A Unicode code point.
289
290 @return code converted to ASCII lower case.
291
292 @since LibreOffice 4.2
293 */
toAsciiLowerCase(sal_uInt32 code)294 inline sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
295 {
296 assert(isUnicodeCodePoint(code));
297 return isAsciiUpperCase(code) ? code + 32 : code;
298 }
299
300 #if defined LIBO_INTERNAL_ONLY
301 sal_uInt32 toAsciiLowerCase(char) = delete;
302 sal_uInt32 toAsciiLowerCase(signed char) = delete;
toAsciiLowerCase(T code)303 template<typename T> inline sal_uInt32 toAsciiLowerCase(T code)
304 { return toAsciiLowerCase(sal_uInt32(code)); }
305 #endif
306
307 /** Compare two characters ignoring ASCII case.
308
309 @param code1 A Unicode code point.
310
311 @param code2 A unicode code point.
312
313 @return 0 if both code points are equal,
314 < 0 if code1 is less than code2,
315 > 0 if code1 is greater than code2.
316
317 @since LibreOffice 4.2
318 */
compareIgnoreAsciiCase(sal_uInt32 code1,sal_uInt32 code2)319 inline sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
320 {
321 assert(isUnicodeCodePoint(code1));
322 assert(isUnicodeCodePoint(code2));
323 return static_cast<sal_Int32>(toAsciiLowerCase(code1))
324 - static_cast<sal_Int32>(toAsciiLowerCase(code2));
325 }
326
327 /// @cond INTERNAL
328 namespace detail {
329
330 sal_uInt32 const surrogatesHighFirst = 0xD800;
331 sal_uInt32 const surrogatesHighLast = 0xDBFF;
332 sal_uInt32 const surrogatesLowFirst = 0xDC00;
333 sal_uInt32 const surrogatesLowLast = 0xDFFF;
334
335 }
336 /// @endcond
337
338 /** Check for surrogate.
339
340 @param code A Unicode code point.
341
342 @return True if code is a surrogate code point (0xD800--0xDFFF).
343
344 @since LibreOffice 6.0
345 */
isSurrogate(sal_uInt32 code)346 inline bool isSurrogate(sal_uInt32 code) {
347 assert(isUnicodeCodePoint(code));
348 return code >= detail::surrogatesHighFirst
349 && code <= detail::surrogatesLowLast;
350 }
351
352 /** Check for high surrogate.
353
354 @param code A Unicode code point.
355
356 @return True if code is a high surrogate code point (0xD800--0xDBFF).
357
358 @since LibreOffice 5.0
359 */
isHighSurrogate(sal_uInt32 code)360 inline bool isHighSurrogate(sal_uInt32 code) {
361 assert(isUnicodeCodePoint(code));
362 return code >= detail::surrogatesHighFirst
363 && code <= detail::surrogatesHighLast;
364 }
365
366 /** Check for low surrogate.
367
368 @param code A Unicode code point.
369
370 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
371
372 @since LibreOffice 5.0
373 */
isLowSurrogate(sal_uInt32 code)374 inline bool isLowSurrogate(sal_uInt32 code) {
375 assert(isUnicodeCodePoint(code));
376 return code >= detail::surrogatesLowFirst
377 && code <= detail::surrogatesLowLast;
378 }
379
380 /** Get high surrogate half of a non-BMP Unicode code point.
381
382 @param code A non-BMP Unicode code point.
383
384 @return The UTF-16 high surrogate half for the give code point.
385
386 @since LibreOffice 5.0
387 */
getHighSurrogate(sal_uInt32 code)388 inline sal_Unicode getHighSurrogate(sal_uInt32 code) {
389 assert(isUnicodeCodePoint(code));
390 assert(code >= 0x10000);
391 return static_cast<sal_Unicode>(((code - 0x10000) >> 10) | detail::surrogatesHighFirst);
392 }
393
394 /** Get low surrogate half of a non-BMP Unicode code point.
395
396 @param code A non-BMP Unicode code point.
397
398 @return The UTF-16 low surrogate half for the give code point.
399
400 @since LibreOffice 5.0
401 */
getLowSurrogate(sal_uInt32 code)402 inline sal_Unicode getLowSurrogate(sal_uInt32 code) {
403 assert(isUnicodeCodePoint(code));
404 assert(code >= 0x10000);
405 return static_cast<sal_Unicode>(((code - 0x10000) & 0x3FF) | detail::surrogatesLowFirst);
406 }
407
408 /** Combine surrogates to form a code point.
409
410 @param high A high surrogate code point.
411
412 @param low A low surrogate code point.
413
414 @return The code point represented by the surrogate pair.
415
416 @since LibreOffice 5.0
417 */
combineSurrogates(sal_uInt32 high,sal_uInt32 low)418 inline sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low) {
419 assert(isHighSurrogate(high));
420 assert(isLowSurrogate(low));
421 return ((high - detail::surrogatesHighFirst) << 10)
422 + (low - detail::surrogatesLowFirst) + 0x10000;
423 }
424
425 /** Split a Unicode code point into UTF-16 code units.
426
427 @param code A Unicode code point.
428
429 @param output A non-null pointer to an array with space for at least two
430 sal_Unicode UTF-16 code units.
431
432 @return The number of UTF-16 code units placed into the output (either one
433 or two).
434
435 @since LibreOffice 5.3
436 */
splitSurrogates(sal_uInt32 code,sal_Unicode * output)437 inline std::size_t splitSurrogates(sal_uInt32 code, sal_Unicode * output) {
438 assert(isUnicodeCodePoint(code));
439 assert(output != NULL);
440 if (code < 0x10000) {
441 output[0] = code;
442 return 1;
443 } else {
444 output[0] = getHighSurrogate(code);
445 output[1] = getLowSurrogate(code);
446 return 2;
447 }
448 }
449
450 /** Check for Unicode scalar value.
451
452 @param code An integer.
453
454 @return True if code is a Unicode scalar value.
455
456 @since LibreOffice 6.0
457 */
isUnicodeScalarValue(sal_uInt32 code)458 inline bool isUnicodeScalarValue(sal_uInt32 code)
459 {
460 return isUnicodeCodePoint(code) && !isSurrogate(code);
461 }
462
463 }
464
465 #endif
466
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
468