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"
32 /** Check for Unicode code point.
34 @param code An integer.
36 @return True if code is a Unicode code point.
38 @since LibreOffice 5.2
40 inline bool isUnicodeCodePoint(sal_uInt32 code
) { return code
<= 0x10FFFF; }
42 /** Check for ASCII character.
44 @param code A Unicode code point.
46 @return True if code is an ASCII character (0x00--0x7F).
48 @since LibreOffice 4.1
50 inline bool isAscii(sal_uInt32 code
)
52 assert(isUnicodeCodePoint(code
));
56 #if defined LIBO_INTERNAL_ONLY
57 bool isAscii(char) = delete;
58 bool isAscii(signed char) = delete;
59 template <typename T
> inline bool isAscii(T code
) { return isAscii(sal_uInt32(code
)); }
62 /** Check for ASCII lower case character.
64 @param code A Unicode code point.
66 @return True if code is an ASCII lower case alphabetic character (ASCII
69 @since LibreOffice 4.1
71 inline bool isAsciiLowerCase(sal_uInt32 code
)
73 assert(isUnicodeCodePoint(code
));
74 return code
>= 'a' && code
<= 'z';
77 #if defined LIBO_INTERNAL_ONLY
78 bool isAsciiLowerCase(char) = delete;
79 bool isAsciiLowerCase(signed char) = delete;
80 template <typename T
> inline bool isAsciiLowerCase(T code
)
82 return isAsciiLowerCase(sal_uInt32(code
));
86 /** Check for ASCII upper case character.
88 @param code A Unicode code point.
90 @return True if code is an ASCII upper case alphabetic character (ASCII
93 @since LibreOffice 4.1
95 inline bool isAsciiUpperCase(sal_uInt32 code
)
97 assert(isUnicodeCodePoint(code
));
98 return code
>= 'A' && code
<= 'Z';
101 #if defined LIBO_INTERNAL_ONLY
102 bool isAsciiUpperCase(char) = delete;
103 bool isAsciiUpperCase(signed char) = delete;
104 template <typename T
> inline bool isAsciiUpperCase(T code
)
106 return isAsciiUpperCase(sal_uInt32(code
));
110 /** Check for ASCII alphabetic character.
112 @param code A Unicode code point.
114 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
117 @since LibreOffice 4.1
119 inline bool isAsciiAlpha(sal_uInt32 code
)
121 assert(isUnicodeCodePoint(code
));
122 return isAsciiLowerCase(code
) || isAsciiUpperCase(code
);
125 #if defined LIBO_INTERNAL_ONLY
126 bool isAsciiAlpha(char) = delete;
127 bool isAsciiAlpha(signed char) = delete;
128 template <typename T
> inline bool isAsciiAlpha(T code
) { return isAsciiAlpha(sal_uInt32(code
)); }
131 /** Check for ASCII digit character.
133 @param code A Unicode code point.
135 @return True if code is an ASCII (decimal) digit character (ASCII
138 @since LibreOffice 4.1
140 inline bool isAsciiDigit(sal_uInt32 code
)
142 assert(isUnicodeCodePoint(code
));
143 return code
>= '0' && code
<= '9';
146 #if defined LIBO_INTERNAL_ONLY
147 bool isAsciiDigit(char) = delete;
148 bool isAsciiDigit(signed char) = delete;
149 template <typename T
> inline bool isAsciiDigit(T code
) { return isAsciiDigit(sal_uInt32(code
)); }
152 /** Check for ASCII alphanumeric character.
154 @param code A Unicode code point.
156 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
157 'A'--'Z', or 'a'--'z').
159 @since LibreOffice 4.1
161 inline bool isAsciiAlphanumeric(sal_uInt32 code
)
163 assert(isUnicodeCodePoint(code
));
164 return isAsciiDigit(code
) || isAsciiAlpha(code
);
167 #if defined LIBO_INTERNAL_ONLY
168 bool isAsciiAlphanumeric(char) = delete;
169 bool isAsciiAlphanumeric(signed char) = delete;
170 template <typename T
> inline bool isAsciiAlphanumeric(T code
)
172 return isAsciiAlphanumeric(sal_uInt32(code
));
176 /** Check for ASCII canonic hexadecimal digit character.
178 @param code A Unicode code point.
180 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
181 digit character (ASCII '0'--'9' or 'A'--'F').
183 @since LibreOffice 4.1
185 inline bool isAsciiCanonicHexDigit(sal_uInt32 code
)
187 assert(isUnicodeCodePoint(code
));
188 return isAsciiDigit(code
) || (code
>= 'A' && code
<= 'F');
191 #if defined LIBO_INTERNAL_ONLY
192 bool isAsciiCanonicHexDigit(char) = delete;
193 bool isAsciiCanonicHexDigit(signed char) = delete;
194 template <typename T
> inline bool isAsciiCanonicHexDigit(T code
)
196 return isAsciiCanonicHexDigit(sal_uInt32(code
));
200 /** Check for ASCII hexadecimal digit character.
202 @param code A Unicode code point.
204 @return True if code is an ASCII hexadecimal digit character (ASCII
205 '0'--'9', 'A'--'F', or 'a'--'f').
207 @since LibreOffice 4.1
209 inline bool isAsciiHexDigit(sal_uInt32 code
)
211 assert(isUnicodeCodePoint(code
));
212 return isAsciiCanonicHexDigit(code
) || (code
>= 'a' && code
<= 'f');
215 #if defined LIBO_INTERNAL_ONLY
216 bool isAsciiHexDigit(char) = delete;
217 bool isAsciiHexDigit(signed char) = delete;
218 template <typename T
> inline bool isAsciiHexDigit(T code
)
220 return isAsciiHexDigit(sal_uInt32(code
));
224 /** Check for ASCII octal digit character.
226 @param code A Unicode code point.
228 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
230 @since LibreOffice 5.0
232 inline bool isAsciiOctalDigit(sal_uInt32 code
)
234 assert(isUnicodeCodePoint(code
));
235 return code
>= '0' && code
<= '7';
238 #if defined LIBO_INTERNAL_ONLY
239 bool isAsciiOctalDigit(char) = delete;
240 bool isAsciiOctalDigit(signed char) = delete;
241 template <typename T
> inline bool isAsciiOctalDigit(T code
)
243 return isAsciiOctalDigit(sal_uInt32(code
));
247 /** Check for ASCII white space character.
249 @param code A Unicode code point.
251 @return True if code is an ASCII white space character as defined by C for
252 isspace in the "C" locale (ASCII ' ', '\\f', '\\n', '\\r', '\\t' '\\v').
254 @since LibreOffice 5.4
256 inline bool isAsciiWhiteSpace(sal_uInt32 code
)
258 assert(isUnicodeCodePoint(code
));
259 return code
== ' ' || code
== '\f' || code
== '\n' || code
== '\r' || code
== '\t'
263 #if defined LIBO_INTERNAL_ONLY
264 bool isAsciiWhiteSpace(char) = delete;
265 bool isAsciiWhiteSpace(signed char) = delete;
266 template <typename T
> inline bool isAsciiWhiteSpace(T code
)
268 return isAsciiWhiteSpace(sal_uInt32(code
));
272 /** Convert a character, if ASCII, to upper case.
274 @param code A Unicode code point.
276 @return code converted to ASCII upper case.
278 @since LibreOffice 4.2
280 inline sal_uInt32
toAsciiUpperCase(sal_uInt32 code
)
282 assert(isUnicodeCodePoint(code
));
283 return isAsciiLowerCase(code
) ? code
- 32 : code
;
286 #if defined LIBO_INTERNAL_ONLY
287 sal_uInt32
toAsciiUpperCase(char) = delete;
288 sal_uInt32
toAsciiUpperCase(signed char) = delete;
289 template <typename T
> inline sal_uInt32
toAsciiUpperCase(T code
)
291 return toAsciiUpperCase(sal_uInt32(code
));
295 /** Convert a character, if ASCII, to lower case.
297 @param code A Unicode code point.
299 @return code converted to ASCII lower case.
301 @since LibreOffice 4.2
303 inline sal_uInt32
toAsciiLowerCase(sal_uInt32 code
)
305 assert(isUnicodeCodePoint(code
));
306 return isAsciiUpperCase(code
) ? code
+ 32 : code
;
309 #if defined LIBO_INTERNAL_ONLY
310 sal_uInt32
toAsciiLowerCase(char) = delete;
311 sal_uInt32
toAsciiLowerCase(signed char) = delete;
312 template <typename T
> inline sal_uInt32
toAsciiLowerCase(T code
)
314 return toAsciiLowerCase(sal_uInt32(code
));
318 /** Compare two characters ignoring ASCII case.
320 @param code1 A Unicode code point.
322 @param code2 A unicode code point.
324 @return 0 if both code points are equal,
325 < 0 if code1 is less than code2,
326 > 0 if code1 is greater than code2.
328 @since LibreOffice 4.2
330 inline sal_Int32
compareIgnoreAsciiCase(sal_uInt32 code1
, sal_uInt32 code2
)
332 assert(isUnicodeCodePoint(code1
));
333 assert(isUnicodeCodePoint(code2
));
334 return static_cast<sal_Int32
>(toAsciiLowerCase(code1
))
335 - static_cast<sal_Int32
>(toAsciiLowerCase(code2
));
341 sal_uInt32
const surrogatesHighFirst
= 0xD800;
342 sal_uInt32
const surrogatesHighLast
= 0xDBFF;
343 sal_uInt32
const surrogatesLowFirst
= 0xDC00;
344 sal_uInt32
const surrogatesLowLast
= 0xDFFF;
348 /** Check for surrogate.
350 @param code A Unicode code point.
352 @return True if code is a surrogate code point (0xD800--0xDFFF).
354 @since LibreOffice 6.0
356 inline bool isSurrogate(sal_uInt32 code
)
358 assert(isUnicodeCodePoint(code
));
359 return code
>= detail::surrogatesHighFirst
&& code
<= detail::surrogatesLowLast
;
362 /** Check for high surrogate.
364 @param code A Unicode code point.
366 @return True if code is a high surrogate code point (0xD800--0xDBFF).
368 @since LibreOffice 5.0
370 inline bool isHighSurrogate(sal_uInt32 code
)
372 assert(isUnicodeCodePoint(code
));
373 return code
>= detail::surrogatesHighFirst
&& code
<= detail::surrogatesHighLast
;
376 /** Check for low surrogate.
378 @param code A Unicode code point.
380 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
382 @since LibreOffice 5.0
384 inline bool isLowSurrogate(sal_uInt32 code
)
386 assert(isUnicodeCodePoint(code
));
387 return code
>= detail::surrogatesLowFirst
&& code
<= detail::surrogatesLowLast
;
390 /** Get high surrogate half of a non-BMP Unicode code point.
392 @param code A non-BMP Unicode code point.
394 @return The UTF-16 high surrogate half for the give code point.
396 @since LibreOffice 5.0
398 inline sal_Unicode
getHighSurrogate(sal_uInt32 code
)
400 assert(isUnicodeCodePoint(code
));
401 assert(code
>= 0x10000);
402 return static_cast<sal_Unicode
>(((code
- 0x10000) >> 10) | detail::surrogatesHighFirst
);
405 /** Get low surrogate half of a non-BMP Unicode code point.
407 @param code A non-BMP Unicode code point.
409 @return The UTF-16 low surrogate half for the give code point.
411 @since LibreOffice 5.0
413 inline sal_Unicode
getLowSurrogate(sal_uInt32 code
)
415 assert(isUnicodeCodePoint(code
));
416 assert(code
>= 0x10000);
417 return static_cast<sal_Unicode
>(((code
- 0x10000) & 0x3FF) | detail::surrogatesLowFirst
);
420 /** Combine surrogates to form a code point.
422 @param high A high surrogate code point.
424 @param low A low surrogate code point.
426 @return The code point represented by the surrogate pair.
428 @since LibreOffice 5.0
430 inline sal_uInt32
combineSurrogates(sal_uInt32 high
, sal_uInt32 low
)
432 assert(isHighSurrogate(high
));
433 assert(isLowSurrogate(low
));
434 return ((high
- detail::surrogatesHighFirst
) << 10) + (low
- detail::surrogatesLowFirst
)
438 /** Split a Unicode code point into UTF-16 code units.
440 @param code A Unicode code point.
442 @param output A non-null pointer to an array with space for at least two
443 sal_Unicode UTF-16 code units.
445 @return The number of UTF-16 code units placed into the output (either one
448 @since LibreOffice 5.3
450 inline std::size_t splitSurrogates(sal_uInt32 code
, sal_Unicode
* output
)
452 assert(isUnicodeCodePoint(code
));
453 assert(output
!= NULL
);
461 output
[0] = getHighSurrogate(code
);
462 output
[1] = getLowSurrogate(code
);
467 /** Check for Unicode scalar value.
469 @param code An integer.
471 @return True if code is a Unicode scalar value.
473 @since LibreOffice 6.0
475 inline bool isUnicodeScalarValue(sal_uInt32 code
)
477 return isUnicodeCodePoint(code
) && !isSurrogate(code
);
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */