1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.tomcat.util.buf;
19
20 /**
21 * This class implements some basic ASCII character handling functions.
22 *
23 * @author dac@eng.sun.com
24 * @author James Todd [gonzo@eng.sun.com]
25 */
26 public final class Ascii {
27 /*
28 * Character translation tables.
29 */
30
31 private static final byte[] toUpper = new byte[256];
32 private static final byte[] toLower = new byte[256];
33
34 /*
35 * Character type tables.
36 */
37
38 private static final boolean[] isAlpha = new boolean[256];
39 private static final boolean[] isUpper = new boolean[256];
40 private static final boolean[] isLower = new boolean[256];
41 private static final boolean[] isWhite = new boolean[256];
42 private static final boolean[] isDigit = new boolean[256];
43
44 /*
45 * Initialize character translation and type tables.
46 */
47
48 static {
49 for (int i = 0; i < 256; i++) {
50 toUpper[i] = (byte)i;
51 toLower[i] = (byte)i;
52 }
53
54 for (int lc = 'a'; lc <= 'z'; lc++) {
55 int uc = lc + 'A' - 'a';
56
57 toUpper[lc] = (byte)uc;
58 toLower[uc] = (byte)lc;
59 isAlpha[lc] = true;
60 isAlpha[uc] = true;
61 isLower[lc] = true;
62 isUpper[uc] = true;
63 }
64
65 isWhite[ ' '] = true;
66 isWhite['\t'] = true;
67 isWhite['\r'] = true;
68 isWhite['\n'] = true;
69 isWhite['\f'] = true;
70 isWhite['\b'] = true;
71
72 for (int d = '0'; d <= '9'; d++) {
73 isDigit[d] = true;
74 }
75 }
76
77 /**
78 * Returns the upper case equivalent of the specified ASCII character.
79 */
80
81 public static int toUpper(int c) {
82 return toUpper[c & 0xff] & 0xff;
83 }
84
85 /**
86 * Returns the lower case equivalent of the specified ASCII character.
87 */
88
89 public static int toLower(int c) {
90 return toLower[c & 0xff] & 0xff;
91 }
92
93 /**
94 * Returns true if the specified ASCII character is upper or lower case.
95 */
96
97 public static boolean isAlpha(int c) {
98 return isAlpha[c & 0xff];
99 }
100
101 /**
102 * Returns true if the specified ASCII character is upper case.
103 */
104
105 public static boolean isUpper(int c) {
106 return isUpper[c & 0xff];
107 }
108
109 /**
110 * Returns true if the specified ASCII character is lower case.
111 */
112
113 public static boolean isLower(int c) {
114 return isLower[c & 0xff];
115 }
116
117 /**
118 * Returns true if the specified ASCII character is white space.
119 */
120
121 public static boolean isWhite(int c) {
122 return isWhite[c & 0xff];
123 }
124
125 /**
126 * Returns true if the specified ASCII character is a digit.
127 */
128
129 public static boolean isDigit(int c) {
130 return isDigit[c & 0xff];
131 }
132
133 /**
134 * Parses an unsigned integer from the specified subarray of bytes.
135 * @param b the bytes to parse
136 * @param off the start offset of the bytes
137 * @param len the length of the bytes
138 * @exception NumberFormatException if the integer format was invalid
139 */
140 public static int parseInt(byte[] b, int off, int len)
141 throws NumberFormatException
142 {
143 int c;
144
145 if (b == null || len <= 0 || !isDigit(c = b[off++])) {
146 throw new NumberFormatException();
147 }
148
149 int n = c - '0';
150
151 while (--len > 0) {
152 if (!isDigit(c = b[off++])) {
153 throw new NumberFormatException();
154 }
155 n = n * 10 + c - '0';
156 }
157
158 return n;
159 }
160
161 public static int parseInt(char[] b, int off, int len)
162 throws NumberFormatException
163 {
164 int c;
165
166 if (b == null || len <= 0 || !isDigit(c = b[off++])) {
167 throw new NumberFormatException();
168 }
169
170 int n = c - '0';
171
172 while (--len > 0) {
173 if (!isDigit(c = b[off++])) {
174 throw new NumberFormatException();
175 }
176 n = n * 10 + c - '0';
177 }
178
179 return n;
180 }
181
182 /**
183 * Parses an unsigned long from the specified subarray of bytes.
184 * @param b the bytes to parse
185 * @param off the start offset of the bytes
186 * @param len the length of the bytes
187 * @exception NumberFormatException if the long format was invalid
188 */
189 public static long parseLong(byte[] b, int off, int len)
190 throws NumberFormatException
191 {
192 int c;
193
194 if (b == null || len <= 0 || !isDigit(c = b[off++])) {
195 throw new NumberFormatException();
196 }
197
198 long n = c - '0';
199 long m;
200
201 while (--len > 0) {
202 if (!isDigit(c = b[off++])) {
203 throw new NumberFormatException();
204 }
205 m = n * 10 + c - '0';
206
207 if (m < n) {
208 // Overflow
209 throw new NumberFormatException();
210 } else {
211 n = m;
212 }
213 }
214
215 return n;
216 }
217
218 public static long parseLong(char[] b, int off, int len)
219 throws NumberFormatException
220 {
221 int c;
222
223 if (b == null || len <= 0 || !isDigit(c = b[off++])) {
224 throw new NumberFormatException();
225 }
226
227 long n = c - '0';
228 long m;
229
230 while (--len > 0) {
231 if (!isDigit(c = b[off++])) {
232 throw new NumberFormatException();
233 }
234 m = n * 10 + c - '0';
235
236 if (m < n) {
237 // Overflow
238 throw new NumberFormatException();
239 } else {
240 n = m;
241 }
242 }
243
244 return n;
245 }
246
247 }