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
24namespace Poco {
25
26
27class 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{
43public:
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
119private:
120 static const int CHARACTER_PROPERTIES[128];
121};
122
123
124//
125// inlines
126//
127inline int Ascii::properties(int ch)
128{
129 if (isAscii(ch))
130 return CHARACTER_PROPERTIES[ch];
131 else
132 return 0;
133}
134
135
136inline bool Ascii::isAscii(int ch)
137{
138 return (static_cast<UInt32>(ch) & 0xFFFFFF80) == 0;
139}
140
141
142inline bool Ascii::hasProperties(int ch, int props)
143{
144 return (properties(ch) & props) == props;
145}
146
147
148inline bool Ascii::hasSomeProperties(int ch, int props)
149{
150 return (properties(ch) & props) != 0;
151}
152
153
154inline bool Ascii::isSpace(int ch)
155{
156 return hasProperties(ch, ACP_SPACE);
157}
158
159
160inline bool Ascii::isDigit(int ch)
161{
162 return hasProperties(ch, ACP_DIGIT);
163}
164
165
166inline bool Ascii::isHexDigit(int ch)
167{
168 return hasProperties(ch, ACP_HEXDIGIT);
169}
170
171
172inline bool Ascii::isPunct(int ch)
173{
174 return hasProperties(ch, ACP_PUNCT);
175}
176
177
178inline bool Ascii::isAlpha(int ch)
179{
180 return hasProperties(ch, ACP_ALPHA);
181}
182
183
184inline bool Ascii::isAlphaNumeric(int ch)
185{
186 return hasSomeProperties(ch, ACP_ALPHA | ACP_DIGIT);
187}
188
189
190inline bool Ascii::isLower(int ch)
191{
192 return hasProperties(ch, ACP_LOWER);
193}
194
195
196inline bool Ascii::isUpper(int ch)
197{
198 return hasProperties(ch, ACP_UPPER);
199}
200
201
202inline bool Ascii::isPrintable(int ch)
203{
204 return hasProperties(ch, ACP_PRINT);
205}
206
207
208inline int Ascii::toLower(int ch)
209{
210 if (isUpper(ch))
211 return ch + 32;
212 else
213 return ch;
214}
215
216
217inline int Ascii::toUpper(int ch)
218{
219 if (isLower(ch))
220 return ch - 32;
221 else
222 return ch;
223}
224
225
226} // namespace Poco
227
228
229#endif // Foundation_Ascii_INCLUDED
230