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 .
20 #ifndef INCLUDED_RTL_CHARACTER_HXX
21 #define INCLUDED_RTL_CHARACTER_HXX
23 #include "sal/config.h"
28 #include "sal/types.h"
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
));
60 #if defined LIBO_INTERNAL_ONLY
61 bool isAscii(char) = delete;
62 bool isAscii(signed char) = delete;
63 template<typename T
> inline bool isAscii(T code
)
64 { return isAscii(sal_uInt32(code
)); }
67 /** Check for ASCII lower case character.
69 @param code A Unicode code point.
71 @return True if code is an ASCII lower case alphabetic character (ASCII
74 @since LibreOffice 4.1
76 inline bool isAsciiLowerCase(sal_uInt32 code
)
78 assert(isUnicodeCodePoint(code
));
79 return code
>= 'a' && code
<= 'z';
82 #if defined LIBO_INTERNAL_ONLY
83 bool isAsciiLowerCase(char) = delete;
84 bool isAsciiLowerCase(signed char) = delete;
85 template<typename T
> inline bool isAsciiLowerCase(T code
)
86 { return isAsciiLowerCase(sal_uInt32(code
)); }
89 /** Check for ASCII upper case character.
91 @param code A Unicode code point.
93 @return True if code is an ASCII upper case alphabetic character (ASCII
96 @since LibreOffice 4.1
98 inline bool isAsciiUpperCase(sal_uInt32 code
)
100 assert(isUnicodeCodePoint(code
));
101 return code
>= 'A' && code
<= 'Z';
104 #if defined LIBO_INTERNAL_ONLY
105 bool isAsciiUpperCase(char) = delete;
106 bool isAsciiUpperCase(signed char) = delete;
107 template<typename T
> inline bool isAsciiUpperCase(T code
)
108 { return isAsciiUpperCase(sal_uInt32(code
)); }
111 /** Check for ASCII alphabetic character.
113 @param code A Unicode code point.
115 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
118 @since LibreOffice 4.1
120 inline bool isAsciiAlpha(sal_uInt32 code
)
122 assert(isUnicodeCodePoint(code
));
123 return isAsciiLowerCase(code
) || isAsciiUpperCase(code
);
126 #if defined LIBO_INTERNAL_ONLY
127 bool isAsciiAlpha(char) = delete;
128 bool isAsciiAlpha(signed char) = delete;
129 template<typename T
> inline bool isAsciiAlpha(T code
)
130 { return isAsciiAlpha(sal_uInt32(code
)); }
133 /** Check for ASCII digit character.
135 @param code A Unicode code point.
137 @return True if code is an ASCII (decimal) digit character (ASCII
140 @since LibreOffice 4.1
142 inline bool isAsciiDigit(sal_uInt32 code
)
144 assert(isUnicodeCodePoint(code
));
145 return code
>= '0' && code
<= '9';
148 #if defined LIBO_INTERNAL_ONLY
149 bool isAsciiDigit(char) = delete;
150 bool isAsciiDigit(signed char) = delete;
151 template<typename T
> inline bool isAsciiDigit(T code
)
152 { return isAsciiDigit(sal_uInt32(code
)); }
155 /** Check for ASCII alphanumeric character.
157 @param code A Unicode code point.
159 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
160 'A'--'Z', or 'a'--'z').
162 @since LibreOffice 4.1
164 inline bool isAsciiAlphanumeric(sal_uInt32 code
)
166 assert(isUnicodeCodePoint(code
));
167 return isAsciiDigit(code
) || isAsciiAlpha(code
);
170 #if defined LIBO_INTERNAL_ONLY
171 bool isAsciiAlphanumeric(char) = delete;
172 bool isAsciiAlphanumeric(signed char) = delete;
173 template<typename T
> inline bool isAsciiAlphanumeric(T code
)
174 { return isAsciiAlphanumeric(sal_uInt32(code
)); }
177 /** Check for ASCII canonic hexadecimal digit character.
179 @param code A Unicode code point.
181 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
182 digit character (ASCII '0'--'9' or 'A'--'F').
184 @since LibreOffice 4.1
186 inline bool isAsciiCanonicHexDigit(sal_uInt32 code
)
188 assert(isUnicodeCodePoint(code
));
189 return isAsciiDigit(code
) || (code
>= 'A' && code
<= 'F');
192 #if defined LIBO_INTERNAL_ONLY
193 bool isAsciiCanonicHexDigit(char) = delete;
194 bool isAsciiCanonicHexDigit(signed char) = delete;
195 template<typename T
> inline bool isAsciiCanonicHexDigit(T code
)
196 { return isAsciiCanonicHexDigit(sal_uInt32(code
)); }
199 /** Check for ASCII hexadecimal digit character.
201 @param code A Unicode code point.
203 @return True if code is an ASCII hexadecimal digit character (ASCII
204 '0'--'9', 'A'--'F', or 'a'--'f').
206 @since LibreOffice 4.1
208 inline bool isAsciiHexDigit(sal_uInt32 code
)
210 assert(isUnicodeCodePoint(code
));
211 return isAsciiCanonicHexDigit(code
) || (code
>= 'a' && code
<= 'f');
214 #if defined LIBO_INTERNAL_ONLY
215 bool isAsciiHexDigit(char) = delete;
216 bool isAsciiHexDigit(signed char) = delete;
217 template<typename T
> inline bool isAsciiHexDigit(T code
)
218 { return isAsciiHexDigit(sal_uInt32(code
)); }
221 /** Check for ASCII octal digit character.
223 @param code A Unicode code point.
225 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
227 @since LibreOffice 5.0
229 inline bool isAsciiOctalDigit(sal_uInt32 code
)
231 assert(isUnicodeCodePoint(code
));
232 return code
>= '0' && code
<= '7';
235 #if defined LIBO_INTERNAL_ONLY
236 bool isAsciiOctalDigit(char) = delete;
237 bool isAsciiOctalDigit(signed char) = delete;
238 template<typename T
> inline bool isAsciiOctalDigit(T code
)
239 { return isAsciiOctalDigit(sal_uInt32(code
)); }
242 /** Check for ASCII white space character.
244 @param code A Unicode code point.
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').
249 @since LibreOffice 5.4
251 inline bool isAsciiWhiteSpace(sal_uInt32 code
)
253 assert(isUnicodeCodePoint(code
));
254 return code
== ' ' || code
== '\f' || code
== '\n' || code
== '\r'
255 || code
== '\t' || code
== '\v';
258 #if defined LIBO_INTERNAL_ONLY
259 bool isAsciiWhiteSpace(char) = delete;
260 bool isAsciiWhiteSpace(signed char) = delete;
261 template<typename T
> inline bool isAsciiWhiteSpace(T code
)
262 { return isAsciiWhiteSpace(sal_uInt32(code
)); }
265 /** Convert a character, if ASCII, to upper case.
267 @param code A Unicode code point.
269 @return code converted to ASCII upper case.
271 @since LibreOffice 4.2
273 inline sal_uInt32
toAsciiUpperCase(sal_uInt32 code
)
275 assert(isUnicodeCodePoint(code
));
276 return isAsciiLowerCase(code
) ? code
- 32 : code
;
279 #if defined LIBO_INTERNAL_ONLY
280 sal_uInt32
toAsciiUpperCase(char) = delete;
281 sal_uInt32
toAsciiUpperCase(signed char) = delete;
282 template<typename T
> inline sal_uInt32
toAsciiUpperCase(T code
)
283 { return toAsciiUpperCase(sal_uInt32(code
)); }
286 /** Convert a character, if ASCII, to lower case.
288 @param code A Unicode code point.
290 @return code converted to ASCII lower case.
292 @since LibreOffice 4.2
294 inline sal_uInt32
toAsciiLowerCase(sal_uInt32 code
)
296 assert(isUnicodeCodePoint(code
));
297 return isAsciiUpperCase(code
) ? code
+ 32 : code
;
300 #if defined LIBO_INTERNAL_ONLY
301 sal_uInt32
toAsciiLowerCase(char) = delete;
302 sal_uInt32
toAsciiLowerCase(signed char) = delete;
303 template<typename T
> inline sal_uInt32
toAsciiLowerCase(T code
)
304 { return toAsciiLowerCase(sal_uInt32(code
)); }
307 /** Compare two characters ignoring ASCII case.
309 @param code1 A Unicode code point.
311 @param code2 A unicode code point.
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.
317 @since LibreOffice 4.2
319 inline sal_Int32
compareIgnoreAsciiCase(sal_uInt32 code1
, sal_uInt32 code2
)
321 assert(isUnicodeCodePoint(code1
));
322 assert(isUnicodeCodePoint(code2
));
323 return static_cast<sal_Int32
>(toAsciiLowerCase(code1
))
324 - static_cast<sal_Int32
>(toAsciiLowerCase(code2
));
330 sal_uInt32
const surrogatesHighFirst
= 0xD800;
331 sal_uInt32
const surrogatesHighLast
= 0xDBFF;
332 sal_uInt32
const surrogatesLowFirst
= 0xDC00;
333 sal_uInt32
const surrogatesLowLast
= 0xDFFF;
338 /** Check for surrogate.
340 @param code A Unicode code point.
342 @return True if code is a surrogate code point (0xD800--0xDFFF).
344 @since LibreOffice 6.0
346 inline bool isSurrogate(sal_uInt32 code
) {
347 assert(isUnicodeCodePoint(code
));
348 return code
>= detail::surrogatesHighFirst
349 && code
<= detail::surrogatesLowLast
;
352 /** Check for high surrogate.
354 @param code A Unicode code point.
356 @return True if code is a high surrogate code point (0xD800--0xDBFF).
358 @since LibreOffice 5.0
360 inline bool isHighSurrogate(sal_uInt32 code
) {
361 assert(isUnicodeCodePoint(code
));
362 return code
>= detail::surrogatesHighFirst
363 && code
<= detail::surrogatesHighLast
;
366 /** Check for low surrogate.
368 @param code A Unicode code point.
370 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
372 @since LibreOffice 5.0
374 inline bool isLowSurrogate(sal_uInt32 code
) {
375 assert(isUnicodeCodePoint(code
));
376 return code
>= detail::surrogatesLowFirst
377 && code
<= detail::surrogatesLowLast
;
380 /** Get high surrogate half of a non-BMP Unicode code point.
382 @param code A non-BMP Unicode code point.
384 @return The UTF-16 high surrogate half for the give code point.
386 @since LibreOffice 5.0
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
);
394 /** Get low surrogate half of a non-BMP Unicode code point.
396 @param code A non-BMP Unicode code point.
398 @return The UTF-16 low surrogate half for the give code point.
400 @since LibreOffice 5.0
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
);
408 /** Combine surrogates to form a code point.
410 @param high A high surrogate code point.
412 @param low A low surrogate code point.
414 @return The code point represented by the surrogate pair.
416 @since LibreOffice 5.0
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;
425 /** Split a Unicode code point into UTF-16 code units.
427 @param code A Unicode code point.
429 @param output A non-null pointer to an array with space for at least two
430 sal_Unicode UTF-16 code units.
432 @return The number of UTF-16 code units placed into the output (either one
435 @since LibreOffice 5.3
437 inline std::size_t splitSurrogates(sal_uInt32 code
, sal_Unicode
* output
) {
438 assert(isUnicodeCodePoint(code
));
439 assert(output
!= NULL
);
440 if (code
< 0x10000) {
444 output
[0] = getHighSurrogate(code
);
445 output
[1] = getLowSurrogate(code
);
450 /** Check for Unicode scalar value.
452 @param code An integer.
454 @return True if code is a Unicode scalar value.
456 @since LibreOffice 6.0
458 inline bool isUnicodeScalarValue(sal_uInt32 code
)
460 return isUnicodeCodePoint(code
) && !isSurrogate(code
);
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */