1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
32 #include "sal/types.h"
36 /** Check for Unicode code point.
38 @param code An integer.
40 @return True if code is a Unicode code point.
42 @since LibreOffice 5.2
44 inline SAL_CONSTEXPR
bool isUnicodeCodePoint(sal_uInt32 code
) { 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 SAL_CONSTEXPR
bool isAscii(sal_uInt32 code
)
56 assert(isUnicodeCodePoint(code
));
60 #if defined LIBO_INTERNAL_ONLY
61 bool isAscii(char) = delete;
62 bool isAscii(signed char) = delete;
63 template <typename T
> inline constexpr bool isAscii(T code
) { return isAscii(sal_uInt32(code
)); }
66 /** Check for ASCII lower case character.
68 @param code A Unicode code point.
70 @return True if code is an ASCII lower case alphabetic character (ASCII
73 @since LibreOffice 4.1
75 inline SAL_CONSTEXPR
bool isAsciiLowerCase(sal_uInt32 code
)
77 assert(isUnicodeCodePoint(code
));
78 return code
>= 'a' && code
<= 'z';
81 #if defined LIBO_INTERNAL_ONLY
82 bool isAsciiLowerCase(char) = delete;
83 bool isAsciiLowerCase(signed char) = delete;
84 template <typename T
> inline constexpr bool isAsciiLowerCase(T code
)
86 return isAsciiLowerCase(sal_uInt32(code
));
90 /** Check for ASCII upper case character.
92 @param code A Unicode code point.
94 @return True if code is an ASCII upper case alphabetic character (ASCII
97 @since LibreOffice 4.1
99 inline SAL_CONSTEXPR
bool isAsciiUpperCase(sal_uInt32 code
)
101 assert(isUnicodeCodePoint(code
));
102 return code
>= 'A' && code
<= 'Z';
105 #if defined LIBO_INTERNAL_ONLY
106 bool isAsciiUpperCase(char) = delete;
107 bool isAsciiUpperCase(signed char) = delete;
108 template <typename T
> inline constexpr bool isAsciiUpperCase(T code
)
110 return isAsciiUpperCase(sal_uInt32(code
));
114 /** Check for ASCII alphabetic character.
116 @param code A Unicode code point.
118 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
121 @since LibreOffice 4.1
123 inline SAL_CONSTEXPR
bool isAsciiAlpha(sal_uInt32 code
)
125 assert(isUnicodeCodePoint(code
));
126 return isAsciiLowerCase(code
) || isAsciiUpperCase(code
);
129 #if defined LIBO_INTERNAL_ONLY
130 bool isAsciiAlpha(char) = delete;
131 bool isAsciiAlpha(signed char) = delete;
132 template <typename T
> inline constexpr bool isAsciiAlpha(T code
)
134 return isAsciiAlpha(sal_uInt32(code
));
138 /** Check for ASCII digit character.
140 @param code A Unicode code point.
142 @return True if code is an ASCII (decimal) digit character (ASCII
145 @since LibreOffice 4.1
147 inline SAL_CONSTEXPR
bool isAsciiDigit(sal_uInt32 code
)
149 assert(isUnicodeCodePoint(code
));
150 return code
>= '0' && code
<= '9';
153 #if defined LIBO_INTERNAL_ONLY
154 bool isAsciiDigit(char) = delete;
155 bool isAsciiDigit(signed char) = delete;
156 template <typename T
> inline constexpr bool isAsciiDigit(T code
)
158 return isAsciiDigit(sal_uInt32(code
));
162 /** Check for ASCII alphanumeric character.
164 @param code A Unicode code point.
166 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
167 'A'--'Z', or 'a'--'z').
169 @since LibreOffice 4.1
171 inline SAL_CONSTEXPR
bool isAsciiAlphanumeric(sal_uInt32 code
)
173 assert(isUnicodeCodePoint(code
));
174 return isAsciiDigit(code
) || isAsciiAlpha(code
);
177 #if defined LIBO_INTERNAL_ONLY
178 bool isAsciiAlphanumeric(char) = delete;
179 bool isAsciiAlphanumeric(signed char) = delete;
180 template <typename T
> inline constexpr bool isAsciiAlphanumeric(T code
)
182 return isAsciiAlphanumeric(sal_uInt32(code
));
186 /** Check for ASCII canonic hexadecimal digit character.
188 @param code A Unicode code point.
190 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
191 digit character (ASCII '0'--'9' or 'A'--'F').
193 @since LibreOffice 4.1
195 inline SAL_CONSTEXPR
bool isAsciiCanonicHexDigit(sal_uInt32 code
)
197 assert(isUnicodeCodePoint(code
));
198 return isAsciiDigit(code
) || (code
>= 'A' && code
<= 'F');
201 #if defined LIBO_INTERNAL_ONLY
202 bool isAsciiCanonicHexDigit(char) = delete;
203 bool isAsciiCanonicHexDigit(signed char) = delete;
204 template <typename T
> inline constexpr bool isAsciiCanonicHexDigit(T code
)
206 return isAsciiCanonicHexDigit(sal_uInt32(code
));
210 /** Check for ASCII hexadecimal digit character.
212 @param code A Unicode code point.
214 @return True if code is an ASCII hexadecimal digit character (ASCII
215 '0'--'9', 'A'--'F', or 'a'--'f').
217 @since LibreOffice 4.1
219 inline SAL_CONSTEXPR
bool isAsciiHexDigit(sal_uInt32 code
)
221 assert(isUnicodeCodePoint(code
));
222 return isAsciiCanonicHexDigit(code
) || (code
>= 'a' && code
<= 'f');
225 #if defined LIBO_INTERNAL_ONLY
226 bool isAsciiHexDigit(char) = delete;
227 bool isAsciiHexDigit(signed char) = delete;
228 template <typename T
> inline constexpr bool isAsciiHexDigit(T code
)
230 return isAsciiHexDigit(sal_uInt32(code
));
234 /** Check for ASCII octal digit character.
236 @param code A Unicode code point.
238 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
240 @since LibreOffice 5.0
242 inline SAL_CONSTEXPR
bool isAsciiOctalDigit(sal_uInt32 code
)
244 assert(isUnicodeCodePoint(code
));
245 return code
>= '0' && code
<= '7';
248 #if defined LIBO_INTERNAL_ONLY
249 bool isAsciiOctalDigit(char) = delete;
250 bool isAsciiOctalDigit(signed char) = delete;
251 template <typename T
> inline constexpr bool isAsciiOctalDigit(T code
)
253 return isAsciiOctalDigit(sal_uInt32(code
));
257 /** Check for ASCII white space character.
259 @param code A Unicode code point.
261 @return True if code is an ASCII white space character as defined by C for
262 isspace in the "C" locale (ASCII ' ', '\\f', '\\n', '\\r', '\\t' '\\v').
264 @since LibreOffice 5.4
266 inline SAL_CONSTEXPR
bool isAsciiWhiteSpace(sal_uInt32 code
)
268 assert(isUnicodeCodePoint(code
));
269 return code
== ' ' || code
== '\f' || code
== '\n' || code
== '\r' || code
== '\t'
273 #if defined LIBO_INTERNAL_ONLY
274 bool isAsciiWhiteSpace(char) = delete;
275 bool isAsciiWhiteSpace(signed char) = delete;
276 template <typename T
> inline constexpr bool isAsciiWhiteSpace(T code
)
278 return isAsciiWhiteSpace(sal_uInt32(code
));
282 /** Convert a character, if ASCII, to upper case.
284 @param code A Unicode code point.
286 @return code converted to ASCII upper case.
288 @since LibreOffice 4.2
290 inline SAL_CONSTEXPR sal_uInt32
toAsciiUpperCase(sal_uInt32 code
)
292 assert(isUnicodeCodePoint(code
));
293 return isAsciiLowerCase(code
) ? code
- 32 : code
;
296 #if defined LIBO_INTERNAL_ONLY
297 sal_uInt32
toAsciiUpperCase(char) = delete;
298 sal_uInt32
toAsciiUpperCase(signed char) = delete;
299 template <typename T
> inline constexpr sal_uInt32
toAsciiUpperCase(T code
)
301 return toAsciiUpperCase(sal_uInt32(code
));
305 /** Convert a character, if ASCII, to lower case.
307 @param code A Unicode code point.
309 @return code converted to ASCII lower case.
311 @since LibreOffice 4.2
313 inline SAL_CONSTEXPR sal_uInt32
toAsciiLowerCase(sal_uInt32 code
)
315 assert(isUnicodeCodePoint(code
));
316 return isAsciiUpperCase(code
) ? code
+ 32 : code
;
319 #if defined LIBO_INTERNAL_ONLY
320 sal_uInt32
toAsciiLowerCase(char) = delete;
321 sal_uInt32
toAsciiLowerCase(signed char) = delete;
322 template <typename T
> inline constexpr sal_uInt32
toAsciiLowerCase(T code
)
324 return toAsciiLowerCase(sal_uInt32(code
));
328 /** Compare two characters ignoring ASCII case.
330 @param code1 A Unicode code point.
332 @param code2 A unicode code point.
334 @return 0 if both code points are equal,
335 < 0 if code1 is less than code2,
336 > 0 if code1 is greater than code2.
338 @since LibreOffice 4.2
340 inline SAL_CONSTEXPR sal_Int32
compareIgnoreAsciiCase(sal_uInt32 code1
, sal_uInt32 code2
)
342 assert(isUnicodeCodePoint(code1
));
343 assert(isUnicodeCodePoint(code2
));
344 return static_cast<sal_Int32
>(toAsciiLowerCase(code1
))
345 - static_cast<sal_Int32
>(toAsciiLowerCase(code2
));
351 sal_uInt32
const surrogatesHighFirst
= 0xD800;
352 sal_uInt32
const surrogatesHighLast
= 0xDBFF;
353 sal_uInt32
const surrogatesLowFirst
= 0xDC00;
354 sal_uInt32
const surrogatesLowLast
= 0xDFFF;
358 /** Check for surrogate.
360 @param code A Unicode code point.
362 @return True if code is a surrogate code point (0xD800--0xDFFF).
364 @since LibreOffice 6.0
366 inline SAL_CONSTEXPR
bool isSurrogate(sal_uInt32 code
)
368 assert(isUnicodeCodePoint(code
));
369 return code
>= detail::surrogatesHighFirst
&& code
<= detail::surrogatesLowLast
;
372 /** Check for high surrogate.
374 @param code A Unicode code point.
376 @return True if code is a high surrogate code point (0xD800--0xDBFF).
378 @since LibreOffice 5.0
380 inline SAL_CONSTEXPR
bool isHighSurrogate(sal_uInt32 code
)
382 assert(isUnicodeCodePoint(code
));
383 return code
>= detail::surrogatesHighFirst
&& code
<= detail::surrogatesHighLast
;
386 /** Check for low surrogate.
388 @param code A Unicode code point.
390 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
392 @since LibreOffice 5.0
394 inline SAL_CONSTEXPR
bool isLowSurrogate(sal_uInt32 code
)
396 assert(isUnicodeCodePoint(code
));
397 return code
>= detail::surrogatesLowFirst
&& code
<= detail::surrogatesLowLast
;
400 /** Get high surrogate half of a non-BMP Unicode code point.
402 @param code A non-BMP Unicode code point.
404 @return The UTF-16 high surrogate half for the give code point.
406 @since LibreOffice 5.0
408 inline SAL_CONSTEXPR sal_Unicode
getHighSurrogate(sal_uInt32 code
)
410 assert(isUnicodeCodePoint(code
));
411 assert(code
>= 0x10000);
412 return static_cast<sal_Unicode
>(((code
- 0x10000) >> 10) | detail::surrogatesHighFirst
);
415 /** Get low surrogate half of a non-BMP Unicode code point.
417 @param code A non-BMP Unicode code point.
419 @return The UTF-16 low surrogate half for the give code point.
421 @since LibreOffice 5.0
423 inline SAL_CONSTEXPR sal_Unicode
getLowSurrogate(sal_uInt32 code
)
425 assert(isUnicodeCodePoint(code
));
426 assert(code
>= 0x10000);
427 return static_cast<sal_Unicode
>(((code
- 0x10000) & 0x3FF) | detail::surrogatesLowFirst
);
430 /** Combine surrogates to form a code point.
432 @param high A high surrogate code point.
434 @param low A low surrogate code point.
436 @return The code point represented by the surrogate pair.
438 @since LibreOffice 5.0
440 inline SAL_CONSTEXPR sal_uInt32
combineSurrogates(sal_uInt32 high
, sal_uInt32 low
)
442 assert(isHighSurrogate(high
));
443 assert(isLowSurrogate(low
));
444 return ((high
- detail::surrogatesHighFirst
) << 10) + (low
- detail::surrogatesLowFirst
)
448 /** Split a Unicode code point into UTF-16 code units.
450 @param code A Unicode code point.
452 @param output A non-null pointer to an array with space for at least two
453 sal_Unicode UTF-16 code units.
455 @return The number of UTF-16 code units placed into the output (either one
458 @since LibreOffice 5.3
460 inline SAL_CONSTEXPR
std::size_t splitSurrogates(sal_uInt32 code
, sal_Unicode
* output
)
462 assert(isUnicodeCodePoint(code
));
463 assert(output
!= NULL
);
471 output
[0] = getHighSurrogate(code
);
472 output
[1] = getLowSurrogate(code
);
477 /** Check for Unicode scalar value.
479 @param code An integer.
481 @return True if code is a Unicode scalar value.
483 @since LibreOffice 6.0
485 inline SAL_CONSTEXPR
bool isUnicodeScalarValue(sal_uInt32 code
)
487 return isUnicodeCodePoint(code
) && !isSurrogate(code
);
493 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */