/src/poco/Foundation/include/Poco/Ascii.h
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Ascii.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Core |
6 | | // Module: Ascii |
7 | | // |
8 | | // Definition of the Ascii class. |
9 | | // |
10 | | // Copyright (c) 2010, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_Ascii_INCLUDED |
18 | | #define Foundation_Ascii_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | |
23 | | |
24 | | namespace Poco { |
25 | | |
26 | | |
27 | | class Foundation_API Ascii |
28 | | /// This class contains enumerations and static |
29 | | /// utility functions for dealing with ASCII characters |
30 | | /// and their properties. |
31 | | /// |
32 | | /// The classification functions will also work if |
33 | | /// non-ASCII character codes are passed to them, |
34 | | /// but classification will only check for |
35 | | /// ASCII characters. |
36 | | /// |
37 | | /// This allows the classification methods to be used |
38 | | /// on the single bytes of a UTF-8 string, without |
39 | | /// causing assertions or inconsistent results (depending |
40 | | /// upon the current locale) on bytes outside the ASCII range, |
41 | | /// as may be produced by Ascii::isSpace(), etc. |
42 | | { |
43 | | public: |
44 | | enum CharacterProperties |
45 | | /// ASCII character properties. |
46 | | { |
47 | | ACP_CONTROL = 0x0001, |
48 | | ACP_SPACE = 0x0002, |
49 | | ACP_PUNCT = 0x0004, |
50 | | ACP_DIGIT = 0x0008, |
51 | | ACP_HEXDIGIT = 0x0010, |
52 | | ACP_ALPHA = 0x0020, |
53 | | ACP_LOWER = 0x0040, |
54 | | ACP_UPPER = 0x0080, |
55 | | ACP_GRAPH = 0x0100, |
56 | | ACP_PRINT = 0x0200 |
57 | | }; |
58 | | |
59 | | static int properties(int ch); |
60 | | /// Return the ASCII character properties for the |
61 | | /// character with the given ASCII value. |
62 | | /// |
63 | | /// If the character is outside the ASCII range |
64 | | /// (0 .. 127), 0 is returned. |
65 | | |
66 | | static bool hasSomeProperties(int ch, int properties); |
67 | | /// Returns true if the given character is |
68 | | /// within the ASCII range and has at least one of |
69 | | /// the given properties. |
70 | | |
71 | | static bool hasProperties(int ch, int properties); |
72 | | /// Returns true if the given character is |
73 | | /// within the ASCII range and has all of |
74 | | /// the given properties. |
75 | | |
76 | | static bool isAscii(int ch); |
77 | | /// Returns true iff the given character code is within |
78 | | /// the ASCII range (0 .. 127). |
79 | | |
80 | | static bool isSpace(int ch); |
81 | | /// Returns true iff the given character is a whitespace. |
82 | | |
83 | | static bool isDigit(int ch); |
84 | | /// Returns true iff the given character is a digit. |
85 | | |
86 | | static bool isHexDigit(int ch); |
87 | | /// Returns true iff the given character is a hexadecimal digit. |
88 | | |
89 | | static bool isPunct(int ch); |
90 | | /// Returns true iff the given character is a punctuation character. |
91 | | |
92 | | static bool isAlpha(int ch); |
93 | | /// Returns true iff the given character is an alphabetic character. |
94 | | |
95 | | static bool isAlphaNumeric(int ch); |
96 | | /// Returns true iff the given character is an alphabetic character. |
97 | | |
98 | | static bool isLower(int ch); |
99 | | /// Returns true iff the given character is a lowercase alphabetic |
100 | | /// character. |
101 | | |
102 | | static bool isUpper(int ch); |
103 | | /// Returns true iff the given character is an uppercase alphabetic |
104 | | /// character. |
105 | | |
106 | | static bool isPrintable(int ch); |
107 | | /// Returns true iff the given character is printable. |
108 | | |
109 | | static int toLower(int ch); |
110 | | /// If the given character is an uppercase character, |
111 | | /// return its lowercase counterpart, otherwise return |
112 | | /// the character. |
113 | | |
114 | | static int toUpper(int ch); |
115 | | /// If the given character is a lowercase character, |
116 | | /// return its uppercase counterpart, otherwise return |
117 | | /// the character. |
118 | | |
119 | | private: |
120 | | static const int CHARACTER_PROPERTIES[128]; |
121 | | }; |
122 | | |
123 | | |
124 | | // |
125 | | // inlines |
126 | | // |
127 | | inline int Ascii::properties(int ch) |
128 | 478M | { |
129 | 478M | if (isAscii(ch)) |
130 | 298M | return CHARACTER_PROPERTIES[ch]; |
131 | 180M | else |
132 | 180M | return 0; |
133 | 478M | } |
134 | | |
135 | | |
136 | | inline bool Ascii::isAscii(int ch) |
137 | 478M | { |
138 | 478M | return (static_cast<UInt32>(ch) & 0xFFFFFF80) == 0; |
139 | 478M | } |
140 | | |
141 | | |
142 | | inline bool Ascii::hasProperties(int ch, int props) |
143 | 422M | { |
144 | 422M | return (properties(ch) & props) == props; |
145 | 422M | } |
146 | | |
147 | | |
148 | | inline bool Ascii::hasSomeProperties(int ch, int props) |
149 | 56.8M | { |
150 | 56.8M | return (properties(ch) & props) != 0; |
151 | 56.8M | } |
152 | | |
153 | | |
154 | | inline bool Ascii::isSpace(int ch) |
155 | 38.0M | { |
156 | 38.0M | return hasProperties(ch, ACP_SPACE); |
157 | 38.0M | } |
158 | | |
159 | | |
160 | | inline bool Ascii::isDigit(int ch) |
161 | 59.8M | { |
162 | 59.8M | return hasProperties(ch, ACP_DIGIT); |
163 | 59.8M | } |
164 | | |
165 | | |
166 | | inline bool Ascii::isHexDigit(int ch) |
167 | 171k | { |
168 | 171k | return hasProperties(ch, ACP_HEXDIGIT); |
169 | 171k | } |
170 | | |
171 | | |
172 | | inline bool Ascii::isPunct(int ch) |
173 | 5.58k | { |
174 | 5.58k | return hasProperties(ch, ACP_PUNCT); |
175 | 5.58k | } |
176 | | |
177 | | |
178 | | inline bool Ascii::isAlpha(int ch) |
179 | 7.44M | { |
180 | 7.44M | return hasProperties(ch, ACP_ALPHA); |
181 | 7.44M | } |
182 | | |
183 | | |
184 | | inline bool Ascii::isAlphaNumeric(int ch) |
185 | 56.8M | { |
186 | 56.8M | return hasSomeProperties(ch, ACP_ALPHA | ACP_DIGIT); |
187 | 56.8M | } |
188 | | |
189 | | |
190 | | inline bool Ascii::isLower(int ch) |
191 | 2.24M | { |
192 | 2.24M | return hasProperties(ch, ACP_LOWER); |
193 | 2.24M | } |
194 | | |
195 | | |
196 | | inline bool Ascii::isUpper(int ch) |
197 | 314M | { |
198 | 314M | return hasProperties(ch, ACP_UPPER); |
199 | 314M | } |
200 | | |
201 | | |
202 | | inline bool Ascii::isPrintable(int ch) |
203 | 0 | { |
204 | 0 | return hasProperties(ch, ACP_PRINT); |
205 | 0 | } |
206 | | |
207 | | |
208 | | inline int Ascii::toLower(int ch) |
209 | 314M | { |
210 | 314M | if (isUpper(ch)) |
211 | 11.8M | return ch | 0x20; |
212 | 302M | else |
213 | 302M | return ch; |
214 | 314M | } |
215 | | |
216 | | |
217 | | inline int Ascii::toUpper(int ch) |
218 | 2.24M | { |
219 | 2.24M | if (isLower(ch)) |
220 | 1.85M | return ch & ~0x20; |
221 | 391k | else |
222 | 391k | return ch; |
223 | 2.24M | } |
224 | | |
225 | | |
226 | | } // namespace Poco |
227 | | |
228 | | |
229 | | #endif // Foundation_Ascii_INCLUDED |