001 /*
002 * Copyright 1999-2000 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.regex;
027
028 /**
029 * Utility class that implements the standard C ctype functionality.
030 *
031 * @author Hong Zhang
032 */
033
034 final class ASCII {
035
036 static final int UPPER = 0x00000100;
037
038 static final int LOWER = 0x00000200;
039
040 static final int DIGIT = 0x00000400;
041
042 static final int SPACE = 0x00000800;
043
044 static final int PUNCT = 0x00001000;
045
046 static final int CNTRL = 0x00002000;
047
048 static final int BLANK = 0x00004000;
049
050 static final int HEX = 0x00008000;
051
052 static final int UNDER = 0x00010000;
053
054 static final int ASCII = 0x0000FF00;
055
056 static final int ALPHA = (UPPER | LOWER);
057
058 static final int ALNUM = (UPPER | LOWER | DIGIT);
059
060 static final int GRAPH = (PUNCT | UPPER | LOWER | DIGIT);
061
062 static final int WORD = (UPPER | LOWER | UNDER | DIGIT);
063
064 static final int XDIGIT = (HEX);
065
066 private static final int[] ctype = new int[] { CNTRL, /* 00 (NUL) */
067 CNTRL, /* 01 (SOH) */
068 CNTRL, /* 02 (STX) */
069 CNTRL, /* 03 (ETX) */
070 CNTRL, /* 04 (EOT) */
071 CNTRL, /* 05 (ENQ) */
072 CNTRL, /* 06 (ACK) */
073 CNTRL, /* 07 (BEL) */
074 CNTRL, /* 08 (BS) */
075 SPACE + CNTRL + BLANK, /* 09 (HT) */
076 SPACE + CNTRL, /* 0A (LF) */
077 SPACE + CNTRL, /* 0B (VT) */
078 SPACE + CNTRL, /* 0C (FF) */
079 SPACE + CNTRL, /* 0D (CR) */
080 CNTRL, /* 0E (SI) */
081 CNTRL, /* 0F (SO) */
082 CNTRL, /* 10 (DLE) */
083 CNTRL, /* 11 (DC1) */
084 CNTRL, /* 12 (DC2) */
085 CNTRL, /* 13 (DC3) */
086 CNTRL, /* 14 (DC4) */
087 CNTRL, /* 15 (NAK) */
088 CNTRL, /* 16 (SYN) */
089 CNTRL, /* 17 (ETB) */
090 CNTRL, /* 18 (CAN) */
091 CNTRL, /* 19 (EM) */
092 CNTRL, /* 1A (SUB) */
093 CNTRL, /* 1B (ESC) */
094 CNTRL, /* 1C (FS) */
095 CNTRL, /* 1D (GS) */
096 CNTRL, /* 1E (RS) */
097 CNTRL, /* 1F (US) */
098 SPACE + BLANK, /* 20 SPACE */
099 PUNCT, /* 21 ! */
100 PUNCT, /* 22 " */
101 PUNCT, /* 23 # */
102 PUNCT, /* 24 $ */
103 PUNCT, /* 25 % */
104 PUNCT, /* 26 & */
105 PUNCT, /* 27 ' */
106 PUNCT, /* 28 ( */
107 PUNCT, /* 29 ) */
108 PUNCT, /* 2A * */
109 PUNCT, /* 2B + */
110 PUNCT, /* 2C , */
111 PUNCT, /* 2D - */
112 PUNCT, /* 2E . */
113 PUNCT, /* 2F / */
114 DIGIT + HEX + 0, /* 30 0 */
115 DIGIT + HEX + 1, /* 31 1 */
116 DIGIT + HEX + 2, /* 32 2 */
117 DIGIT + HEX + 3, /* 33 3 */
118 DIGIT + HEX + 4, /* 34 4 */
119 DIGIT + HEX + 5, /* 35 5 */
120 DIGIT + HEX + 6, /* 36 6 */
121 DIGIT + HEX + 7, /* 37 7 */
122 DIGIT + HEX + 8, /* 38 8 */
123 DIGIT + HEX + 9, /* 39 9 */
124 PUNCT, /* 3A : */
125 PUNCT, /* 3B ; */
126 PUNCT, /* 3C < */
127 PUNCT, /* 3D = */
128 PUNCT, /* 3E > */
129 PUNCT, /* 3F ? */
130 PUNCT, /* 40 @ */
131 UPPER + HEX + 10, /* 41 A */
132 UPPER + HEX + 11, /* 42 B */
133 UPPER + HEX + 12, /* 43 C */
134 UPPER + HEX + 13, /* 44 D */
135 UPPER + HEX + 14, /* 45 E */
136 UPPER + HEX + 15, /* 46 F */
137 UPPER + 16, /* 47 G */
138 UPPER + 17, /* 48 H */
139 UPPER + 18, /* 49 I */
140 UPPER + 19, /* 4A J */
141 UPPER + 20, /* 4B K */
142 UPPER + 21, /* 4C L */
143 UPPER + 22, /* 4D M */
144 UPPER + 23, /* 4E N */
145 UPPER + 24, /* 4F O */
146 UPPER + 25, /* 50 P */
147 UPPER + 26, /* 51 Q */
148 UPPER + 27, /* 52 R */
149 UPPER + 28, /* 53 S */
150 UPPER + 29, /* 54 T */
151 UPPER + 30, /* 55 U */
152 UPPER + 31, /* 56 V */
153 UPPER + 32, /* 57 W */
154 UPPER + 33, /* 58 X */
155 UPPER + 34, /* 59 Y */
156 UPPER + 35, /* 5A Z */
157 PUNCT, /* 5B [ */
158 PUNCT, /* 5C \ */
159 PUNCT, /* 5D ] */
160 PUNCT, /* 5E ^ */
161 PUNCT | UNDER, /* 5F _ */
162 PUNCT, /* 60 ` */
163 LOWER + HEX + 10, /* 61 a */
164 LOWER + HEX + 11, /* 62 b */
165 LOWER + HEX + 12, /* 63 c */
166 LOWER + HEX + 13, /* 64 d */
167 LOWER + HEX + 14, /* 65 e */
168 LOWER + HEX + 15, /* 66 f */
169 LOWER + 16, /* 67 g */
170 LOWER + 17, /* 68 h */
171 LOWER + 18, /* 69 i */
172 LOWER + 19, /* 6A j */
173 LOWER + 20, /* 6B k */
174 LOWER + 21, /* 6C l */
175 LOWER + 22, /* 6D m */
176 LOWER + 23, /* 6E n */
177 LOWER + 24, /* 6F o */
178 LOWER + 25, /* 70 p */
179 LOWER + 26, /* 71 q */
180 LOWER + 27, /* 72 r */
181 LOWER + 28, /* 73 s */
182 LOWER + 29, /* 74 t */
183 LOWER + 30, /* 75 u */
184 LOWER + 31, /* 76 v */
185 LOWER + 32, /* 77 w */
186 LOWER + 33, /* 78 x */
187 LOWER + 34, /* 79 y */
188 LOWER + 35, /* 7A z */
189 PUNCT, /* 7B { */
190 PUNCT, /* 7C | */
191 PUNCT, /* 7D } */
192 PUNCT, /* 7E ~ */
193 CNTRL, /* 7F (DEL) */
194 };
195
196 static int getType(int ch) {
197 return ((ch & 0xFFFFFF80) == 0 ? ctype[ch] : 0);
198 }
199
200 static boolean isType(int ch, int type) {
201 return (getType(ch) & type) != 0;
202 }
203
204 static boolean isAscii(int ch) {
205 return ((ch & 0xFFFFFF80) == 0);
206 }
207
208 static boolean isAlpha(int ch) {
209 return isType(ch, ALPHA);
210 }
211
212 static boolean isDigit(int ch) {
213 return ((ch - '0') | ('9' - ch)) >= 0;
214 }
215
216 static boolean isAlnum(int ch) {
217 return isType(ch, ALNUM);
218 }
219
220 static boolean isGraph(int ch) {
221 return isType(ch, GRAPH);
222 }
223
224 static boolean isPrint(int ch) {
225 return ((ch - 0x20) | (0x7E - ch)) >= 0;
226 }
227
228 static boolean isPunct(int ch) {
229 return isType(ch, PUNCT);
230 }
231
232 static boolean isSpace(int ch) {
233 return isType(ch, SPACE);
234 }
235
236 static boolean isHexDigit(int ch) {
237 return isType(ch, HEX);
238 }
239
240 static boolean isOctDigit(int ch) {
241 return ((ch - '0') | ('7' - ch)) >= 0;
242 }
243
244 static boolean isCntrl(int ch) {
245 return isType(ch, CNTRL);
246 }
247
248 static boolean isLower(int ch) {
249 return ((ch - 'a') | ('z' - ch)) >= 0;
250 }
251
252 static boolean isUpper(int ch) {
253 return ((ch - 'A') | ('Z' - ch)) >= 0;
254 }
255
256 static boolean isWord(int ch) {
257 return isType(ch, WORD);
258 }
259
260 static int toDigit(int ch) {
261 return (ctype[ch & 0x7F] & 0x3F);
262 }
263
264 static int toLower(int ch) {
265 return isUpper(ch) ? (ch + 0x20) : ch;
266 }
267
268 static int toUpper(int ch) {
269 return isLower(ch) ? (ch - 0x20) : ch;
270 }
271
272 }
|