LibreOffice
LibreOffice 5.3 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
character.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_RTL_CHARACTER_HXX
21 #define INCLUDED_RTL_CHARACTER_HXX
22 
23 #include <sal/config.h>
24 
25 #include <cassert>
26 
27 #include <sal/types.h>
28 
29 namespace rtl
30 {
31 
40 inline bool isUnicodeCodePoint(sal_uInt32 code)
41 {
42  return code <= 0x10FFFF;
43 }
44 
53 inline bool isAscii(sal_uInt32 code)
54 {
55  assert(isUnicodeCodePoint(code));
56  return code <= 0x7F;
57 }
58 
68 inline bool isAsciiLowerCase(sal_uInt32 code)
69 {
70  assert(isUnicodeCodePoint(code));
71  return code >= 'a' && code <= 'z';
72 }
73 
83 inline bool isAsciiUpperCase(sal_uInt32 code)
84 {
85  assert(isUnicodeCodePoint(code));
86  return code >= 'A' && code <= 'Z';
87 }
88 
98 inline bool isAsciiAlpha(sal_uInt32 code)
99 {
100  assert(isUnicodeCodePoint(code));
101  return isAsciiLowerCase(code) || isAsciiUpperCase(code);
102 }
103 
113 inline bool isAsciiDigit(sal_uInt32 code)
114 {
115  assert(isUnicodeCodePoint(code));
116  return code >= '0' && code <= '9';
117 }
118 
128 inline bool isAsciiAlphanumeric(sal_uInt32 code)
129 {
130  assert(isUnicodeCodePoint(code));
131  return isAsciiDigit(code) || isAsciiAlpha(code);
132 }
133 
143 inline bool isAsciiCanonicHexDigit(sal_uInt32 code)
144 {
145  assert(isUnicodeCodePoint(code));
146  return isAsciiDigit(code) || (code >= 'A' && code <= 'F');
147 }
148 
158 inline bool isAsciiHexDigit(sal_uInt32 code)
159 {
160  assert(isUnicodeCodePoint(code));
161  return isAsciiCanonicHexDigit(code) || (code >= 'a' && code <= 'f');
162 }
163 
172 inline bool isAsciiOctalDigit(sal_uInt32 code)
173 {
174  assert(isUnicodeCodePoint(code));
175  return code >= '0' && code <= '7';
176 }
177 
178 
187 inline sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
188 {
189  assert(isUnicodeCodePoint(code));
190  return isAsciiLowerCase(code) ? code - 32 : code;
191 }
192 
201 inline sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
202 {
203  assert(isUnicodeCodePoint(code));
204  return isAsciiUpperCase(code) ? code + 32 : code;
205 }
206 
219 inline sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
220 {
221  assert(isUnicodeCodePoint(code1));
222  assert(isUnicodeCodePoint(code2));
223  return static_cast<sal_Int32>(toAsciiLowerCase(code1))
224  - static_cast<sal_Int32>(toAsciiLowerCase(code2));
225 }
226 
228 namespace detail {
229 
230 sal_uInt32 const surrogatesHighFirst = 0xD800;
231 sal_uInt32 const surrogatesHighLast = 0xDBFF;
232 sal_uInt32 const surrogatesLowFirst = 0xDC00;
233 sal_uInt32 const surrogatesLowLast = 0xDFFF;
234 
235 }
237 
246 inline bool isHighSurrogate(sal_uInt32 code) {
247  assert(isUnicodeCodePoint(code));
248  return code >= detail::surrogatesHighFirst
249  && code <= detail::surrogatesHighLast;
250 }
251 
260 inline bool isLowSurrogate(sal_uInt32 code) {
261  assert(isUnicodeCodePoint(code));
262  return code >= detail::surrogatesLowFirst
263  && code <= detail::surrogatesLowLast;
264 }
265 
274 inline sal_Unicode getHighSurrogate(sal_uInt32 code) {
275  assert(isUnicodeCodePoint(code));
276  assert(code >= 0x10000);
277  return static_cast<sal_Unicode>(((code - 0x10000) >> 10) | detail::surrogatesHighFirst);
278 }
279 
288 inline sal_Unicode getLowSurrogate(sal_uInt32 code) {
289  assert(isUnicodeCodePoint(code));
290  assert(code >= 0x10000);
291  return static_cast<sal_Unicode>(((code - 0x10000) & 0x3FF) | detail::surrogatesLowFirst);
292 }
293 
304 inline sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low) {
305  assert(isHighSurrogate(high));
306  assert(isLowSurrogate(low));
307  return ((high - detail::surrogatesHighFirst) << 10)
308  + (low - detail::surrogatesLowFirst) + 0x10000;
309 }
310 
311 }
312 
313 #endif
314 
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool isAsciiHexDigit(sal_uInt32 code)
Check for ASCII hexadecimal digit character.
Definition: character.hxx:158
bool isUnicodeCodePoint(sal_uInt32 code)
Check for Unicode code point.
Definition: character.hxx:40
bool isAsciiDigit(sal_uInt32 code)
Check for ASCII digit character.
Definition: character.hxx:113
bool isAsciiAlpha(sal_uInt32 code)
Check for ASCII alphabetic character.
Definition: character.hxx:98
bool isAsciiOctalDigit(sal_uInt32 code)
Check for ASCII octal digit character.
Definition: character.hxx:172
sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
Convert a character, if ASCII, to upper case.
Definition: character.hxx:187
bool isAscii(sal_uInt32 code)
Check for ASCII character.
Definition: character.hxx:53
bool isAsciiCanonicHexDigit(sal_uInt32 code)
Check for ASCII canonic hexadecimal digit character.
Definition: character.hxx:143
bool isLowSurrogate(sal_uInt32 code)
Check for low surrogate.
Definition: character.hxx:260
bool isAsciiAlphanumeric(sal_uInt32 code)
Check for ASCII alphanumeric character.
Definition: character.hxx:128
bool isAsciiUpperCase(sal_uInt32 code)
Check for ASCII upper case character.
Definition: character.hxx:83
bool isAsciiLowerCase(sal_uInt32 code)
Check for ASCII lower case character.
Definition: character.hxx:68
sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
Convert a character, if ASCII, to lower case.
Definition: character.hxx:201
sal_uInt16 sal_Unicode
Definition: types.h:155
sal_Unicode getLowSurrogate(sal_uInt32 code)
Get low surrogate half of a non-BMP Unicode code point.
Definition: character.hxx:288
sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low)
Combine surrogates to form a code point.
Definition: character.hxx:304
sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
Compare two characters ignoring ASCII case.
Definition: character.hxx:219
sal_Unicode getHighSurrogate(sal_uInt32 code)
Get high surrogate half of a non-BMP Unicode code point.
Definition: character.hxx:274
bool isHighSurrogate(sal_uInt32 code)
Check for high surrogate.
Definition: character.hxx:246