| 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 int toLower(int ch); |
| 107 | /// If the given character is an uppercase character, |
| 108 | /// return its lowercase counterpart, otherwise return |
| 109 | /// the character. |
| 110 | |
| 111 | static int toUpper(int ch); |
| 112 | /// If the given character is a lowercase character, |
| 113 | /// return its uppercase counterpart, otherwise return |
| 114 | /// the character. |
| 115 | |
| 116 | private: |
| 117 | static const int CHARACTER_PROPERTIES[128]; |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | // |
| 122 | // inlines |
| 123 | // |
| 124 | inline int Ascii::properties(int ch) |
| 125 | { |
| 126 | if (isAscii(ch)) |
| 127 | return CHARACTER_PROPERTIES[ch]; |
| 128 | else |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | inline bool Ascii::isAscii(int ch) |
| 134 | { |
| 135 | return (static_cast<UInt32>(ch) & 0xFFFFFF80) == 0; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | inline bool Ascii::hasProperties(int ch, int props) |
| 140 | { |
| 141 | return (properties(ch) & props) == props; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | inline bool Ascii::hasSomeProperties(int ch, int props) |
| 146 | { |
| 147 | return (properties(ch) & props) != 0; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | inline bool Ascii::isSpace(int ch) |
| 152 | { |
| 153 | return hasProperties(ch, ACP_SPACE); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | inline bool Ascii::isDigit(int ch) |
| 158 | { |
| 159 | return hasProperties(ch, ACP_DIGIT); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | inline bool Ascii::isHexDigit(int ch) |
| 164 | { |
| 165 | return hasProperties(ch, ACP_HEXDIGIT); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | inline bool Ascii::isPunct(int ch) |
| 170 | { |
| 171 | return hasProperties(ch, ACP_PUNCT); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | inline bool Ascii::isAlpha(int ch) |
| 176 | { |
| 177 | return hasProperties(ch, ACP_ALPHA); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | inline bool Ascii::isAlphaNumeric(int ch) |
| 182 | { |
| 183 | return hasSomeProperties(ch, ACP_ALPHA | ACP_DIGIT); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | inline bool Ascii::isLower(int ch) |
| 188 | { |
| 189 | return hasProperties(ch, ACP_LOWER); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | inline bool Ascii::isUpper(int ch) |
| 194 | { |
| 195 | return hasProperties(ch, ACP_UPPER); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | inline int Ascii::toLower(int ch) |
| 200 | { |
| 201 | if (isUpper(ch)) |
| 202 | return ch + 32; |
| 203 | else |
| 204 | return ch; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | inline int Ascii::toUpper(int ch) |
| 209 | { |
| 210 | if (isLower(ch)) |
| 211 | return ch - 32; |
| 212 | else |
| 213 | return ch; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | } // namespace Poco |
| 218 | |
| 219 | |
| 220 | #endif // Foundation_Ascii_INCLUDED |
| 221 | |