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>
27 #include <sal/types.h>
32 /** Check for ASCII character.
34 @param code A Unicode code point.
36 @return True if code is an ASCII character (0x00--0x7F).
38 @since LibreOffice 4.1
40 inline bool isAscii(sal_uInt32 code
)
42 assert(code
<= 0x10FFFF);
46 /** Check for ASCII lower case character.
48 @param code A Unicode code point.
50 @return True if code is an ASCII lower case alphabetic character (ASCII
53 @since LibreOffice 4.1
55 inline bool isAsciiLowerCase(sal_uInt32 code
)
57 assert(code
<= 0x10FFFF);
58 return code
>= 'a' && code
<= 'z';
61 /** Check for ASCII upper case character.
63 @param code A Unicode code point.
65 @return True if code is an ASCII upper case alphabetic character (ASCII
68 @since LibreOffice 4.1
70 inline bool isAsciiUpperCase(sal_uInt32 code
)
72 assert(code
<= 0x10FFFF);
73 return code
>= 'A' && code
<= 'Z';
76 /** Check for ASCII alphabetic character.
78 @param code A Unicode code point.
80 @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
83 @since LibreOffice 4.1
85 inline bool isAsciiAlpha(sal_uInt32 code
)
87 assert(code
<= 0x10FFFF);
88 return isAsciiLowerCase(code
) || isAsciiUpperCase(code
);
91 /** Check for ASCII digit character.
93 @param code A Unicode code point.
95 @return True if code is an ASCII (decimal) digit character (ASCII
98 @since LibreOffice 4.1
100 inline bool isAsciiDigit(sal_uInt32 code
)
102 assert(code
<= 0x10FFFF);
103 return code
>= '0' && code
<= '9';
106 /** Check for ASCII alphanumeric character.
108 @param code A Unicode code point.
110 @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
111 'A'--'Z', or 'a'--'z').
113 @since LibreOffice 4.1
115 inline bool isAsciiAlphanumeric(sal_uInt32 code
)
117 assert(code
<= 0x10FFFF);
118 return isAsciiDigit(code
) || isAsciiAlpha(code
);
121 /** Check for ASCII canonic hexadecimal digit character.
123 @param code A Unicode code point.
125 @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
126 digit character (ASCII '0'--'9' or 'A'--'F').
128 @since LibreOffice 4.1
130 inline bool isAsciiCanonicHexDigit(sal_uInt32 code
)
132 assert(code
<= 0x10FFFF);
133 return isAsciiDigit(code
) || (code
>= 'A' && code
<= 'F');
136 /** Check for ASCII hexadecimal digit character.
138 @param code A Unicode code point.
140 @return True if code is an ASCII hexadecimal digit character (ASCII
141 '0'--'9', 'A'--'F', or 'a'--'f').
143 @since LibreOffice 4.1
145 inline bool isAsciiHexDigit(sal_uInt32 code
)
147 assert(code
<= 0x10FFFF);
148 return isAsciiCanonicHexDigit(code
) || (code
>= 'a' && code
<= 'f');
151 /** Check for ASCII octal digit character.
153 @param code A Unicode code point.
155 @return True if code is an ASCII octal digit character (ASCII '0'--'7').
157 @since LibreOffice 5.0
159 inline bool isAsciiOctalDigit(sal_uInt32 code
)
161 assert(code
<= 0x10FFFF);
162 return code
>= '0' && code
<= '7';
166 /** Convert a character, if ASCII, to upper case.
168 @param code A Unicode code point.
170 @return code converted to ASCII upper case.
172 @since LibreOffice 4.2
174 inline sal_uInt32
toAsciiUpperCase(sal_uInt32 code
)
176 assert(code
<= 0x10FFFF);
177 return isAsciiLowerCase(code
) ? code
- 32 : code
;
180 /** Convert a character, if ASCII, to lower case.
182 @param code A Unicode code point.
184 @return code converted to ASCII lower case.
186 @since LibreOffice 4.2
188 inline sal_uInt32
toAsciiLowerCase(sal_uInt32 code
)
190 assert(code
<= 0x10FFFF);
191 return isAsciiUpperCase(code
) ? code
+ 32 : code
;
194 /** Compare two characters ignoring ASCII case.
196 @param code1 A Unicode code point.
198 @param code2 A unicode code point.
200 @return 0 if both code points are equal,
201 < 0 if code1 is less than code2,
202 > 0 if code1 is greater than code2.
204 @since LibreOffice 4.2
206 inline sal_Int32
compareIgnoreAsciiCase(sal_uInt32 code1
, sal_uInt32 code2
)
208 assert(code1
<= 0x10FFFF);
209 assert(code2
<= 0x10FFFF);
210 return static_cast<sal_Int32
>(toAsciiLowerCase(code1
))
211 - static_cast<sal_Int32
>(toAsciiLowerCase(code2
));
217 sal_uInt32
const surrogatesHighFirst
= 0xD800;
218 sal_uInt32
const surrogatesHighLast
= 0xDBFF;
219 sal_uInt32
const surrogatesLowFirst
= 0xDC00;
220 sal_uInt32
const surrogatesLowLast
= 0xDFFF;
225 /** Check for high surrogate.
227 @param code A Unicode code point.
229 @return True if code is a high surrogate code point (0xD800--0xDBFF).
231 @since LibreOffice 5.0
233 inline bool isHighSurrogate(sal_uInt32 code
) {
234 assert(code
<= 0x10FFFF);
235 return code
>= detail::surrogatesHighFirst
236 && code
<= detail::surrogatesHighLast
;
239 /** Check for low surrogate.
241 @param code A Unicode code point.
243 @return True if code is a low surrogate code point (0xDC00--0xDFFF).
245 @since LibreOffice 5.0
247 inline bool isLowSurrogate(sal_uInt32 code
) {
248 assert(code
<= 0x10FFFF);
249 return code
>= detail::surrogatesLowFirst
250 && code
<= detail::surrogatesLowLast
;
253 /** Get high surrogate half of a non-BMP Unicode code point.
255 @param code A non-BMP Unicode code point.
257 @return The UTF-16 high surrogate half for the give code point.
259 @since LibreOffice 5.0
261 inline sal_Unicode
getHighSurrogate(sal_uInt32 code
) {
262 assert(code
<= 0x10FFFF);
263 assert(code
>= 0x10000);
264 return ((code
- 0x10000) >> 10) | detail::surrogatesHighFirst
;
267 /** Get low surrogate half of a non-BMP Unicode code point.
269 @param code A non-BMP Unicode code point.
271 @return The UTF-16 low surrogate half for the give code point.
273 @since LibreOffice 5.0
275 inline sal_Unicode
getLowSurrogate(sal_uInt32 code
) {
276 assert(code
<= 0x10FFFF);
277 assert(code
>= 0x10000);
278 return ((code
- 0x10000) & 0x3FF) | detail::surrogatesLowFirst
;
281 /** Combine surrogates to form a code point.
283 @param high A high surrogate code point.
285 @param low A low surrogate code point.
287 @return The code point represented by the surrogate pair.
289 @since LibreOffice 5.0
291 inline sal_uInt32
combineSurrogates(sal_uInt32 high
, sal_uInt32 low
) {
292 assert(isHighSurrogate(high
));
293 assert(isLowSurrogate(low
));
294 return ((high
- detail::surrogatesHighFirst
) << 10)
295 + (low
- detail::surrogatesLowFirst
) + 0x10000;
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */