Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / rtl / character.hxx
blob5801063532da40c02ce6b4202370a0c0426b3efa
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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_RTL_CHARACTER_HXX
25 #define INCLUDED_RTL_CHARACTER_HXX
27 #include "sal/config.h"
29 #include <cassert>
30 #include <cstddef>
32 #include "sal/types.h"
34 #if defined LIBO_INTERNAL_ONLY
35 #include <type_traits>
36 #endif
38 namespace rtl
40 /** Check for Unicode code point.
42 @param code An integer.
44 @return True if code is a Unicode code point.
46 @since LibreOffice 5.2
48 inline SAL_CONSTEXPR bool isUnicodeCodePoint(sal_uInt32 code) { return code <= 0x10FFFF; }
50 /** Check for ASCII character.
52 @param code A Unicode code point.
54 @return True if code is an ASCII character (0x00--0x7F).
56 @since LibreOffice 4.1
58 inline SAL_CONSTEXPR bool isAscii(sal_uInt32 code)
60 assert(isUnicodeCodePoint(code));
61 return code <= 0x7F;
64 #if defined LIBO_INTERNAL_ONLY
65 bool isAscii(char) = delete;
66 bool isAscii(signed char) = delete;
67 template <typename T>
68 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
69 isAscii(T code)
71 return isAscii(sal_uInt32(code));
73 #endif
75 /** Check for ASCII lower case character.
77 @param code A Unicode code point.
79 @return True if code is an ASCII lower case alphabetic character (ASCII
80 'a'--'z').
82 @since LibreOffice 4.1
84 inline SAL_CONSTEXPR bool isAsciiLowerCase(sal_uInt32 code)
86 assert(isUnicodeCodePoint(code));
87 return code >= 'a' && code <= 'z';
90 #if defined LIBO_INTERNAL_ONLY
91 bool isAsciiLowerCase(char) = delete;
92 bool isAsciiLowerCase(signed char) = delete;
93 template <typename T>
94 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
95 isAsciiLowerCase(T code)
97 return isAsciiLowerCase(sal_uInt32(code));
99 #endif
101 /** Check for ASCII upper case character.
103 @param code A Unicode code point.
105 @return True if code is an ASCII upper case alphabetic character (ASCII
106 'A'--'Z').
108 @since LibreOffice 4.1
110 inline SAL_CONSTEXPR bool isAsciiUpperCase(sal_uInt32 code)
112 assert(isUnicodeCodePoint(code));
113 return code >= 'A' && code <= 'Z';
116 #if defined LIBO_INTERNAL_ONLY
117 bool isAsciiUpperCase(char) = delete;
118 bool isAsciiUpperCase(signed char) = delete;
119 template <typename T>
120 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
121 isAsciiUpperCase(T code)
123 return isAsciiUpperCase(sal_uInt32(code));
125 #endif
127 /** Check for ASCII alphabetic character.
129 @param code A Unicode code point.
131 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
132 'a'--'z').
134 @since LibreOffice 4.1
136 inline SAL_CONSTEXPR bool isAsciiAlpha(sal_uInt32 code)
138 assert(isUnicodeCodePoint(code));
139 return isAsciiLowerCase(code) || isAsciiUpperCase(code);
142 #if defined LIBO_INTERNAL_ONLY
143 bool isAsciiAlpha(char) = delete;
144 bool isAsciiAlpha(signed char) = delete;
145 template <typename T>
146 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
147 isAsciiAlpha(T code)
149 return isAsciiAlpha(sal_uInt32(code));
151 #endif
153 /** Check for ASCII digit character.
155 @param code A Unicode code point.
157 @return True if code is an ASCII (decimal) digit character (ASCII
158 '0'--'9').
160 @since LibreOffice 4.1
162 inline SAL_CONSTEXPR bool isAsciiDigit(sal_uInt32 code)
164 assert(isUnicodeCodePoint(code));
165 return code >= '0' && code <= '9';
168 #if defined LIBO_INTERNAL_ONLY
169 bool isAsciiDigit(char) = delete;
170 bool isAsciiDigit(signed char) = delete;
171 template <typename T>
172 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
173 isAsciiDigit(T code)
175 return isAsciiDigit(sal_uInt32(code));
177 #endif
179 /** Check for ASCII alphanumeric character.
181 @param code A Unicode code point.
183 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
184 'A'--'Z', or 'a'--'z').
186 @since LibreOffice 4.1
188 inline SAL_CONSTEXPR bool isAsciiAlphanumeric(sal_uInt32 code)
190 assert(isUnicodeCodePoint(code));
191 return isAsciiDigit(code) || isAsciiAlpha(code);
194 #if defined LIBO_INTERNAL_ONLY
195 bool isAsciiAlphanumeric(char) = delete;
196 bool isAsciiAlphanumeric(signed char) = delete;
197 template <typename T>
198 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
199 isAsciiAlphanumeric(T code)
201 return isAsciiAlphanumeric(sal_uInt32(code));
203 #endif
205 /** Check for ASCII canonic hexadecimal digit character.
207 @param code A Unicode code point.
209 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
210 digit character (ASCII '0'--'9' or 'A'--'F').
212 @since LibreOffice 4.1
214 inline SAL_CONSTEXPR bool isAsciiCanonicHexDigit(sal_uInt32 code)
216 assert(isUnicodeCodePoint(code));
217 return isAsciiDigit(code) || (code >= 'A' && code <= 'F');
220 #if defined LIBO_INTERNAL_ONLY
221 bool isAsciiCanonicHexDigit(char) = delete;
222 bool isAsciiCanonicHexDigit(signed char) = delete;
223 template <typename T>
224 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
225 isAsciiCanonicHexDigit(T code)
227 return isAsciiCanonicHexDigit(sal_uInt32(code));
229 #endif
231 /** Check for ASCII hexadecimal digit character.
233 @param code A Unicode code point.
235 @return True if code is an ASCII hexadecimal digit character (ASCII
236 '0'--'9', 'A'--'F', or 'a'--'f').
238 @since LibreOffice 4.1
240 inline SAL_CONSTEXPR bool isAsciiHexDigit(sal_uInt32 code)
242 assert(isUnicodeCodePoint(code));
243 return isAsciiCanonicHexDigit(code) || (code >= 'a' && code <= 'f');
246 #if defined LIBO_INTERNAL_ONLY
247 bool isAsciiHexDigit(char) = delete;
248 bool isAsciiHexDigit(signed char) = delete;
249 template <typename T>
250 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
251 isAsciiHexDigit(T code)
253 return isAsciiHexDigit(sal_uInt32(code));
255 #endif
257 /** Check for ASCII octal digit character.
259 @param code A Unicode code point.
261 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
263 @since LibreOffice 5.0
265 inline SAL_CONSTEXPR bool isAsciiOctalDigit(sal_uInt32 code)
267 assert(isUnicodeCodePoint(code));
268 return code >= '0' && code <= '7';
271 #if defined LIBO_INTERNAL_ONLY
272 bool isAsciiOctalDigit(char) = delete;
273 bool isAsciiOctalDigit(signed char) = delete;
274 template <typename T>
275 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
276 isAsciiOctalDigit(T code)
278 return isAsciiOctalDigit(sal_uInt32(code));
280 #endif
282 /** Check for ASCII white space character.
284 @param code A Unicode code point.
286 @return True if code is an ASCII white space character as defined by C for
287 isspace in the "C" locale (ASCII ' ', '\\f', '\\n', '\\r', '\\t' '\\v').
289 @since LibreOffice 5.4
291 inline SAL_CONSTEXPR bool isAsciiWhiteSpace(sal_uInt32 code)
293 assert(isUnicodeCodePoint(code));
294 return code == ' ' || code == '\f' || code == '\n' || code == '\r' || code == '\t'
295 || code == '\v';
298 #if defined LIBO_INTERNAL_ONLY
299 bool isAsciiWhiteSpace(char) = delete;
300 bool isAsciiWhiteSpace(signed char) = delete;
301 template <typename T>
302 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32), bool>
303 isAsciiWhiteSpace(T code)
305 return isAsciiWhiteSpace(sal_uInt32(code));
307 #endif
309 /** Convert a character, if ASCII, to upper case.
311 @param code A Unicode code point.
313 @return code converted to ASCII upper case.
315 @since LibreOffice 4.2
317 inline SAL_CONSTEXPR sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
319 assert(isUnicodeCodePoint(code));
320 return isAsciiLowerCase(code) ? code - 32 : code;
323 #if defined LIBO_INTERNAL_ONLY
324 sal_uInt32 toAsciiUpperCase(char) = delete;
325 sal_uInt32 toAsciiUpperCase(signed char) = delete;
326 template <typename T>
327 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32),
328 sal_uInt32>
329 toAsciiUpperCase(T code)
331 return toAsciiUpperCase(sal_uInt32(code));
333 #endif
335 /** Convert a character, if ASCII, to lower case.
337 @param code A Unicode code point.
339 @return code converted to ASCII lower case.
341 @since LibreOffice 4.2
343 inline SAL_CONSTEXPR sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
345 assert(isUnicodeCodePoint(code));
346 return isAsciiUpperCase(code) ? code + 32 : code;
349 #if defined LIBO_INTERNAL_ONLY
350 sal_uInt32 toAsciiLowerCase(char) = delete;
351 sal_uInt32 toAsciiLowerCase(signed char) = delete;
352 template <typename T>
353 inline constexpr std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= sizeof(sal_uInt32),
354 sal_uInt32>
355 toAsciiLowerCase(T code)
357 return toAsciiLowerCase(sal_uInt32(code));
359 #endif
361 /** Compare two characters ignoring ASCII case.
363 @param code1 A Unicode code point.
365 @param code2 A unicode code point.
367 @return 0 if both code points are equal,
368 < 0 if code1 is less than code2,
369 > 0 if code1 is greater than code2.
371 @since LibreOffice 4.2
373 inline SAL_CONSTEXPR sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
375 assert(isUnicodeCodePoint(code1));
376 assert(isUnicodeCodePoint(code2));
377 return static_cast<sal_Int32>(toAsciiLowerCase(code1))
378 - static_cast<sal_Int32>(toAsciiLowerCase(code2));
381 /// @cond INTERNAL
382 namespace detail
384 sal_uInt32 const surrogatesHighFirst = 0xD800;
385 sal_uInt32 const surrogatesHighLast = 0xDBFF;
386 sal_uInt32 const surrogatesLowFirst = 0xDC00;
387 sal_uInt32 const surrogatesLowLast = 0xDFFF;
389 /// @endcond
391 /** Check for surrogate.
393 @param code A Unicode code point.
395 @return True if code is a surrogate code point (0xD800--0xDFFF).
397 @since LibreOffice 6.0
399 inline SAL_CONSTEXPR bool isSurrogate(sal_uInt32 code)
401 assert(isUnicodeCodePoint(code));
402 return code >= detail::surrogatesHighFirst && code <= detail::surrogatesLowLast;
405 /** Check for high surrogate.
407 @param code A Unicode code point.
409 @return True if code is a high surrogate code point (0xD800--0xDBFF).
411 @since LibreOffice 5.0
413 inline SAL_CONSTEXPR bool isHighSurrogate(sal_uInt32 code)
415 assert(isUnicodeCodePoint(code));
416 return code >= detail::surrogatesHighFirst && code <= detail::surrogatesHighLast;
419 /** Check for low surrogate.
421 @param code A Unicode code point.
423 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
425 @since LibreOffice 5.0
427 inline SAL_CONSTEXPR bool isLowSurrogate(sal_uInt32 code)
429 assert(isUnicodeCodePoint(code));
430 return code >= detail::surrogatesLowFirst && code <= detail::surrogatesLowLast;
433 /** Get high surrogate half of a non-BMP Unicode code point.
435 @param code A non-BMP Unicode code point.
437 @return The UTF-16 high surrogate half for the give code point.
439 @since LibreOffice 5.0
441 inline SAL_CONSTEXPR sal_Unicode getHighSurrogate(sal_uInt32 code)
443 assert(isUnicodeCodePoint(code));
444 assert(code >= 0x10000);
445 return static_cast<sal_Unicode>(((code - 0x10000) >> 10) | detail::surrogatesHighFirst);
448 /** Get low surrogate half of a non-BMP Unicode code point.
450 @param code A non-BMP Unicode code point.
452 @return The UTF-16 low surrogate half for the give code point.
454 @since LibreOffice 5.0
456 inline SAL_CONSTEXPR sal_Unicode getLowSurrogate(sal_uInt32 code)
458 assert(isUnicodeCodePoint(code));
459 assert(code >= 0x10000);
460 return static_cast<sal_Unicode>(((code - 0x10000) & 0x3FF) | detail::surrogatesLowFirst);
463 /** Combine surrogates to form a code point.
465 @param high A high surrogate code point.
467 @param low A low surrogate code point.
469 @return The code point represented by the surrogate pair.
471 @since LibreOffice 5.0
473 inline SAL_CONSTEXPR sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low)
475 assert(isHighSurrogate(high));
476 assert(isLowSurrogate(low));
477 return ((high - detail::surrogatesHighFirst) << 10) + (low - detail::surrogatesLowFirst)
478 + 0x10000;
481 /** Split a Unicode code point into UTF-16 code units.
483 @param code A Unicode code point.
485 @param output A non-null pointer to an array with space for at least two
486 sal_Unicode UTF-16 code units.
488 @return The number of UTF-16 code units placed into the output (either one
489 or two).
491 @since LibreOffice 5.3
493 inline SAL_CONSTEXPR std::size_t splitSurrogates(sal_uInt32 code, sal_Unicode* output)
495 assert(isUnicodeCodePoint(code));
496 assert(output != NULL);
497 if (code < 0x10000)
499 output[0] = code;
500 return 1;
502 else
504 output[0] = getHighSurrogate(code);
505 output[1] = getLowSurrogate(code);
506 return 2;
510 /** Check for Unicode scalar value.
512 @param code An integer.
514 @return True if code is a Unicode scalar value.
516 @since LibreOffice 6.0
518 inline SAL_CONSTEXPR bool isUnicodeScalarValue(sal_uInt32 code)
520 return isUnicodeCodePoint(code) && !isSurrogate(code);
524 #endif
526 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */