KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > regex > ASCII


1 /*
2  * @(#)ASCII.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.regex;
9
10
11 /**
12  * Utility class that implements the standard C ctype functionality.
13  *
14  * @author Hong Zhang
15  */

16
17 final class ASCII {
18     
19     static final int UPPER = 0x00000100;
20     
21     static final int LOWER = 0x00000200;
22     
23     static final int DIGIT = 0x00000400;
24     
25     static final int SPACE = 0x00000800;
26     
27     static final int PUNCT = 0x00001000;
28     
29     static final int CNTRL = 0x00002000;
30     
31     static final int BLANK = 0x00004000;
32     
33     static final int HEX = 0x00008000;
34     
35     static final int UNDER = 0x00010000;
36     
37     static final int ASCII = 0x0000FF00;
38
39     static final int ALPHA = (UPPER|LOWER);
40
41     static final int ALNUM = (UPPER|LOWER|DIGIT);
42
43     static final int GRAPH = (PUNCT|UPPER|LOWER|DIGIT);
44
45     static final int WORD = (UPPER|LOWER|UNDER|DIGIT);
46
47     static final int XDIGIT = (HEX);
48
49     private static final int[] ctype = new int[] {
50         CNTRL, /* 00 (NUL) */
51         CNTRL, /* 01 (SOH) */
52         CNTRL, /* 02 (STX) */
53         CNTRL, /* 03 (ETX) */
54         CNTRL, /* 04 (EOT) */
55         CNTRL, /* 05 (ENQ) */
56         CNTRL, /* 06 (ACK) */
57         CNTRL, /* 07 (BEL) */
58         CNTRL, /* 08 (BS) */
59         SPACE+CNTRL+BLANK, /* 09 (HT) */
60         SPACE+CNTRL, /* 0A (LF) */
61         SPACE+CNTRL, /* 0B (VT) */
62         SPACE+CNTRL, /* 0C (FF) */
63         SPACE+CNTRL, /* 0D (CR) */
64         CNTRL, /* 0E (SI) */
65         CNTRL, /* 0F (SO) */
66         CNTRL, /* 10 (DLE) */
67         CNTRL, /* 11 (DC1) */
68         CNTRL, /* 12 (DC2) */
69         CNTRL, /* 13 (DC3) */
70         CNTRL, /* 14 (DC4) */
71         CNTRL, /* 15 (NAK) */
72         CNTRL, /* 16 (SYN) */
73         CNTRL, /* 17 (ETB) */
74         CNTRL, /* 18 (CAN) */
75         CNTRL, /* 19 (EM) */
76         CNTRL, /* 1A (SUB) */
77         CNTRL, /* 1B (ESC) */
78         CNTRL, /* 1C (FS) */
79         CNTRL, /* 1D (GS) */
80         CNTRL, /* 1E (RS) */
81         CNTRL, /* 1F (US) */
82         SPACE+BLANK, /* 20 SPACE */
83         PUNCT, /* 21 ! */
84         PUNCT, /* 22 " */
85         PUNCT, /* 23 # */
86         PUNCT, /* 24 $ */
87         PUNCT, /* 25 % */
88         PUNCT, /* 26 & */
89         PUNCT, /* 27 ' */
90         PUNCT, /* 28 ( */
91         PUNCT, /* 29 ) */
92         PUNCT, /* 2A * */
93         PUNCT, /* 2B + */
94         PUNCT, /* 2C , */
95         PUNCT, /* 2D - */
96         PUNCT, /* 2E . */
97         PUNCT, /* 2F / */
98         DIGIT+HEX+0, /* 30 0 */
99         DIGIT+HEX+1, /* 31 1 */
100         DIGIT+HEX+2, /* 32 2 */
101         DIGIT+HEX+3, /* 33 3 */
102         DIGIT+HEX+4, /* 34 4 */
103         DIGIT+HEX+5, /* 35 5 */
104         DIGIT+HEX+6, /* 36 6 */
105         DIGIT+HEX+7, /* 37 7 */
106         DIGIT+HEX+8, /* 38 8 */
107         DIGIT+HEX+9, /* 39 9 */
108         PUNCT, /* 3A : */
109         PUNCT, /* 3B ; */
110         PUNCT, /* 3C < */
111         PUNCT, /* 3D = */
112         PUNCT, /* 3E > */
113         PUNCT, /* 3F ? */
114         PUNCT, /* 40 @ */
115         UPPER+HEX+10, /* 41 A */
116         UPPER+HEX+11, /* 42 B */
117         UPPER+HEX+12, /* 43 C */
118         UPPER+HEX+13, /* 44 D */
119         UPPER+HEX+14, /* 45 E */
120         UPPER+HEX+15, /* 46 F */
121         UPPER+16, /* 47 G */
122         UPPER+17, /* 48 H */
123         UPPER+18, /* 49 I */
124         UPPER+19, /* 4A J */
125         UPPER+20, /* 4B K */
126         UPPER+21, /* 4C L */
127         UPPER+22, /* 4D M */
128         UPPER+23, /* 4E N */
129         UPPER+24, /* 4F O */
130         UPPER+25, /* 50 P */
131         UPPER+26, /* 51 Q */
132         UPPER+27, /* 52 R */
133         UPPER+28, /* 53 S */
134         UPPER+29, /* 54 T */
135         UPPER+30, /* 55 U */
136         UPPER+31, /* 56 V */
137         UPPER+32, /* 57 W */
138         UPPER+33, /* 58 X */
139         UPPER+34, /* 59 Y */
140         UPPER+35, /* 5A Z */
141         PUNCT, /* 5B [ */
142         PUNCT, /* 5C \ */
143         PUNCT, /* 5D ] */
144         PUNCT, /* 5E ^ */
145         PUNCT|UNDER, /* 5F _ */
146         PUNCT, /* 60 ` */
147         LOWER+HEX+10, /* 61 a */
148         LOWER+HEX+11, /* 62 b */
149         LOWER+HEX+12, /* 63 c */
150         LOWER+HEX+13, /* 64 d */
151         LOWER+HEX+14, /* 65 e */
152         LOWER+HEX+15, /* 66 f */
153         LOWER+16, /* 67 g */
154         LOWER+17, /* 68 h */
155         LOWER+18, /* 69 i */
156         LOWER+19, /* 6A j */
157         LOWER+20, /* 6B k */
158         LOWER+21, /* 6C l */
159         LOWER+22, /* 6D m */
160         LOWER+23, /* 6E n */
161         LOWER+24, /* 6F o */
162         LOWER+25, /* 70 p */
163         LOWER+26, /* 71 q */
164         LOWER+27, /* 72 r */
165         LOWER+28, /* 73 s */
166         LOWER+29, /* 74 t */
167         LOWER+30, /* 75 u */
168         LOWER+31, /* 76 v */
169         LOWER+32, /* 77 w */
170         LOWER+33, /* 78 x */
171         LOWER+34, /* 79 y */
172         LOWER+35, /* 7A z */
173         PUNCT, /* 7B { */
174         PUNCT, /* 7C | */
175         PUNCT, /* 7D } */
176         PUNCT, /* 7E ~ */
177         CNTRL, /* 7F (DEL) */
178     };
179
180     static int getType(int ch) {
181         return ((ch & 0xFFFFFF80) == 0 ? ctype[ch] : 0);
182     }
183
184     static boolean isType(int ch, int type) {
185         return (getType(ch) & type) != 0;
186     }
187
188     static boolean isAscii(int ch) {
189         return ((ch & 0xFFFFFF80) == 0);
190     }
191
192     static boolean isAlpha(int ch) {
193         return isType(ch, ALPHA);
194     }
195
196     static boolean isDigit(int ch) {
197         return ((ch-'0')|('9'-ch)) >= 0;
198     }
199
200     static boolean isAlnum(int ch) {
201         return isType(ch, ALNUM);
202     }
203
204     static boolean isGraph(int ch) {
205         return isType(ch, GRAPH);
206     }
207
208     static boolean isPrint(int ch) {
209         return ((ch-0x20)|(0x7E-ch)) >= 0;
210     }
211
212     static boolean isPunct(int ch) {
213         return isType(ch, PUNCT);
214     }
215
216     static boolean isSpace(int ch) {
217         return isType(ch, SPACE);
218     }
219
220     static boolean isHexDigit(int ch) {
221         return isType(ch, HEX);
222     }
223
224     static boolean isOctDigit(int ch) {
225         return ((ch-'0')|('7'-ch)) >= 0;
226     }
227
228     static boolean isCntrl(int ch) {
229         return isType(ch, CNTRL);
230     }
231
232     static boolean isLower(int ch) {
233         return ((ch-'a')|('z'-ch)) >= 0;
234     }
235
236     static boolean isUpper(int ch) {
237         return ((ch-'A')|('Z'-ch)) >= 0;
238     }
239
240     static boolean isWord(int ch) {
241         return isType(ch, WORD);
242     }
243
244     static int toDigit(int ch) {
245         return (ctype[ch & 0x7F] & 0x3F);
246     }
247
248     static int toLower(int ch) {
249         return isUpper(ch) ? (ch + 0x20) : ch;
250     }
251
252     static int toUpper(int ch) {
253         return isLower(ch) ? (ch - 0x20) : ch;
254     }
255
256 }
257
Popular Tags