Line data Source code
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 :
32 : /** Check for ASCII character.
33 :
34 : @param code A Unicode code point.
35 :
36 : @return True if code is an ASCII character (0x00--0x7F).
37 :
38 : @since LibreOffice 4.1
39 : */
40 49391163 : inline bool isAscii(sal_uInt32 code)
41 : {
42 : assert(code <= 0x10FFFF);
43 49391163 : return code <= 0x7F;
44 : }
45 :
46 : /** Check for ASCII lower case character.
47 :
48 : @param code A Unicode code point.
49 :
50 : @return True if code is an ASCII lower case alphabetic character (ASCII
51 : 'a'--'z').
52 :
53 : @since LibreOffice 4.1
54 : */
55 11607776 : inline bool isAsciiLowerCase(sal_uInt32 code)
56 : {
57 : assert(code <= 0x10FFFF);
58 11607776 : return code >= 'a' && code <= 'z';
59 : }
60 :
61 : /** Check for ASCII upper case character.
62 :
63 : @param code A Unicode code point.
64 :
65 : @return True if code is an ASCII upper case alphabetic character (ASCII
66 : 'A'--'Z').
67 :
68 : @since LibreOffice 4.1
69 : */
70 149338990 : inline bool isAsciiUpperCase(sal_uInt32 code)
71 : {
72 : assert(code <= 0x10FFFF);
73 149338990 : return code >= 'A' && code <= 'Z';
74 : }
75 :
76 : /** Check for ASCII alphabetic character.
77 :
78 : @param code A Unicode code point.
79 :
80 : @return True if code is an ASCII alphabetic character (ASCII 'A'--'Z' or
81 : 'a'--'z').
82 :
83 : @since LibreOffice 4.1
84 : */
85 8260442 : inline bool isAsciiAlpha(sal_uInt32 code)
86 : {
87 : assert(code <= 0x10FFFF);
88 8260442 : return isAsciiLowerCase(code) || isAsciiUpperCase(code);
89 : }
90 :
91 : /** Check for ASCII digit character.
92 :
93 : @param code A Unicode code point.
94 :
95 : @return True if code is an ASCII (decimal) digit character (ASCII
96 : '0'--'9').
97 :
98 : @since LibreOffice 4.1
99 : */
100 6485743 : inline bool isAsciiDigit(sal_uInt32 code)
101 : {
102 : assert(code <= 0x10FFFF);
103 6485743 : return code >= '0' && code <= '9';
104 : }
105 :
106 : /** Check for ASCII alphanumeric character.
107 :
108 : @param code A Unicode code point.
109 :
110 : @return True if code is an ASCII alphanumeric character (ASCII '0'--'9',
111 : 'A'--'Z', or 'a'--'z').
112 :
113 : @since LibreOffice 4.1
114 : */
115 4248810 : inline bool isAsciiAlphanumeric(sal_uInt32 code)
116 : {
117 : assert(code <= 0x10FFFF);
118 4248810 : return isAsciiDigit(code) || isAsciiAlpha(code);
119 : }
120 :
121 : /** Check for ASCII canonic hexadecimal digit character.
122 :
123 : @param code A Unicode code point.
124 :
125 : @return True if code is an ASCII canonic (i.e., upper case) hexadecimal
126 : digit character (ASCII '0'--'9' or 'A'--'F').
127 :
128 : @since LibreOffice 4.1
129 : */
130 0 : inline bool isAsciiCanonicHexDigit(sal_uInt32 code)
131 : {
132 : assert(code <= 0x10FFFF);
133 0 : return isAsciiDigit(code) || (code >= 'A' && code <= 'F');
134 : }
135 :
136 : /** Check for ASCII hexadecimal digit character.
137 :
138 : @param code A Unicode code point.
139 :
140 : @return True if code is an ASCII hexadecimal digit character (ASCII
141 : '0'--'9', 'A'--'F', or 'a'--'f').
142 :
143 : @since LibreOffice 4.1
144 : */
145 0 : inline bool isAsciiHexDigit(sal_uInt32 code)
146 : {
147 : assert(code <= 0x10FFFF);
148 0 : return isAsciiCanonicHexDigit(code) || (code >= 'a' && code <= 'f');
149 : }
150 :
151 : /** Convert a character, if ASCII, to upper case.
152 :
153 : @param code A Unicode code point.
154 :
155 : @return code converted to ASCII upper case.
156 :
157 : @since LibreOffice 4.2
158 : */
159 1066232 : inline sal_uInt32 toAsciiUpperCase(sal_uInt32 code)
160 : {
161 : assert(code <= 0x10FFFF);
162 1066232 : return isAsciiLowerCase(code) ? code - 32 : code;
163 : }
164 :
165 : /** Convert a character, if ASCII, to lower case.
166 :
167 : @param code A Unicode code point.
168 :
169 : @return code converted to ASCII lower case.
170 :
171 : @since LibreOffice 4.2
172 : */
173 128750560 : inline sal_uInt32 toAsciiLowerCase(sal_uInt32 code)
174 : {
175 : assert(code <= 0x10FFFF);
176 128750560 : return isAsciiUpperCase(code) ? code + 32 : code;
177 : }
178 :
179 : /** Compare two characters ignoring ASCII case.
180 :
181 : @param code1 A Unicode code point.
182 :
183 : @param code2 A unicode code point.
184 :
185 : @return 0 if both code points are equal,
186 : < 0 if code1 is less than code2,
187 : > 0 if code1 is greater than code2.
188 :
189 : @since LibreOffice 4.2
190 : */
191 27663026 : inline sal_Int32 compareIgnoreAsciiCase(sal_uInt32 code1, sal_uInt32 code2)
192 : {
193 : assert(code1 <= 0x10FFFF);
194 : assert(code2 <= 0x10FFFF);
195 27663026 : return static_cast<sal_Int32>(toAsciiLowerCase(code1))
196 27663026 : - static_cast<sal_Int32>(toAsciiLowerCase(code2));
197 : }
198 :
199 : }
200 :
201 : #endif
202 :
203 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|