bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / rtl / character.hxx
blob2a8933668964e09acfd2af9e75dbafdd862cf4fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_RTL_CHARACTER_HXX
21 #define INCLUDED_RTL_CHARACTER_HXX
23 #include "sal/config.h"
25 #include <cassert>
26 #include <cstddef>
28 #include "sal/types.h"
30 namespace rtl
33 /** Check for Unicode code point.
35 @param code An integer.
37 @return True if code is a Unicode code point.
39 @since LibreOffice 5.2
41 inline bool isUnicodeCodePoint(sal_uInt32 code)
43 return code <= 0x10FFFF;
46 /** Check for ASCII character.
48 @param code A Unicode code point.
50 @return True if code is an ASCII character (0x00--0x7F).
52 @since LibreOffice 4.1
54 inline bool isAscii(sal_uInt32 code)
56 assert(isUnicodeCodePoint(code));
57 return code <= 0x7F;
60 /** Check for Unicode variation sequence selectors
62 @param code A Unicode code point.
64 @return True if code is an Unicode variation sequence selector.
66 @since LibreOffice 6.3
68 inline bool isIVSSelector(sal_uInt32 nCode)
70 return (nCode >= 0xFE00 && nCode <= 0xFE0F) // Variation Selectors block
71 || (nCode >= 0xE0100 && nCode <= 0xE01EF);// Variation Selectors Supplement block
74 /** Check for base characters of a CJK ideographic variation sequence (IVS)
76 @param code A Unicode code point.
78 @return True if code is an Unicode base character part of CJK IVS
80 @since LibreOffice 6.3
82 inline bool isCJKIVSCharacter(sal_uInt32 nCode)
84 return (nCode >= 0x4E00 && nCode <= 0x9FFF) // CJK Unified Ideographs
85 || (nCode >= 0x3400 && nCode <= 0x4DBF) // CJK Unified Ideographs Extension A
86 || (nCode >= 0x20000 && nCode <= 0x2A6DF); // CJK Unified Ideographs Extension B
89 #if defined LIBO_INTERNAL_ONLY
90 bool isAscii(char) = delete;
91 bool isAscii(signed char) = delete;
92 template<typename T> inline bool isAscii(T code)
93 { return isAscii(sal_uInt32(code)); }
94 #endif
96 /** Check for ASCII lower case character.
98 @param code A Unicode code point.
100 @return True if code is an ASCII lower case alphabetic character (ASCII
101 'a'--'z').
103 @since LibreOffice 4.1
105 inline bool isAsciiLowerCase(sal_uInt32 code)
107 assert(isUnicodeCodePoint(code));
108 return code >= 'a' && code <= 'z';
111 #if defined LIBO_INTERNAL_ONLY
112 bool isAsciiLowerCase(char) = delete;
113 bool isAsciiLowerCase(signed char) = delete;
114 template<typename T> inline bool isAsciiLowerCase(T code)
115 { return isAsciiLowerCase(sal_uInt32(code)); }
116 #endif
118 /** Check for ASCII upper case character.
120 @param code A Unicode code point.
122 @return True if code is an ASCII upper case alphabetic character (ASCII
123 'A'--'Z').
125 @since LibreOffice 4.1
127 inline bool isAsciiUpperCase(sal_uInt32 code)
129 assert(isUnicodeCodePoint(code));
130 return code >= 'A' && code <= 'Z';
133 #if defined LIBO_INTERNAL_ONLY
134 bool isAsciiUpperCase(char) = delete;
135 bool isAsciiUpperCase(signed char) = delete;
136 template<typename T> inline bool isAsciiUpperCase(T code)
137 { return isAsciiUpperCase(sal_uInt32(code)); }
138 #endif
140 /** Check for ASCII alphabetic character.
142 @param code A Unicode code point.
144 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
145 'a'--'z').
147 @since LibreOffice 4.1
149 inline bool isAsciiAlpha(sal_uInt32 code)
151 assert(isUnicodeCodePoint(code));
152 return isAsciiLowerCase(code) || isAsciiUpperCase(code);
155 #if defined LIBO_INTERNAL_ONLY
156 bool isAsciiAlpha(char) = delete;
157 bool isAsciiAlpha(signed char) = delete;
158 template<typename T> inline bool isAsciiAlpha(T code)
159 { return isAsciiAlpha(sal_uInt32(code)); }
160 #endif
162 /** Check for ASCII digit character.
164 @param code A Unicode code point.
166 @return True if code is an ASCII (decimal) digit character (ASCII
167 '0'--'9').
169 @since LibreOffice 4.1
171 inline bool isAsciiDigit(sal_uInt32 code)
173 assert(isUnicodeCodePoint(code));
174 return code >= '0' && code <= '9';
177 #if defined LIBO_INTERNAL_ONLY
178 bool isAsciiDigit(char) = delete;
179 bool isAsciiDigit(signed char) = delete;
180 template<typename T> inline bool isAsciiDigit(T code)
181 { return isAsciiDigit(sal_uInt32(code)); }
182 #endif
184 /** Check for ASCII alphanumeric character.
186 @param code A Unicode code point.
188 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
189 'A'--'Z', or 'a'--'z').
191 @since LibreOffice 4.1
193 inline bool isAsciiAlphanumeric(sal_uInt32 code)
195 assert(isUnicodeCodePoint(code));
196 return isAsciiDigit(code) || isAsciiAlpha(code);
199 #if defined LIBO_INTERNAL_ONLY
200 bool isAsciiAlphanumeric(char) = delete;
201 bool isAsciiAlphanumeric(signed char) = delete;
202 template<typename T> inline bool isAsciiAlphanumeric(T code)
203 { return isAsciiAlphanumeric(sal_uInt32(code)); }
204 #endif
206 /** Check for ASCII canonic hexadecimal digit character.
208 @param code A Unicode code point.
210 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
211 digit character (ASCII '0'--'9' or 'A'--'F').
213 @since LibreOffice 4.1
215 inline bool isAsciiCanonicHexDigit(sal_uInt32 code)
217 assert(isUnicodeCodePoint(code));
218 return isAsciiDigit(code) || (code >= 'A' && code <= 'F');
221 #if defined LIBO_INTERNAL_ONLY
222 bool isAsciiCanonicHexDigit(char) = delete;
223 bool isAsciiCanonicHexDigit(signed char) = delete;
224 template<typename T> inline bool isAsciiCanonicHexDigit(T code)
225 { return isAsciiCanonicHexDigit(sal_uInt32(code)); }
226 #endif
228 /** Check for ASCII hexadecimal digit character.
230 @param code A Unicode code point.
232 @return True if code is an ASCII hexadecimal digit character (ASCII
233 '0'--'9', 'A'--'F', or 'a'--'f').
235 @since LibreOffice 4.1
237 inline bool isAsciiHexDigit(sal_uInt32 code)
239 assert(isUnicodeCodePoint(code));
240 return isAsciiCanonicHexDigit(code) || (code >= 'a' && code <= 'f');
243 #if defined LIBO_INTERNAL_ONLY
244 bool isAsciiHexDigit(char) = delete;
245 bool isAsciiHexDigit(signed char) = delete;
246 template<typename T> inline bool isAsciiHexDigit(T code)
247 { return isAsciiHexDigit(sal_uInt32(code)); }
248 #endif
250 /** Check for ASCII octal digit character.
252 @param code A Unicode code point.
254 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
256 @since LibreOffice 5.0
258 inline bool isAsciiOctalDigit(sal_uInt32 code)
260 assert(isUnicodeCodePoint(code));
261 return code >= '0' && code <= '7';
264 #if defined LIBO_INTERNAL_ONLY
265 bool isAsciiOctalDigit(char) = delete;
266 bool isAsciiOctalDigit(signed char) = delete;
267 template<typename T> inline bool isAsciiOctalDigit(T code)
268 { return isAsciiOctalDigit(sal_uInt32(code)); }
269 #endif
271 /** Check for ASCII white space character.
273 @param code A Unicode code point.
275 @return True if code is an ASCII white space character as defined by C for
276 isspace in the "C" locale (ASCII ' ', '\\f', '\\n', '\\r', '\\t' '\\v').
278 @since LibreOffice 5.4
280 inline bool isAsciiWhiteSpace(sal_uInt32 code)
282 assert(isUnicodeCodePoint(code));
283 return code == ' ' || code == '\f' || code == '\n' || code == '\r'
284 || code == '\t' || code == '\v';
287 #if defined LIBO_INTERNAL_ONLY
288 bool isAsciiWhiteSpace(char) = delete;
289 bool isAsciiWhiteSpace(signed char) = delete;
290 template<typename T> inline bool isAsciiWhiteSpace(T code)
291 { return isAsciiWhiteSpace(sal_uInt32(code)); }
292 #endif
294 /** Convert a character, if ASCII, to upper case.
296 @param code A Unicode code point.
298 @return code converted to ASCII upper case.
300 @since LibreOffice 4.2
302 inline sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
304 assert(isUnicodeCodePoint(code));
305 return isAsciiLowerCase(code) ? code - 32 : code;
308 #if defined LIBO_INTERNAL_ONLY
309 sal_uInt32 toAsciiUpperCase(char) = delete;
310 sal_uInt32 toAsciiUpperCase(signed char) = delete;
311 template<typename T> inline sal_uInt32 toAsciiUpperCase(T code)
312 { return toAsciiUpperCase(sal_uInt32(code)); }
313 #endif
315 /** Convert a character, if ASCII, to lower case.
317 @param code A Unicode code point.
319 @return code converted to ASCII lower case.
321 @since LibreOffice 4.2
323 inline sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
325 assert(isUnicodeCodePoint(code));
326 return isAsciiUpperCase(code) ? code + 32 : code;
329 #if defined LIBO_INTERNAL_ONLY
330 sal_uInt32 toAsciiLowerCase(char) = delete;
331 sal_uInt32 toAsciiLowerCase(signed char) = delete;
332 template<typename T> inline sal_uInt32 toAsciiLowerCase(T code)
333 { return toAsciiLowerCase(sal_uInt32(code)); }
334 #endif
336 /** Compare two characters ignoring ASCII case.
338 @param code1 A Unicode code point.
340 @param code2 A unicode code point.
342 @return 0 if both code points are equal,
343 < 0 if code1 is less than code2,
344 > 0 if code1 is greater than code2.
346 @since LibreOffice 4.2
348 inline sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
350 assert(isUnicodeCodePoint(code1));
351 assert(isUnicodeCodePoint(code2));
352 return static_cast<sal_Int32>(toAsciiLowerCase(code1))
353 - static_cast<sal_Int32>(toAsciiLowerCase(code2));
356 /// @cond INTERNAL
357 namespace detail {
359 sal_uInt32 const surrogatesHighFirst = 0xD800;
360 sal_uInt32 const surrogatesHighLast = 0xDBFF;
361 sal_uInt32 const surrogatesLowFirst = 0xDC00;
362 sal_uInt32 const surrogatesLowLast = 0xDFFF;
365 /// @endcond
367 /** Check for surrogate.
369 @param code A Unicode code point.
371 @return True if code is a surrogate code point (0xD800--0xDFFF).
373 @since LibreOffice 6.0
375 inline bool isSurrogate(sal_uInt32 code) {
376 assert(isUnicodeCodePoint(code));
377 return code >= detail::surrogatesHighFirst
378 && code <= detail::surrogatesLowLast;
381 /** Check for high surrogate.
383 @param code A Unicode code point.
385 @return True if code is a high surrogate code point (0xD800--0xDBFF).
387 @since LibreOffice 5.0
389 inline bool isHighSurrogate(sal_uInt32 code) {
390 assert(isUnicodeCodePoint(code));
391 return code >= detail::surrogatesHighFirst
392 && code <= detail::surrogatesHighLast;
395 /** Check for low surrogate.
397 @param code A Unicode code point.
399 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
401 @since LibreOffice 5.0
403 inline bool isLowSurrogate(sal_uInt32 code) {
404 assert(isUnicodeCodePoint(code));
405 return code >= detail::surrogatesLowFirst
406 && code <= detail::surrogatesLowLast;
409 /** Get high surrogate half of a non-BMP Unicode code point.
411 @param code A non-BMP Unicode code point.
413 @return The UTF-16 high surrogate half for the give code point.
415 @since LibreOffice 5.0
417 inline sal_Unicode getHighSurrogate(sal_uInt32 code) {
418 assert(isUnicodeCodePoint(code));
419 assert(code >= 0x10000);
420 return static_cast<sal_Unicode>(((code - 0x10000) >> 10) | detail::surrogatesHighFirst);
423 /** Get low surrogate half of a non-BMP Unicode code point.
425 @param code A non-BMP Unicode code point.
427 @return The UTF-16 low surrogate half for the give code point.
429 @since LibreOffice 5.0
431 inline sal_Unicode getLowSurrogate(sal_uInt32 code) {
432 assert(isUnicodeCodePoint(code));
433 assert(code >= 0x10000);
434 return static_cast<sal_Unicode>(((code - 0x10000) & 0x3FF) | detail::surrogatesLowFirst);
437 /** Combine surrogates to form a code point.
439 @param high A high surrogate code point.
441 @param low A low surrogate code point.
443 @return The code point represented by the surrogate pair.
445 @since LibreOffice 5.0
447 inline sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low) {
448 assert(isHighSurrogate(high));
449 assert(isLowSurrogate(low));
450 return ((high - detail::surrogatesHighFirst) << 10)
451 + (low - detail::surrogatesLowFirst) + 0x10000;
454 /** Split a Unicode code point into UTF-16 code units.
456 @param code A Unicode code point.
458 @param output A non-null pointer to an array with space for at least two
459 sal_Unicode UTF-16 code units.
461 @return The number of UTF-16 code units placed into the output (either one
462 or two).
464 @since LibreOffice 5.3
466 inline std::size_t splitSurrogates(sal_uInt32 code, sal_Unicode * output) {
467 assert(isUnicodeCodePoint(code));
468 assert(output != NULL);
469 if (code < 0x10000) {
470 output[0] = code;
471 return 1;
472 } else {
473 output[0] = getHighSurrogate(code);
474 output[1] = getLowSurrogate(code);
475 return 2;
479 /** Check for Unicode scalar value.
481 @param code An integer.
483 @return True if code is a Unicode scalar value.
485 @since LibreOffice 6.0
487 inline bool isUnicodeScalarValue(sal_uInt32 code)
489 return isUnicodeCodePoint(code) && !isSurrogate(code);
494 #endif
496 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */