LibreOffice
LibreOffice 6.0 SDK C/C++ API Reference
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 #include <cstddef>
27 
28 #include <sal/types.h>
29 
30 namespace rtl
31 {
32 
41 inline bool isUnicodeCodePoint(sal_uInt32 code)
42 {
43  return code <= 0x10FFFF;
44 }
45 
54 inline bool isAscii(sal_uInt32 code)
55 {
56  assert(isUnicodeCodePoint(code));
57  return code <= 0x7F;
58 }
59 
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)); }
65 #endif
66 
76 inline bool isAsciiLowerCase(sal_uInt32 code)
77 {
78  assert(isUnicodeCodePoint(code));
79  return code >= 'a' && code <= 'z';
80 }
81 
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)); }
87 #endif
88 
98 inline bool isAsciiUpperCase(sal_uInt32 code)
99 {
100  assert(isUnicodeCodePoint(code));
101  return code >= 'A' && code <= 'Z';
102 }
103 
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)); }
109 #endif
110 
120 inline bool isAsciiAlpha(sal_uInt32 code)
121 {
122  assert(isUnicodeCodePoint(code));
123  return isAsciiLowerCase(code) || isAsciiUpperCase(code);
124 }
125 
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)); }
131 #endif
132 
142 inline bool isAsciiDigit(sal_uInt32 code)
143 {
144  assert(isUnicodeCodePoint(code));
145  return code >= '0' && code <= '9';
146 }
147 
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)); }
153 #endif
154 
164 inline bool isAsciiAlphanumeric(sal_uInt32 code)
165 {
166  assert(isUnicodeCodePoint(code));
167  return isAsciiDigit(code) || isAsciiAlpha(code);
168 }
169 
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)); }
175 #endif
176 
186 inline bool isAsciiCanonicHexDigit(sal_uInt32 code)
187 {
188  assert(isUnicodeCodePoint(code));
189  return isAsciiDigit(code) || (code >= 'A' && code <= 'F');
190 }
191 
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)); }
197 #endif
198 
208 inline bool isAsciiHexDigit(sal_uInt32 code)
209 {
210  assert(isUnicodeCodePoint(code));
211  return isAsciiCanonicHexDigit(code) || (code >= 'a' && code <= 'f');
212 }
213 
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)); }
219 #endif
220 
229 inline bool isAsciiOctalDigit(sal_uInt32 code)
230 {
231  assert(isUnicodeCodePoint(code));
232  return code >= '0' && code <= '7';
233 }
234 
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)); }
240 #endif
241 
251 inline bool isAsciiWhiteSpace(sal_uInt32 code)
252 {
253  assert(isUnicodeCodePoint(code));
254  return code == ' ' || code == '\f' || code == '\n' || code == '\r'
255  || code == '\t' || code == '\v';
256 }
257 
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)); }
263 #endif
264 
273 inline sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
274 {
275  assert(isUnicodeCodePoint(code));
276  return isAsciiLowerCase(code) ? code - 32 : code;
277 }
278 
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)); }
284 #endif
285 
294 inline sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
295 {
296  assert(isUnicodeCodePoint(code));
297  return isAsciiUpperCase(code) ? code + 32 : code;
298 }
299 
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)); }
305 #endif
306 
319 inline sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
320 {
321  assert(isUnicodeCodePoint(code1));
322  assert(isUnicodeCodePoint(code2));
323  return static_cast<sal_Int32>(toAsciiLowerCase(code1))
324  - static_cast<sal_Int32>(toAsciiLowerCase(code2));
325 }
326 
328 namespace detail {
329 
330 sal_uInt32 const surrogatesHighFirst = 0xD800;
331 sal_uInt32 const surrogatesHighLast = 0xDBFF;
332 sal_uInt32 const surrogatesLowFirst = 0xDC00;
333 sal_uInt32 const surrogatesLowLast = 0xDFFF;
334 
335 }
337 
346 inline bool isHighSurrogate(sal_uInt32 code) {
347  assert(isUnicodeCodePoint(code));
348  return code >= detail::surrogatesHighFirst
349  && code <= detail::surrogatesHighLast;
350 }
351 
360 inline bool isLowSurrogate(sal_uInt32 code) {
361  assert(isUnicodeCodePoint(code));
362  return code >= detail::surrogatesLowFirst
363  && code <= detail::surrogatesLowLast;
364 }
365 
374 inline sal_Unicode getHighSurrogate(sal_uInt32 code) {
375  assert(isUnicodeCodePoint(code));
376  assert(code >= 0x10000);
377  return static_cast<sal_Unicode>(((code - 0x10000) >> 10) | detail::surrogatesHighFirst);
378 }
379 
388 inline sal_Unicode getLowSurrogate(sal_uInt32 code) {
389  assert(isUnicodeCodePoint(code));
390  assert(code >= 0x10000);
391  return static_cast<sal_Unicode>(((code - 0x10000) & 0x3FF) | detail::surrogatesLowFirst);
392 }
393 
404 inline sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low) {
405  assert(isHighSurrogate(high));
406  assert(isLowSurrogate(low));
407  return ((high - detail::surrogatesHighFirst) << 10)
408  + (low - detail::surrogatesLowFirst) + 0x10000;
409 }
410 
423 inline std::size_t splitSurrogates(sal_uInt32 code, sal_Unicode * output) {
424  assert(isUnicodeCodePoint(code));
425  assert(output != NULL);
426  if (code < 0x10000) {
427  output[0] = code;
428  return 1;
429  } else {
430  output[0] = getHighSurrogate(code);
431  output[1] = getLowSurrogate(code);
432  return 2;
433  }
434 }
435 
436 }
437 
438 #endif
439 
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool isAsciiHexDigit(sal_uInt32 code)
Check for ASCII hexadecimal digit character.
Definition: character.hxx:208
bool isUnicodeCodePoint(sal_uInt32 code)
Check for Unicode code point.
Definition: character.hxx:41
bool isAsciiDigit(sal_uInt32 code)
Check for ASCII digit character.
Definition: character.hxx:142
bool isAsciiWhiteSpace(sal_uInt32 code)
Check for ASCII white space character.
Definition: character.hxx:251
bool isAsciiAlpha(sal_uInt32 code)
Check for ASCII alphabetic character.
Definition: character.hxx:120
bool isAsciiOctalDigit(sal_uInt32 code)
Check for ASCII octal digit character.
Definition: character.hxx:229
sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
Convert a character, if ASCII, to upper case.
Definition: character.hxx:273
bool isAscii(sal_uInt32 code)
Check for ASCII character.
Definition: character.hxx:54
bool isAsciiCanonicHexDigit(sal_uInt32 code)
Check for ASCII canonic hexadecimal digit character.
Definition: character.hxx:186
bool isLowSurrogate(sal_uInt32 code)
Check for low surrogate.
Definition: character.hxx:360
std::size_t splitSurrogates(sal_uInt32 code, sal_Unicode *output)
Split a Unicode code point into UTF-16 code units.
Definition: character.hxx:423
bool isAsciiAlphanumeric(sal_uInt32 code)
Check for ASCII alphanumeric character.
Definition: character.hxx:164
bool isAsciiUpperCase(sal_uInt32 code)
Check for ASCII upper case character.
Definition: character.hxx:98
bool isAsciiLowerCase(sal_uInt32 code)
Check for ASCII lower case character.
Definition: character.hxx:76
sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
Convert a character, if ASCII, to lower case.
Definition: character.hxx:294
sal_uInt16 sal_Unicode
Definition: types.h:142
sal_Unicode getLowSurrogate(sal_uInt32 code)
Get low surrogate half of a non-BMP Unicode code point.
Definition: character.hxx:388
sal_uInt32 combineSurrogates(sal_uInt32 high, sal_uInt32 low)
Combine surrogates to form a code point.
Definition: character.hxx:404
Definition: bootstrap.hxx:29
sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
Compare two characters ignoring ASCII case.
Definition: character.hxx:319
sal_Unicode getHighSurrogate(sal_uInt32 code)
Get high surrogate half of a non-BMP Unicode code point.
Definition: character.hxx:374
bool isHighSurrogate(sal_uInt32 code)
Check for high surrogate.
Definition: character.hxx:346