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 /** Check for ASCII lower case character.
62 @param code A Unicode code point.
64 @return True if code is an ASCII lower case alphabetic character (ASCII
67 @since LibreOffice 4.1
69 inline bool isAsciiLowerCase(sal_uInt32 code
)
71 assert(isUnicodeCodePoint(code
));
72 return code
>= 'a' && code
<= 'z';
75 /** Check for ASCII upper case character.
77 @param code A Unicode code point.
79 @return True if code is an ASCII upper case alphabetic character (ASCII
82 @since LibreOffice 4.1
84 inline bool isAsciiUpperCase(sal_uInt32 code
)
86 assert(isUnicodeCodePoint(code
));
87 return code
>= 'A' && code
<= 'Z';
90 /** Check for ASCII alphabetic character.
92 @param code A Unicode code point.
94 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
97 @since LibreOffice 4.1
99 inline bool isAsciiAlpha(sal_uInt32 code
)
101 assert(isUnicodeCodePoint(code
));
102 return isAsciiLowerCase(code
) || isAsciiUpperCase(code
);
105 /** Check for ASCII digit character.
107 @param code A Unicode code point.
109 @return True if code is an ASCII (decimal) digit character (ASCII
112 @since LibreOffice 4.1
114 inline bool isAsciiDigit(sal_uInt32 code
)
116 assert(isUnicodeCodePoint(code
));
117 return code
>= '0' && code
<= '9';
120 /** Check for ASCII alphanumeric character.
122 @param code A Unicode code point.
124 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
125 'A'--'Z', or 'a'--'z').
127 @since LibreOffice 4.1
129 inline bool isAsciiAlphanumeric(sal_uInt32 code
)
131 assert(isUnicodeCodePoint(code
));
132 return isAsciiDigit(code
) || isAsciiAlpha(code
);
135 /** Check for ASCII canonic hexadecimal digit character.
137 @param code A Unicode code point.
139 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
140 digit character (ASCII '0'--'9' or 'A'--'F').
142 @since LibreOffice 4.1
144 inline bool isAsciiCanonicHexDigit(sal_uInt32 code
)
146 assert(isUnicodeCodePoint(code
));
147 return isAsciiDigit(code
) || (code
>= 'A' && code
<= 'F');
150 /** Check for ASCII hexadecimal digit character.
152 @param code A Unicode code point.
154 @return True if code is an ASCII hexadecimal digit character (ASCII
155 '0'--'9', 'A'--'F', or 'a'--'f').
157 @since LibreOffice 4.1
159 inline bool isAsciiHexDigit(sal_uInt32 code
)
161 assert(isUnicodeCodePoint(code
));
162 return isAsciiCanonicHexDigit(code
) || (code
>= 'a' && code
<= 'f');
165 /** Check for ASCII octal digit character.
167 @param code A Unicode code point.
169 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
171 @since LibreOffice 5.0
173 inline bool isAsciiOctalDigit(sal_uInt32 code
)
175 assert(isUnicodeCodePoint(code
));
176 return code
>= '0' && code
<= '7';
180 /** Convert a character, if ASCII, to upper case.
182 @param code A Unicode code point.
184 @return code converted to ASCII upper case.
186 @since LibreOffice 4.2
188 inline sal_uInt32
toAsciiUpperCase(sal_uInt32 code
)
190 assert(isUnicodeCodePoint(code
));
191 return isAsciiLowerCase(code
) ? code
- 32 : code
;
194 /** Convert a character, if ASCII, to lower case.
196 @param code A Unicode code point.
198 @return code converted to ASCII lower case.
200 @since LibreOffice 4.2
202 inline sal_uInt32
toAsciiLowerCase(sal_uInt32 code
)
204 assert(isUnicodeCodePoint(code
));
205 return isAsciiUpperCase(code
) ? code
+ 32 : code
;
208 /** Compare two characters ignoring ASCII case.
210 @param code1 A Unicode code point.
212 @param code2 A unicode code point.
214 @return 0 if both code points are equal,
215 < 0 if code1 is less than code2,
216 > 0 if code1 is greater than code2.
218 @since LibreOffice 4.2
220 inline sal_Int32
compareIgnoreAsciiCase(sal_uInt32 code1
, sal_uInt32 code2
)
222 assert(isUnicodeCodePoint(code1
));
223 assert(isUnicodeCodePoint(code2
));
224 return static_cast<sal_Int32
>(toAsciiLowerCase(code1
))
225 - static_cast<sal_Int32
>(toAsciiLowerCase(code2
));
231 sal_uInt32
const surrogatesHighFirst
= 0xD800;
232 sal_uInt32
const surrogatesHighLast
= 0xDBFF;
233 sal_uInt32
const surrogatesLowFirst
= 0xDC00;
234 sal_uInt32
const surrogatesLowLast
= 0xDFFF;
239 /** Check for high surrogate.
241 @param code A Unicode code point.
243 @return True if code is a high surrogate code point (0xD800--0xDBFF).
245 @since LibreOffice 5.0
247 inline bool isHighSurrogate(sal_uInt32 code
) {
248 assert(isUnicodeCodePoint(code
));
249 return code
>= detail::surrogatesHighFirst
250 && code
<= detail::surrogatesHighLast
;
253 /** Check for low surrogate.
255 @param code A Unicode code point.
257 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
259 @since LibreOffice 5.0
261 inline bool isLowSurrogate(sal_uInt32 code
) {
262 assert(isUnicodeCodePoint(code
));
263 return code
>= detail::surrogatesLowFirst
264 && code
<= detail::surrogatesLowLast
;
267 /** Get high surrogate half of a non-BMP Unicode code point.
269 @param code A non-BMP Unicode code point.
271 @return The UTF-16 high surrogate half for the give code point.
273 @since LibreOffice 5.0
275 inline sal_Unicode
getHighSurrogate(sal_uInt32 code
) {
276 assert(isUnicodeCodePoint(code
));
277 assert(code
>= 0x10000);
278 return static_cast<sal_Unicode
>(((code
- 0x10000) >> 10) | detail::surrogatesHighFirst
);
281 /** Get low surrogate half of a non-BMP Unicode code point.
283 @param code A non-BMP Unicode code point.
285 @return The UTF-16 low surrogate half for the give code point.
287 @since LibreOffice 5.0
289 inline sal_Unicode
getLowSurrogate(sal_uInt32 code
) {
290 assert(isUnicodeCodePoint(code
));
291 assert(code
>= 0x10000);
292 return static_cast<sal_Unicode
>(((code
- 0x10000) & 0x3FF) | detail::surrogatesLowFirst
);
295 /** Combine surrogates to form a code point.
297 @param high A high surrogate code point.
299 @param low A low surrogate code point.
301 @return The code point represented by the surrogate pair.
303 @since LibreOffice 5.0
305 inline sal_uInt32
combineSurrogates(sal_uInt32 high
, sal_uInt32 low
) {
306 assert(isHighSurrogate(high
));
307 assert(isLowSurrogate(low
));
308 return ((high
- detail::surrogatesHighFirst
) << 10)
309 + (low
- detail::surrogatesLowFirst
) + 0x10000;
312 /** Split a Unicode code point into UTF-16 code units.
314 @param code A Unicode code point.
316 @param output A non-null pointer to an array with space for at least two
317 sal_Unicode UTF-16 code units.
319 @return The number of UTF-16 code units placed into the output (either one
322 @since LibreOffice 5.3
324 inline std::size_t splitSurrogates(sal_uInt32 code
, sal_Unicode
* output
) {
325 assert(isUnicodeCodePoint(code
));
326 assert(output
!= NULL
);
327 if (code
< 0x10000) {
331 output
[0] = getHighSurrogate(code
);
332 output
[1] = getLowSurrogate(code
);
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */